r - 如何将用户定义的图例添加到 R 中的 ggplot?

标签 r ggplot2 plot legend

我正在尝试手动将图例添加到我的 ggplot,但是,我一无所获。做了一些搜索,但找不到相关的解决方案。到目前为止,这是我的代码

library(tidyverse)
library(zoo)

FakeData = data.frame(A = runif(184, 50,100), B = A + 20, C = A + 40, D = A -20, E = A-10, F = A-15, G = seq(1:184))

lab=c("May", "Jun","July", "Aug","Sep","Oct", "Nov")
ggplot(FakeData, aes(G, ymin=A, ymax=B))+geom_ribbon(fill= "grey75")+geom_line(aes(G, C),linetype = "dashed", col = "red", size = 1.3)+
  geom_line(aes(G, D),linetype = "dashed", colour = "black", size = 1.3)+
  geom_line(aes(G, E), colour = "darkblue", size = 1.3)+ geom_point(aes(G, F), col = "blue", size = 1.3)+ 
  scale_x_continuous(breaks = c(0,31,61,92,123,153,184), labels = lab)+ xlab("Month")+ ylab("Daily Cumulative Precipitation (mm)")+ theme_bw()

我想为图添加以下图例

 Leg = c("Upper and lower Quartile", "Maximum", "Minimum", "Median", "Precipitation")

对 ggplot 的编辑

我根据建议修改了 ggplot 代码,但是,数据颜色与其图例不符。例如,颜色编码为红色的 Max 数据应该位于图中所有其他数据的顶部,但事实并非如此——对于其他图例也是如此。

ggplot(FakeData)+geom_ribbon(aes(G, ymin=A, ymax=B, col = "cyan"), alpha = 0.3)+geom_line(aes(G, C, col = "red"),linetype = "dashed", size = 1.3)+
  geom_line(aes(G, D, col = "black"),linetype = "dashed", size = 1.3)+
  geom_line(aes(G, E, col = "darkblue"), size = 1.3)+ geom_point(aes(G, F, col = "blue"), size = 1.3)+ 
  scale_x_continuous(breaks = c(0,31,61,92,123,153,184), labels = lab)+ xlab("Month")+ ylab("Daily Cumulative Precipitation (mm)")+ theme_bw()+
  scale_color_manual(labels =  c("Upper and lower Quartile", "Maximum", "Minimum", "Median", "Precipitation"), values = c("cyan", "red","black", "darkblue","blue") )

这是更新代码的图,其中图例未指向正确的数据图。 enter image description here

最终产品应该是一个有点像这样的图(图例显示在左上角。 enter image description here

最佳答案

要阐明图例中的不同颜色,您可以修改 Leg 向量以将颜色分配给特定标签(重命名为 CType)。然后,您可以直接在 geom_linegeom_point 语句中提供标签,这将与图例中显示的内容相同。如评论中所述,我将 color 移到了图例的美学内部。要显示图例,您可以使用 scale_color_manual

编辑:要获得不同的线型 - 制作一个类似的矢量 LType 来指示哪种线型(虚线、实线等)。没有使用 geom_point 进行沉淀,而是使用了点线型 - 否则,很难将 colorlinetype 图例组合成一个图例(除非你为 geom_point 指定 linetype 会引发警告但随后忽略)。为色带边界添加了额外的线条,因此它会显示在图例中。

library(tidyverse)
library(zoo)

set.seed(1)
N=184
A = runif(N, 50, 100)
G = seq(1:N)
FakeData = data.frame(A, B = A + 20, C = A + 40, D = A - 20, E = A + 10, F = A - 15, G)

lab=c("May", "Jun", "July", "Aug", "Sep", "Oct", "Nov")

CType = c("Upper and lower Quartile" = "grey75", "Maximum" = "red", "Minimum" = "black", "Median" = "darkblue", "Precipitation" = "blue")
LType = c("Upper and lower Quartile" = "solid", "Maximum" = "dashed", "Minimum" = "dashed", "Median" = "solid", "Precipitation" = "dotted")

ggplot(FakeData, aes(x = G))+
  geom_ribbon(aes(ymin = A, ymax = B), fill= "grey75")+
  geom_line(aes(y = A, colour = "Upper and lower Quartile", linetype = "Upper and lower Quartile"), size = 1.3)+
  geom_line(aes(y = B, colour = "Upper and lower Quartile", linetype = "Upper and lower Quartile"), size = 1.3)+
  geom_line(aes(y = C, colour = "Maximum", linetype = "Maximum"), size = 1.3)+
  geom_line(aes(y = D, colour = "Minimum", linetype = "Minimum"), size = 1.3)+
  geom_line(aes(y = E, colour = "Median", linetype = "Median"), size = 1.3)+
  geom_line(aes(y = F, colour = "Precipitation", linetype = "Precipitation"), size = 1.3)+ 
  scale_x_continuous(breaks = c(0,31,61,92,123,153,184), labels = lab)+ 
  xlab("Month")+ 
  ylab("Daily Cumulative Precipitation (mm)")+ 
  theme_bw()+
  scale_color_manual(name = "legend", values = CType)+
  scale_linetype_manual(name = "legend", values = LType)+
  theme(legend.position = c(.2, .8), legend.title = element_blank(), legend.key.width = unit(2, "cm")) 

plot with custom legend

关于r - 如何将用户定义的图例添加到 R 中的 ggplot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58001307/

相关文章:

r - 将新项目添加到 ggplot2 图例

r - 在ggplot2中结合图例

r - 重叠具有不同变量的ggplot2直方图

r - 如何将轴中断设置为轴的一部分?

r - 如果所有列都包含 NA,则删除行中的 NA 值,但保留至少包含一个结果的行

r - Bookdown:是否可以在小节级别达到 "split_by"?

r - 检测简单数值向量中的一个或多个拐点

r - 对满足字符串条件的连续行进行子集化

c++ - 是否有适用于 C++ 的 'out-of-the-box' 2D/3D 绘图库?

r - 使用 R 中 ggplot2 中的 geom_jitter 和 geom_boxplot 控制点颜色