Note that there are some explanatory texts on larger screens.

plurals
  1. POggplot2 draw dashed lines of same colour as solid lines belonging to different groups
    text
    copied!<p>I am trying to plot 2 solid lines in 2 different colours for each group, but also add dashed lines of the same colour around those lines, then add a legend. For some reason I am having trouble using "dashed" or "dotted", it seems as I am plotting over the dashed lines twice. I am also not getting the legend right, I get the error <code>Adding another scale for 'colour', which will replace the existing scale</code>.</p> <p>Can you please help me figure out what I am doing wrong? Here is an example dataset and what I have tried: </p> <pre><code>x &lt;- c(10, 20, 50, 10, 20, 50) mean = c(52.4, 98.2, 97.9, 74.1, 98.1, 97.6) group = c(1, 1, 1, 2,2,2) upper = c(13.64, 89, 86.4, 13.64, 89, 86.4) lower = c(95.4, 99.8, 99.7, 95.4, 99.8, 99.7) data &lt;- data.frame(x=x,y=mean, group, upper, lower) ggplot(data, aes(x = x, y= mean, group = as.factor(data$group), colour=as.factor(data$group))) + geom_line() + geom_point() + geom_line(data=data,aes(x=x, y=lower, group = as.factor(data$group), colour=as.factor(data$group), linetype="dotted")) + geom_line(data=data,aes(x=x, y=upper, group = as.factor(data$group), colour=as.factor(data$group), linetype="dotted")) + scale_color_manual(values=c("red", "blue")) + scale_colour_discrete(name="Groups") </code></pre> <p>I have also tried with <code>geom_ribbon</code>, again with no luck for the grouping part…</p> <pre><code>ggplot(data, aes(x = x, y= mean, group = group)) + geom_line() + geom_ribbon(aes(ymin = lower, ymax = upper)) + geom_line(aes(y = mean), colour = "Mean")) + scale_colour_manual(name = "", values = c("Group1", "Group2")) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload