0

I want to add legend to my plot using the ggplot2 package,

below is the code I tried.

all_plot <- ggplot(rawData, aes(x=c(1:670))) +
  geom_line(aes(y = DO1), color = 'DO1') +
  geom_line(aes(y = DO2), color = 'DO2') +
  geom_line(aes(y = Controlling.DO), color = 'Controlling.DO')+
  ggtitle("Plot of ControllinDO changing in between DO1 and DO2", )+ 
  labs(x="No of Observations", y="DO(Dissolved Oxygen)")+
  scale_color_manual(
    name ='title',
    breaks = c('DO1','DO2','Controlling.DO'),
    values = c('DO1'='darkblue','DO2'='red','Controlling.DO'='green')
  )

  
all_plot

This is the error I'm getting

#Error in `geom_line()`:
#! Problem while converting geom to grob.
#ℹ Error occurred in the 1st layer.
#Caused by error:
stefan
  • 90,330
  • 6
  • 25
  • 51
highclef
  • 169
  • 7
  • Please upload the input data (i.e., rawData in your code). – Ssong Nov 10 '22 at 06:10
  • To add your legend see [Add legend to ggplot2 line plot](https://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot), i.e. move `color='...'`into ´aes()` or reshape your data. – stefan Nov 10 '22 at 06:13
  • Concerning your error. At a first glance your code looks fine. But to have a look we need [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data. Type `dput(head(rawData, 20))` for the first 20 rows of data into the console and copy the output into your post. – stefan Nov 10 '22 at 06:17

0 Answers0