0

In a ggplot I struckling with legends. I have this code:

  l<-factor(c(legend_1, legend_2,legend_3,legend_4,legend_5,legend_6,legend_7,legend_8),
levels=c(legend_1, legend_2,legend_3,legend_4,legend_5,legend_6,legend_7,legend_8))

ggplot(df_selected, aes(x=sec)) + 
  geom_line(aes(y=M1),color='orange', size=1)+ 
  geom_line(aes(y=M2),color='red', size=1) +
  geom_line(aes(y=M3),color='forestgreen', size=1) + 
  geom_line(aes(y=M4),color='black', size=1) + 
  geom_line(aes(y=(PAR1/f_CH4)+minCH4),color='orange',  size=1,linetype="dashed") + 
  geom_line(aes(y=(PAR2/f_CH4)+minCH4),color='red', size=1,linetype="dashed") + 
  geom_line(aes(y=(PAR3/f_CH4)+minCH4),color='forestgreen', size=1,linetype="dashed") + 
  geom_line(aes(y=(PAR4/f_CH4)+minCH4),color='black', size=1,linetype="dashed") + 
  scale_fill_discrete(breaks=l) +
  theme_gray() + 
  labs(title = paste(as.character(plot),"CH4"), y = 'ppm', x= 'Tid (s)', color = '') + 
  theme(plot.title = element_text(hjust = 0.5)) + 
  
   scale_y_continuous(
    "CH4  (ppm)", limits=c(minCH4,maxCH4),
    sec.axis = sec_axis(~ (. -minCH4)*f_CH4, name = "PAR")
  )

ggplot

I expected scale_fill_discrete(breaks=l) to put in the legends. What am I doing wrong?

Limey
  • 10,234
  • 2
  • 12
  • 32
  • 2
    Hi, in short, you need to include `color` inside the `aes()` function. A little more details, use `geom_*(aes(color = ...))` to tell ggplot which variable has to be represented with colors. Then use `scale_color_*()` to change the colors (i.e. blue, green, orange...) and the displayed labels in the legend. – Paul Jun 29 '21 at 10:58
  • Maybe this answers your question: [Add legend to ggplot2 line plot](https://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot) – stefan Jun 29 '21 at 11:06

1 Answers1

0

Here is an example to show some tips about how to use aes() and scale_*_*(). If you need more help on your plot, please provide a reproducible example

library(ggplot2)
# usually I set here aes that are used by all geoms i.e. color is used by both geom lines
ggplot(data = mtcars, aes(x = hp, color = as.factor(gear))) +
# if I want to set specific parameter for a geom, I use aes() inside the geom, i.e. y = drat and y = wt. Note that linetype is also inside the aes().
  geom_line(aes(y = drat, linetype = "drat")) +
  geom_line(aes(y = wt, linetype = "wt")) +
# use scales to set how the geoms will appear along with the legend.
  scale_color_manual(name = "Gear",
                       values = c("3" = "orange",
                                  "4" = "blue",
                                  "5" = "red")) +
# note how the scale for linetype is related to aes(linetype = ...) in the previous geom lines
  scale_linetype_manual(name = "Vars",
                        values = c("drat" = "solid", "wt" = "longdash"))

Created on 2021-06-29 by the reprex package (v2.0.0)

Paul
  • 2,850
  • 1
  • 12
  • 37
  • I continued the question in [link](https://stackoverflow.com/questions/68440404/getting-linetype-dashed-and-color-in-the-legent) – user3357510 Jul 19 '21 at 12:29
  • @user3357510 seems like you got an answer ^^ understanding how to set aestetics is key to work with ggplot. – Paul Jul 20 '21 at 06:01