Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 draw dashed lines of same colour as solid lines belonging to different groups

Tags:

r

ggplot2

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"))
like image 223
user2337032 Avatar asked May 01 '13 14:05

user2337032


People also ask

How to add dotted lines in ggplot2?

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().

How to create a dashed horizontal line in a ggplot2 graph in R?

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.

How to plot fitted lines in ggplot2 using 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.

How do you plot different slopes in ggplot2?

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.


Video Answer


1 Answers

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)))

enter image description here

like image 77
Didzis Elferts Avatar answered Oct 24 '22 16:10

Didzis Elferts