繁体   English   中英

如何使用点图向ggplot2线添加图例?

[英]How to add legend to ggplot2 line with point plot?

我尝试了很多代码,但总是无法为图形添加图例。我想为测量的红点和模拟黑线添加图例图形仍然缺少图例,代码如下

    library(foreign)
    library(ggplot2)
    library(dplyr)
    library(readxl)
    library(scales)
    Sys.setlocale("LC_TIME", "English")
    X0_40cm <- read_excel("C:/Users/Connie/Desktop/LAI/Wheat_2017-2018.xlsx")
    View(X0_40cm)
    ggplot(X0_40cm, aes(Date,LAI,group=1))+
      geom_point(data=subset(X0_40cm, Condition=="Measured"),col="red")+
      geom_line(data=subset(X0_40cm, Condition=="Simulated"),col="black")+
      theme(legend.position=c(0.85,0.80))+
      scale_y_continuous(limits = c(0,3)) +
      labs(title="Winter wheat of I plot",y="LAI",x="Date")+
      theme_update(plot.title=element_text(hjust=0.5))

只有在颜色美学上映射变量时才会绘制自动图例。 在您的情况下,根据颜色映射条件并手动设置颜色。 尝试这个:

    ggplot(mapping = aes(Date, LAI, color = Condition, linetype = Condition, shape = Condition))+
  geom_point(data=subset(X0_40cm, Condition=="Measured"))+
  geom_line(data=subset(X0_40cm, Condition=="Simulated"))+
  scale_color_manual(values = c("red", "black")) +
  scale_linetype_manual(values=c(NA,1)) +
  scale_shape_manual(values=c(16,NA)) +
  theme(legend.position=c(0.85,0.80))+
  scale_y_continuous(limits = c(0,3)) +
  labs(title="Winter wheat of I plot",y="LAI",x="Date")
  theme_update(plot.title=element_text(hjust=0.5))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM