Problem generating a second legend in the same plot

Hi all

I have issues generating two legends in the same plot. Down you can see the original code and I tried several ways how to implement a second legend, which will indicate my two geom_line and geom_point. Until now I was just able to generate a legend with the arrow color. With some ways I think I generated a seperate legend, but it did not show up even I did not receive an error message.
Could you help me please generate the second legend? I would be very thankful.

ggplot(data = dat, aes(x = Tag)) +
  geom_segment(data = dat,
               size = 0.75,
               aes(x = Tag,
                   xend = x.end,
                   y = 30,
                   yend = y.end,
                   color = Windgeschwindigkeit),
               arrow = arrow(length = unit(0.15, "cm"))) +
  scale_colour_gradient(low="green", high="red") +
  ylim(-5, 100) +
  ggtitle("Meteorologische Umstände") +
  ylab("Temperatur °C, Feuchtigkeit in %")+
  geom_line(aes(y = Temp), color = "red", size = 1)+
  geom_point(aes(y=Regen), color = "blue")+
  geom_line(aes(y=Feucht), size = 1)+

Thanks a lot and stay safe!

ggplot2 isn't designed to map two different variables to the same aesthetic (for example, mapping two different variables to color). However, there are two (experimental) ggplot2 extension packages that allow you to create multiple colour and fill scales. They are relayer and ggnewscale.

1 Like

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

A man of honor :slight_smile: With the extension package ggnewscale it worked as I wanted. Thank you a lot for the help! Have a nice sunday