R 如何在ggplot中为垂直线添加图例?
我试图用一些垂直线作为特定时间点的指示器显示一个折线图,并用图例标记这些时间点 根据ggplot2一书第6.4.4节中的示例,我尝试了以下方法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)) +
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