0

I'm making a plot with 2 y-axis and it's all alright except that I can't have my legend in my plot,

I tried commands :

  • scale_color_hue
  • scale_color_manuel

And still nothing,

Here is my code :

ggplot() + 
  geom_bar(mapping = aes(x = meteo$Date, y = meteo$Moyenne.de.Tre.sol.à....10.cm), stat = "identity", fill = "gray") +
  geom_line(mapping = aes(x = flux$Date, y = flux$Moyenne.de.CO2GPP_flux), lty=2 , size = 0.2, color = "green") +
  geom_line(mapping = aes(x = flux$Date, y = flux$Moyenne.de.CO2Reco_flux), lty=2 , size = 0.2, color = "yellow") +
  geom_line(mapping = aes(x = flux$Date, y = flux$Moyenne.de.CO2NEE_flux), size = 0.5, color = "blue") +
  #geom_errorbar(width=.1, aes(ymin=flux$Moyenne.de.CO2NEE_flux- var(flux$Moyenne.de.CO2NEE_flux) , ymax=flux$Moyenne.de.CO2NEE_flux+ var(flux$Moyenne.de.CO2NEE_flux)), colour="red")
  geom_point(mapping = aes(x = flux$Date, y = flux$Moyenne.de.CO2NEE_flux) , pch =16 ,size = 1, color = "blue") +
  scale_x_date(name = "") +
  scale_y_continuous(name = "Température du sol", 
                     sec.axis = sec_axis(~./(abs(max(meteo$Moyenne.de.Tre.sol.à....10.cm))/abs(max(flux$Moyenne.de.CO2GPP_flux))), name = "CO2 Flux")) +
  scale_color_hue(breaks=c("OJ", "VC"),labels=c("Orange juice", "Ascorbic acid"),l=40) +
  theme(axis.title.y = element_text(color = "black"),
        axis.title.y.right = element_text(color = "black"),
        legend.text = element_text(size = 8, colour = "red"),
        legend.title=element_blank(),legend.position = c(1,0))

Thank you for your help and have a good day :D

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    Can you add some data to make your example reproducible? – Col Bates - collynomial May 05 '21 at 12:57
  • 1
    Can't really fix it without data, but legends and such typically come from aesthetics such as `colour=`, `size=`, `group=`, etc. Compounding that is the fact that your data is not in the recommended *long* format, see https://stackoverflow.com/q/3777174/3358272. – r2evans May 05 '21 at 13:02
  • Thank's @r2evans , that was it ! i should have add : colour = "label" – Moha med May 05 '21 at 13:16

0 Answers0