0

I am trying to make a plot using geom_smooth with different color patterns. However, I am getting two legends one with blue color (solid and dashed lines) and one with two different colors (solid lines). I want the dashed and solid lines to be different colors. How can I fix this error? Here is the code and output.

df<-data.frame(y=rnorm(200,30,20),x=rnorm(200,20,10),treat=factor(rbinom(100,1,0.5)))
p2 <- ggplot(df, aes(x=x, y=y, group=treat, color = treat))+
      geom_point(size = 2)+
      geom_smooth(aes(linetype=treat), se = F)+
      labs(x ="tf", y = "xm") +
      theme_bw() +
      scale_linetype_manual(values=c( "solid", "dashed"),
                    labels = c("3", "4"))+
      scale_color_manual(values = c("#00AFBB", "#E7B800"))
 p2

enter image description here

user12582271
  • 91
  • 11
  • 1
    Just remove the `labels` argument in `scale_linetype_manual()`. – Phil Jan 06 '20 at 03:48
  • You set the labels 3 & 4 on one scale, but have labels 0 & 1 on the other. You'll need to give your code some logic for combining these; how would 3 and 0 get combined? – camille Jan 06 '20 at 04:05
  • Thank you @ phil and camille. I would like to re-name the legends to 3, 4. If I remove this the output is showing as 0 and 1. I want to keep dashed and dotted lines as colors and be renamed as 3 and 4. – user12582271 Jan 06 '20 at 04:17
  • 2
    Then add `labels = c("3", "4")` to `scale_color_manual()`. – Phil Jan 06 '20 at 04:18
  • Thank you Phil. Here is the code line: scale_color_manual(values = c("#00AFBB", "#E7B800"), labels = c("3", "4")). I am not getting the expected output. It is showing 2 legends – user12582271 Jan 06 '20 at 04:22
  • 1
    That's odd. [This is what it returned to me](https://i.imgur.com/BzRznLl.png). What version of `ggplot2` are you using? – Phil Jan 06 '20 at 04:25
  • The version is 3.2.1 @ Phil – user12582271 Jan 06 '20 at 04:28
  • 2
    And your last 2 lines are `scale_linetype_manual(values=c( "solid", "dashed"), labels = c("3", "4")) + scale_color_manual(values = c("#00AFBB", "#E7B800"), labels = c("3", "4"))`? – Phil Jan 06 '20 at 04:30
  • p2 <- ggplot(df, aes(x=x, y=y, group=treat, color = treat))+ geom_point(size = 2)+ geom_smooth(aes(linetype=treat), se = F)+ labs(x ="tf", y = "xm") + theme_bw() + scale_linetype_manual(values=c( "solid", "dashed"))+ scale_color_manual(values = c("#00AFBB", "#E7B800"), labels = c("3", "4")) p2 – user12582271 Jan 06 '20 at 04:32
  • 1
    The `labels` argument need to be in both `scale_*` calls. – Phil Jan 06 '20 at 04:33
  • Thank you so much @Phil. Another question, I have a data set which contains different types of treatments (a,b,c,d) and their growth at different time points (1d, 2d, 3d) and at different concentrations (1,2,3,4). The growth values contain 3 replications each. I want to plot mean + se. My first guess I have to use dpylr package to calculate the mean and se. Can I use same code to plot the data, but would like to include mean and se. Can I use this code: geom_errorbar(aes(ymin=mean - se, ymax=mean + se) plus with the above code to plot the data? – user12582271 Jan 06 '20 at 04:46
  • If you have a different question, you should post it as a separate question – camille Jan 06 '20 at 05:11
  • Thank you camille. I will post it as a separate question. By the way "the question how to merge color, line, style and shape legends in ggplot" did not solve the problem. But thanks to Phil who provided the solution to my question – user12582271 Jan 06 '20 at 05:18

0 Answers0