ggplot图例未显示

huangapple go评论102阅读模式
英文:

ggplot legend does not appear

问题

我的绘图代码如下:

  1. ggplot(NULL, aes(V2, V1)) +
  2. geom_line(data = df1, colour = "blue") +
  3. geom_line(data = df2, colour = "green") +
  4. geom_line(data = df3, colour = "purple") +
  5. theme(
  6. legend.position = "bottom") +

df1、df2 和 df3 具有以下相同的结构:

  1. V2 V1
  2. 1 1.4
  3. 2 1.5
  4. 3 1.9
  5. 4 4.5
  6. 5 6.7
  7. 6 7.8
  8. 7 8.1
  9. 8 8.2
  10. 9 8.3
  11. 10 8.9

输出中未显示图例,但我需要在图上区分这些线。

英文:

My code for the plot is the following

  1. ggplot(NULL, aes(V2, V1)) +
  2. geom_line(data = df1, colour = "blue") +
  3. geom_line(data = df2, colour = "green") +
  4. geom_line(data = df3, colour = "purple") +
  5. theme(
  6. legend.position = "bottom") +

df1, df2, and df3 have the same following structure

  1. V2 V1
  2. 1 1.4
  3. 2 1.5
  4. 3 1.9
  5. 4 4.5
  6. 5 6.7
  7. 6 7.8
  8. 7 8.1
  9. 8 8.2
  10. 9 8.3
  11. 10 8.9

The output does not show the legend, but I need to differentiate between the lines on the plot.

ggplot图例未显示. I need just to have the legend here.

答案1

得分: 3

以下是翻译好的部分:

也许将数据放入一个数据框中会更有帮助,然后使用一个 geom_line 来绘制数据,而不是多个。

  1. library(ggplot2)
  2. dplyr::bind_rows(df1 = df1, df2 = df2, df3 = df3, .id = "id") %>%
  3. ggplot() + aes(V2, V1, color = id) +
  4. geom_line() +
  5. theme(legend.position = "bottom")

数据:

  1. df1 <- structure(list(V2 = 1:10, V1 = c(1.4, 1.5, 1.9, 4.5, 6.7, 7.8,
  2. 8.1, 8.2, 8.3, 8.9)), class = "data.frame", row.names = c(NA, -10L))
  3. df2 <- structure(list(V2 = 1:10, V1 = c(1.43390152077191, 2.30610947613604,
  4. 2.23775280718692, 5.41628585802391, 7.05710641788319, 8.77536501311697,
  5. 8.48437852263451, 8.68867353517562, 8.7907762312796, 8.91225462416187
  6. )), row.names = c(NA, -10L), class = "data.frame")
  7. df3 <- structure(list(V2 = 1:10, V1 = c(2.04147320063785, 2.01257497165352,
  8. 2.22035211822949, 5.08143315766938, 7.31734440829605, 8.23827453767881,
  9. 8.27036898061633, 8.91508049662225, 9.04778654868715, 9.74391470812261
  10. )), row.names = c(NA, -10L), class = "data.frame")

ggplot图例未显示

英文:

Maybe it would be helpful if you get the data into one dataframe, then plot the data with one geom_line instead of multiple.

  1. library(ggplot2)
  2. dplyr::bind_rows(df1 = df1, df2 = df2, df3 = df3, .id = &quot;id&quot;) %&gt;%
  3. ggplot() + aes(V2, V1, color = id) +
  4. geom_line() +
  5. theme(legend.position = &quot;bottom&quot;)

ggplot图例未显示

data

  1. df1 &lt;- structure(list(V2 = 1:10, V1 = c(1.4, 1.5, 1.9, 4.5, 6.7, 7.8,
  2. 8.1, 8.2, 8.3, 8.9)), class = &quot;data.frame&quot;, row.names = c(NA, -10L))
  3. df2 &lt;- structure(list(V2 = 1:10, V1 = c(1.43390152077191, 2.30610947613604,
  4. 2.23775280718692, 5.41628585802391, 7.05710641788319, 8.77536501311697,
  5. 8.48437852263451, 8.68867353517562, 8.7907762312796, 8.91225462416187
  6. )), row.names = c(NA, -10L), class = &quot;data.frame&quot;)
  7. df3 &lt;- structure(list(V2 = 1:10, V1 = c(2.04147320063785, 2.01257497165352,
  8. 2.22035211822949, 5.08143315766938, 7.31734440829605, 8.23827453767881,
  9. 8.27036898061633, 8.91508049662225, 9.04778654868715, 9.74391470812261
  10. )), row.names = c(NA, -10L), class = &quot;data.frame&quot;)

huangapple
  • 本文由 发表于 2020年1月6日 14:38:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59607705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定