Legend with geom_line and geom_ribbon

I create a figure in which I have a line and trust groups around it. For this, I use both geom_line and geom_ribbon in ggplot2 in R. The problem is how to deal with the legend. Slashes are added in it, and after a long search, I understand that this is a common problem. Most of the solutions I found are for barriers (e.g. ggplot cookbook). I also found solutions to suppress it, but that did not work for me.

Below I have three plots showing the problem. Firstly, when I am just building a line, it looks good. Then, when I add the ribbon portion of the graph, slashes are added. The third plot is how I want it to look (a dash, obviously). How to achieve this?

enter image description here

Edit: To be clear, I want this to be in the following figure (which I fixed with MS Paint):

enter image description here

 library(ggplot2) library(gridExtra) set.seed(1) y <- sin(seq(1, 2*pi, length.out = 100)) x <- 1:100 plotdata <- data.frame(x=x, y=y, lower = (y+runif(100, -1, -0.5)), upper = (y+runif(100, 0.5, 1))) h1 <- ggplot(plotdata) + geom_line(aes(y=y, x=x, colour = "sin")) h2 <- h1 + geom_ribbon(aes(ymin=lower, ymax=upper, x=x, colour = "bands"), alpha = 0.3) h3 <- h2 + scale_colour_manual(name='', values=c("bands" = "grey", "sin" = "blue")) grid.arrange(h1, h2, h3) 
+7
r plot ggplot2
source share
3 answers

You can make separate legends - one for the color of the line and one for filling the ribbon. Then with scale_... set the names for the legend empty. Only problem is that legend keys will be split.

 ggplot(plotdata) + geom_line(aes(y=y, x=x, colour = "sin"))+ geom_ribbon(aes(ymin=lower, ymax=upper, x=x, fill = "band"), alpha = 0.3)+ scale_colour_manual("",values="blue")+ scale_fill_manual("",values="grey12") 

enter image description here

+5
source share

If you want to show the color in the legend, you probably have to resort to adding a few extra geom_line s. Do not use color in geom_ribbon , and then add the lines upper and lower .

 h2 <- h1 + geom_ribbon(aes(ymin=lower, ymax=upper, x=x), alpha = 0.3) + geom_line(aes(x=x, y = lower, color = "bands")) + geom_line(aes(x=x, y = upper, color = "bands")) 

image

EDIT: you can optionally use the fill scale.

 h1 <- ggplot(plotdata) + geom_line(aes(y=y, x=x, colour = "sin", fill="sin")) h2 <- h1 + geom_ribbon(aes(ymin=lower, ymax=upper, x=x, fill="bands"), alpha = 0.3) + geom_line(aes(x=x, y = lower, color = "bands")) + geom_line(aes(x=x, y = upper, color = "bands")) h3 <- h2 + scale_colour_manual(name='', values=c("bands" = "grey", "sin" = "blue")) + scale_fill_manual(name = '', values=c("bands" = "grey12", "sin" = "grey")) grid.arrange(h1, h2, h3) 

image2

You can also set "bands" = "transparent" in the color instruction if you don't need a (barely visible) gray line.

+2
source share

Try to keep the color outside of aes , then it will not be displayed according to legend:

 h1 + geom_ribbon(aes(ymin=lower, ymax=upper, x=x), colour = "red", alpha = 0.3) 

enter image description here

+1
source share

All Articles