如何在 R 中的 goem_hline() 中添加和编辑图例
how to add and edit a legend in goem_hline() in R
我正在尝试为 geom_hline ()
添加标题,但我不知道该怎么做。
我试过 geom_hline (aes (yintercept = 18, 'Historical serie'), size = 1.5, col =" # ffff00 ")
但它 returns 出错了。我拍了 'Historical serie' 它起作用了,但是标题没有出现。
library(tidyr)
library(dplyr)
library(lubridate)
library(ggplot2)
date2<-c('01/01/2000','08/08/2000','16/03/2001','06/09/2001')
name<-c('A','B','A','B')
total<-c(23,25,36,41)
esp<-c(0.363,0.4123,0.09,0.65)
df<-data.frame(date2,name, total, name, esp)%>%
mutate(date3=as.Date(date2, "%d/%m/%Y"))%>%
mutate(year_date=as.Date(trunc(as.POSIXlt(date3), "years")))
ggplot(data=df, aes(x=year_date, y=total, fill=name))+
geom_col(position = "dodge")+
scale_fill_manual(values=c("#08088A","#9F81F7"))+
theme_light()+labs(fill="rain", title ="rain anual", x="Estation", y="total mm")+
theme(axis.text.x = element_text(angle = 90))+
geom_hline(aes(yintercept = 18, 'Historiacal serie'), size=1.5, col="#ffff00")
预期
我希望显示值 18
以及黄线的描述 Historical serie
。
更新:OP 请求:
p <- ggplot(data=df, aes(x=year_date, y=total, fill=name))+
geom_col(position = "dodge")+
scale_fill_manual(values=c("#08088A","#9F81F7"))+
theme_light()+labs(fill="rain", title ="rain anual", x="Estation", y="total mm")+
theme(axis.text.x = element_text(angle = 90))
data_line <- data.frame(yintercept=18, data_line=factor(18))
p +
geom_hline(data=data_line, aes(yintercept=yintercept, linetype="18"), size=1.5, col="#ffff00") +
scale_linetype_manual(name = "Historiacal serie",values = c(1,1))
我在 https://community.rstudio.com/t/how-to-add-legend-in-goem-hline-in-r/105127/2 的帮助下得到它,只使用 geom_hline ()
备选
date2<-c('01/01/2000','08/08/2000','16/03/2001','06/09/2001')
name<-c('A','B','A','B')
total<-c(23,25,36,41)
esp<-c(0.363,0.4123,0.09,0.65)
ggplot(data=df, aes(x=year_date, y=total, fill=name))+
geom_col(position = "dodge")+
scale_fill_manual(values=c("#08088A","#9F81F7"))+
theme_light()+labs(fill="rain", title ="rain anual", x="Estation", y="total mm")+
theme(axis.text.x = element_text(angle = 90))+
geom_hline(aes(yintercept = 18, linetype = "18"), colour = "green", size=1.5) +
scale_linetype_manual(name ="Historical serie", values = c('solid'))
我正在尝试为 geom_hline ()
添加标题,但我不知道该怎么做。
我试过 geom_hline (aes (yintercept = 18, 'Historical serie'), size = 1.5, col =" # ffff00 ")
但它 returns 出错了。我拍了 'Historical serie' 它起作用了,但是标题没有出现。
library(tidyr)
library(dplyr)
library(lubridate)
library(ggplot2)
date2<-c('01/01/2000','08/08/2000','16/03/2001','06/09/2001')
name<-c('A','B','A','B')
total<-c(23,25,36,41)
esp<-c(0.363,0.4123,0.09,0.65)
df<-data.frame(date2,name, total, name, esp)%>%
mutate(date3=as.Date(date2, "%d/%m/%Y"))%>%
mutate(year_date=as.Date(trunc(as.POSIXlt(date3), "years")))
ggplot(data=df, aes(x=year_date, y=total, fill=name))+
geom_col(position = "dodge")+
scale_fill_manual(values=c("#08088A","#9F81F7"))+
theme_light()+labs(fill="rain", title ="rain anual", x="Estation", y="total mm")+
theme(axis.text.x = element_text(angle = 90))+
geom_hline(aes(yintercept = 18, 'Historiacal serie'), size=1.5, col="#ffff00")
预期
我希望显示值 18
以及黄线的描述 Historical serie
。
更新:OP 请求:
p <- ggplot(data=df, aes(x=year_date, y=total, fill=name))+
geom_col(position = "dodge")+
scale_fill_manual(values=c("#08088A","#9F81F7"))+
theme_light()+labs(fill="rain", title ="rain anual", x="Estation", y="total mm")+
theme(axis.text.x = element_text(angle = 90))
data_line <- data.frame(yintercept=18, data_line=factor(18))
p +
geom_hline(data=data_line, aes(yintercept=yintercept, linetype="18"), size=1.5, col="#ffff00") +
scale_linetype_manual(name = "Historiacal serie",values = c(1,1))
我在 https://community.rstudio.com/t/how-to-add-legend-in-goem-hline-in-r/105127/2 的帮助下得到它,只使用 geom_hline ()
备选
date2<-c('01/01/2000','08/08/2000','16/03/2001','06/09/2001')
name<-c('A','B','A','B')
total<-c(23,25,36,41)
esp<-c(0.363,0.4123,0.09,0.65)
ggplot(data=df, aes(x=year_date, y=total, fill=name))+
geom_col(position = "dodge")+
scale_fill_manual(values=c("#08088A","#9F81F7"))+
theme_light()+labs(fill="rain", title ="rain anual", x="Estation", y="total mm")+
theme(axis.text.x = element_text(angle = 90))+
geom_hline(aes(yintercept = 18, linetype = "18"), colour = "green", size=1.5) +
scale_linetype_manual(name ="Historical serie", values = c('solid'))