0

Using ggplot2, I have produced a plot with 3 lines, colour coded for 3 experimental conditions. I have had to detail these outside of ggplot(aes()) but in stead within geom_line(aes()) such that I could collate the data from their respective, separate dataframes. As such, ggplot has not produced a legend. Code for the plot is here:

ggplot()+
  geom_line(data = linedatac1df, aes(x=time, y=c1y), size=1.2, colour=rgb(0.6,0.9,0.9))+
  geom_line(data = linedatac2df, aes(x=time, y=c2y), size=1.2, colour=rgb(0,0.5,0.5))+
  geom_line(data = linedatacidf, aes(x=time, y=Iy), size=1.2, colour=rgb(0.9,0.3,0.4))+
  theme_light()+
  scale_y_continuous(breaks = c(-2e-23, -1e-23, 0, 1e-23, 2e-23, 3e-23))+
  scale_x_continuous(breaks = c(-250, 0, 250, 500, 750, 1000, 1250, 1500))+
  ggtitle("Changes in Mean Theta Power Across Conditions")+
  theme(plot.title = element_text(hjust = 0.5))+
  xlab("Time (ms)")+
  ylab("Mean theta power ( mV^2)")+
  theme(panel.background = element_rect(colour = "black", size=2))+
  geom_vline(xintercept = -100, linetype="dashed",
             colour="black", size=1)+
  geom_vline(xintercept = 0, linetype="dashed",
             colour="black", size=1)+
  geom_vline(xintercept = -350, linetype="dotted",
             colour="black", size=1)+
  geom_vline(xintercept = -150, linetype="dotted",
             colour="black",size=1)+
  geom_vline(xintercept = 400, linetype="dotted",
             colour="black",size=1)+
  geom_vline(xintercept = 600, linetype="dotted",
             colour = "black",size=1)

I attempted to manually add a legend using the following code, but it did not work:

+
  scale_colour_manual(name="Key",,
                      breaks = c("Single control", "Double control", "Inhibition"),
                      values = c("Single control"=rgb(0.6,0.9,0.9), "Double control"=rgb(0,0.5,0.5), "Inhibition"=rgb(0.9,0.3,0.4)))

Any help on this would be much appreciated!

camille
  • 16,432
  • 18
  • 38
  • 60
  • You should really find a way to combine the data frames and/or get them in the shape that ggplot expects rather than calling the same geoms over and over, especially with intercepts hard-coded like this. Some ggplot tutorials such as the ones linked on the package website would be good places to start. Beyond that, see [this r-faq post](https://stackoverflow.com/q/10349206/5325862) and the many ones linked to it in the sidebar – camille Jan 10 '23 at 14:56

0 Answers0