0

I'm struggling with plotting line graph using ggplot. I managed to make the graph, but when I'm trying to add legend manually, using

scale_color_manual i don't know somehow it does not show up to the plot. Here is the code:

windows()
ggplot(ts_data, aes(x)) +  
  geom_line(aes(y = y1), color = "#B22222",lwd=1.1) +
  geom_line(aes(y = y2), color = "#FF8C00",lwd=1.1) +
  geom_line(aes(y = y3), color = "#008B00",lwd=1.1) +
  geom_line(aes(y = y4), color = "#FF00FF",lwd=1.1) +
  geom_line(aes(y = y5), color = "#4876FF",lwd=1.1)+
  geom_line(aes(y = y6), color = "black",linetype="dashed",lwd=1.1)+
  geom_line(aes(y = y7), color = "#8B8682",linetype="dashed",lwd=1.1)+
  ggtitle("Monthly Sum Average Precipitation 1976-2005") +
  scale_x_continuous(NULL,breaks=seq(1,12,1),
                     limits = c(1,12), labels = my_labels)+
  scale_y_continuous(name="Precipitation [mm/month]",breaks=seq(0,100,10),
                     limits =c(0,100))+
  theme_minimal() +
  theme(plot.title = element_text(color="black", size=12, face="bold",hjust=0.5),
        axis.title.y = element_text(color="black", size=9, face="bold"),
        axis.text = element_text(color="black", face="bold"))+
  scale_color_manual(name=NULL,breaks=c("CNRMr1-ALADIN63","ECEARTHr12-CCLM","HadGEMr1-HIRHAM",
                                        "MPIr1-RACMO","NorESMr1-REMO","CRU","E-OBS"),
                     values=c("CNRMr1-ALADIN63"="#B22222",
                              "ECEARTHr12-CCLM"="#FF8C00",
                              "HadGEMr1-HIRHAM"="#008B00",
                              "MPIr1-RACMO"="#FF00FF",
                              "NorESMr1-REMO"="#4876FF",
                              "CRU"="black",
                              "E-OBS"="#8B8682"))

If anyone can help me , i will be really appreciate it. Thanks in advance.

  • If you want to have a legend you have to map on aesthetics, i.e. do e.g. `geom_line(aes(y = y1, color = "CNRMr1-ALADIN63"), lwd=1.1)` for the first geom_line. Only this way will you get a legend. `scale_color_manual` is only used to set the colors. – stefan Feb 06 '23 at 13:26

0 Answers0