Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在ggplot中为垂直线添加图例?_R_Ggplot2_Legend - Fatal编程技术网

R 如何在ggplot中为垂直线添加图例?

R 如何在ggplot中为垂直线添加图例?,r,ggplot2,legend,R,Ggplot2,Legend,我试图用一些垂直线作为特定时间点的指示器显示一个折线图,并用图例标记这些时间点 根据ggplot2一书第6.4.4节中的示例,我尝试了以下方法 huron<-data.frame(year=1875:1972,level=LakeHuron) vertDf<-data.frame(years=c(1900,1925,1950),labels=c("A","B","C")) pltWithLines<-ggplot(huron, aes (x=year,y=level)) +

我试图用一些垂直线作为特定时间点的指示器显示一个折线图,并用图例标记这些时间点

根据ggplot2一书第6.4.4节中的示例,我尝试了以下方法

huron<-data.frame(year=1875:1972,level=LakeHuron)

vertDf<-data.frame(years=c(1900,1925,1950),labels=c("A","B","C"))

pltWithLines<-ggplot(huron, aes (x=year,y=level)) +
                              geom_line() + 
                              geom_vline(aes(xintercept=years,color=labels),data=vertDf) + 
                              scale_color_manual("Sample Year",
                                      values=c("A"="blue","B"="red","C"="green"))

huron您需要
show\u指南

ggplot(huron, aes (x=year,y=level)) +
  geom_line() + 
  geom_vline(aes(xintercept=years, color=labels),data=vertDf, show_guide=T) + 
  scale_color_manual("Sample Year",
                     values=c("A"="blue","B"="red","C"="green"))

行得通,谢谢。这方面的文档很奇怪。最初几次我在geom_vline帮助中查找show_guide的文档时,我没有看到它。我认为这是因为函数原型中的参数与它们的列表顺序不同。
show\u guide
已被弃用,取而代之的是
show.legend