当我们单独添加元素时,我想添加一个ggplot图例。
我已经知道此问题的先前答案,但出于某种原因,我无法解决我的问题(Add legend to ggplot2 line plot)。谢谢!
library(ggplot2)
Month <- c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8,
8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12)
RR <- c(81.30271, 56.97511, 88.95428, 78.43363, 51.39398, 83.53967,
95.21302, 63.74089, 94.27137, 92.36272, 69.30449, 100.34571,
128.52172, 89.84833, 133.61527, 128.23847, 91.21498, 145.45016,
124.72499, 85.96523, 153.82113, 123.22878, 97.14116, 154.57004,
111.59289, 83.26763, 105.47873, 95.55557, 88.77395, 87.31212,
93.03579, 65.35055, 93.22413, 103.43140, 70.40292, 91.58487)
MI <- c(66.729379, 41.891775, 52.137614, 59.376967, 30.717318, 49.339675,
62.469691, 25.667561, 60.211374, 48.902722, 18.764486, 65.565712,
69.985054, 27.418330, 89.231939, 55.685138, 11.484980, 90.666826,
44.101654, -8.448102, 87.637798, 55.978782, 15.431156, 92.310042,
69.596228, 34.897628, 56.505393, 69.008904, 61.627285, 36.935451,
76.392457, 47.493886, 53.750796, 88.204738, 54.806257, 54.358201)
PE <- c(14.57333, 15.08333, 36.81667, 19.05667, 20.67667, 34.20000, 32.74333,
38.07333, 34.06000, 43.46000, 50.54000, 34.78000, 58.53667, 62.43000,
44.38333, 72.55333, 79.73000, 54.78333, 80.62333, 94.41333, 66.18333,
67.25000, 81.71000, 62.26000, 41.99667, 48.37000, 48.97333, 26.54667,
27.14667, 50.37667, 16.64333, 17.85667, 39.47333, 15.22667, 15.59667,
37.22667)
tt <- data.frame(Month, RR, MI, PE)
ggplot(data = tt,
aes(x = factor(Month))) +
geom_boxplot(aes(y = RR, x = factor(Month)),
fill = "dodgerblue4", colour = "dodgerblue4",
alpha = 0.6) +
stat_summary(aes(y = RR, x = Month),
fun.y = mean,
geom = "smooth",
colour = "dodgerblue4") +
geom_boxplot(aes(y = MI, group = Month),
fill = "dimgray", colour = "dimgray",
alpha = 0.6) +
stat_summary(aes(y = MI, x = Month),
fun.y = mean,
geom = "smooth",
colour = "dimgray") +
geom_boxplot(aes(y = PE, group = Month),
fill = "firebrick", colour = "firebrick",
alpha = 0.6) +
stat_summary(aes(y = PE, x = Month),
fun.y = mean,
geom = "smooth",
colour = "firebrick") +
labs(x = "Months",
y = "Flux, mm") +
scale_fill_manual("",
breaks = c("dodgerblue4", "dimgray", "firebrick"),
labels = c("dodgerblue4", "dimgray", "firebrick")) +
theme_bw(base_size = 18)
答案 0 :(得分:2)
您可以借助named
向量修复代码以定义颜色。确保使用named
向量中的颜色,每个元素的范围为aes
。
其中一个选项可以是:
# Named vector for color
lineColors <- c("RR" = "dodgerblue4", "MI" = "dimgray", "PE" = "firebrick")
ggplot(data = tt,
aes(x = factor(Month))) +
geom_boxplot(aes(y = RR, x = factor(Month), fill = "RR"),
colour = "dodgerblue4",
alpha = 0.6) +
stat_summary(aes(y = RR, x = Month, colour = "RR"),
fun.y = mean,
geom = "smooth") +
geom_boxplot(aes(y = MI, group = Month, fill = "MI"),
colour = "dimgray",
alpha = 0.6) +
stat_summary(aes(y = MI, x = Month, colour = "MI"),
fun.y = mean,
geom = "smooth"
) +
geom_boxplot(aes(y = PE, group = Month, fill = "PE"),
colour = "firebrick",
alpha = 0.6) +
stat_summary(aes(y = PE, x = Month, colour = "PE"),
fun.y = mean,
geom = "smooth"
) +
labs(x = "Months",
y = "Flux, mm") +
scale_colour_manual(name = "Type", values = lineColors) +
scale_fill_manual(name = "Type", values = lineColors) +
theme_bw(base_size = 18)
答案 1 :(得分:2)