簡體   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