Как добавить легенду в линию ggplot2 с точечным сюжетом? - PullRequest
1 голос
/ 06 марта 2020

Я пробовал много кодов, но всегда не могу добавить легенду для графика. Я хочу добавить легенду для измеренной красной точки и смоделированной черной линии На графике все еще отсутствует легенда с кодом ниже

    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))

1 Ответ

0 голосов
/ 06 марта 2020

Легенда c автоматически рисуется, только если вы сопоставите переменную с цветом эстетики c. В вашем случае карта Условие на цвет и установить цвета вручную. Попробуйте это:

    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))
...