Two legends instead of just one

Good Day Everyone

I have issue that ggplot2 is creating two different legends but for the same variables (See attached image).
Does someone have an idea how to merge these two legends into one? That in the end it shows the different linetypes and colors in the same plot. I think is the problem that I am combining lines with points and therefore it has issues merging them together. Because I plotted it once without the blue points and then it worked perfectly fine with the two different linetypes and colors in the same legend.
You can find the code attached. Thank you very much in advance!

ggplot(data = dat, aes(x = Tag)) +
  ggtitle("Meteorologische Bedingungen") +
  labs(x = "Zeit", caption = "Data Source: MeteoSchweiz", linetype = "Variablen")+
  geom_line(aes(y = Temp, color = "Temperatur", linetype="Temperatur"), size = 1)+
  geom_point(aes(y=Regen, color = "Regen"))+
  geom_line(aes(y=Feucht*b, color = "Luftfeuchtigkeit", linetype = "Luftfeuchtigkeit"), size = 1)+
  scale_y_continuous("Temperatur [°C]", sec.axis = sec_axis(~ (. - a)/b, name = "Luftfeuchtigkeit (relativ) [%]")) +
  scale_color_manual(name = "Variablen",
                     breaks = c("Temperatur", "Luftfeuchtigkeit", "Regen"),
                     values = c("Temperatur" = "#B2182B", "Luftfeuchtigkeit" = "grey44", "Regen"="blue"))+
  scale_linetype_manual(breaks = c("Temperatur", "Luftfeuchtigkeit"), 
                        values = c("Temperatur" = "solid", "Luftfeuchtigkeit" = "dashed"))+
    plot.title = element_text(color = "black", size = 15, face = "bold",hjust = 0),
    plot.subtitle = element_text(color = "black",size = 12),
    plot.caption = element_text(color = "black", face = "italic", hjust= 1.1, vjust = 20))

I guess Temperatur, Regen and Luftfeuchtigkeit are all variables in you data frame dat? If this is the case, then this problem results in quoted variable names inside aes(). You could try

# excluding theme options
ggplot(data = dat, aes(x = Tag)) + 
geom_line(aes(y = Temp, color =  Temperatur, linetype= Temperatur), size = 1)+
  geom_point(aes(y=Regen, color = Regen))+
  geom_line(aes(y=Feucht*b, color = Luftfeuchtigkeit, linetype = Luftfeuchtigkeit), size = 1)

In most cases, ggplot2 will combine legends of different aesthetics whenever possible.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.