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 Adding another scale for 'colour', which will replace the existing scale
.
Can you please help me figure out what I am doing wrong? Here is an example dataset and what I have tried:
x <- 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 <- 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")
I have also tried with geom_ribbon
, again with no luck for the grouping part…
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"))
1 Answer 1. To add dotted lines you should add 2 geom_line() call where you provide y values inside aes(). There is no need to put data= and groups= arguments as they are the same as in ggplot() call. linetype="dotted" should be placed outside aes() call. For the colors you need only one scale_color_manual().
To create a dashed horizontal line in a ggplot2 graph in R, we can follow the below steps − First of all, create a data frame. Then, create a plot using ggplot2.
The geom_smooth() function in ggplot2 can plot fitted lines from models with a simple structure. Supported model types include models fit with lm(), glm(), nls(), and mgcv::gam(). Fitted lines can vary by groups if a factor variable is mapped to an aesthetic like color or group.
Plotting separate slopes with geom_smooth () The geom_smooth () function in ggplot2 can plot fitted lines from models with a simple structure. Supported model types include models fit with lm (), glm (), nls (), and mgcv::gam (). Fitted lines can vary by groups if a factor variable is mapped to an aesthetic like color or group.
To add dotted lines you should add 2 geom_line()
call where you provide y values inside aes()
. There is no need to put data=
and groups=
arguments as they are the same as in ggplot()
call. linetype="dotted"
should be placed outside aes()
call. For the colors you need only one scale_color_manual()
. To remove dotted line pattern from legend you can override aesthetic using functions guides()
and guide_legend()
.
ggplot(data, aes(x = x, y= mean, group = as.factor(data$group),
colour=as.factor(data$group))) +
geom_line() + geom_point() +
geom_line(aes(y=lower),linetype="dotted") +
geom_line(aes(y=upper),linetype="dotted")+
scale_color_manual(name="Groups",values=c("red", "blue"))+
guides(colour = guide_legend(override.aes = list(linetype = 1)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With