7

I'm trying to shade the area under two curves; I want to get exactly the same plots (without thresholds though) as in previous post, with the only difference that I want to use geom_line() instead of stat_density(). Is there any way to do this? Thanks in advance.

I've tried what was suggested in that post, but it does not work when I use geom_line(). Also, I have tried something different, but this is not quite what I want, as I want to shade using different colors for different groups. Here is the initial code:

library(ggplot2)
x <- seq(0,1,0.005)
y1 <- dbeta(x,3,3)
data1<-data.frame('x'=x,'y'=y1)
data1$group<-1

y2 <- dbeta(x,10,4)
data2<-data.frame('x'=x,'y'=y2)
data2$group<-2

data<-rbind(data1, data2)
ggplot(data, aes(x=x, y=y, group=group, col=group, fill=group)) +  geom_line(size=1) +geom_ribbon(data=subset(data,x>0 &x<1),aes(x=x,ymax=y),ymin=0, fill="green4",alpha=0.3)

In case the above link doesn't work: ggplot2 shade area under density curve by group

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
Alex
  • 201
  • 1
  • 2
  • 7
  • And what is missing exactly? – jan-glx Jan 15 '18 at 17:39
  • @YAK The suggested syntax in that previous post does not work when I want to use `geom_line()`. – Alex Jan 15 '18 at 17:41
  • 1
    When I run your code I get two curves with the area beneath shaded. Do you want them shaded in different colors? then just use `color=as.factor(group)` in the second `aes(` and get rid of `fill="geen4"`... – jan-glx Jan 15 '18 at 17:45
  • OK your solution is almost there. But you override the the group specific coloring by `fill="green4"` and you a continuous (numeric) grouping variable for the color, just use `"1"` and `"2"` instead of `1` and `2`. – jan-glx Jan 15 '18 at 17:48
  • @YAK Yes, I want to be able to choose colors of the shade. But this is very close now to what I wanted. – Alex Jan 15 '18 at 17:54
  • use `scale_color_manual` – jan-glx Jan 15 '18 at 17:59
  • @YAK This +`scale_fill_manual(name='', values=c("1" = "green4", "2" = "red"))` allowed to choose shade colors! Thanks Yak for helpful answers! – Alex Jan 15 '18 at 18:12
  • Glad you found a solution - don't edit answers in to your question though - it looks like the question still needs answering. Instead, post it as an answer. – Gregor Thomas Jan 15 '18 at 18:32

2 Answers2

11

The solution provided here looks as follows:

library(ggplot2)
x <- seq(0,1,0.005)
y1 <- dbeta(x,3,3)
data1<-data.frame('x'=x,'y'=y1)
data1$group<-"1"

y2 <- dbeta(x,10,4)
data2<-data.frame('x'=x,'y'=y2)
data2$group<-"2"

data<-rbind(data1, data2)
ggplot(data, aes(x=x, y=y, group=group, fill=group)) +
  geom_line(size=.5) + 
  geom_ribbon(data=subset(data,x>0 & x<1),aes(x=x,ymax=y),ymin=0,alpha=0.3) +
  scale_fill_manual(name='', values=c("1" = "green4", "2" = "red"))

enter image description here

This is a perfectly fine solution, but I found myself writing similar code over and over, so I wrote geom_density_line(). It works like geom_density() but draws a line with a filled area instead of a polygon around a filled area. It's part of the current development version of the package ggridges:

# current development version is needed for this to work
library(ggridges) # install.github("clauswilke/ggridges")

ggplot(data, aes(x=x, y=y, group=group, fill=group)) +
  geom_density_line(stat = "identity", size=.5, alpha=0.3) +
  scale_fill_manual(name='', values=c("1" = "green4", "2" = "red"))

enter image description here

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
3

In case it might also be useful to someone else, the solution was to add scale_fill_manual() and change numeric 1 and 2 values in group variable into character values "1" and "2" respectively as follows:

Change 1 to "1" and 2 to "2" as follows:

data1$group<-"1"
data2$group<-"2"

and run:

ggplot(data, aes(x=x, y=y, group=group, fill=group)) +
geom_line(size=.5) + 
geom_ribbon(data=subset(data,x>0 & x<1),aes(x=x,ymax=y),ymin=0,alpha=0.3) +
scale_fill_manual(name='', values=c("1" = "green4", "2" = "red"))
Alex
  • 201
  • 1
  • 2
  • 7
  • This code does not work for me. Please make sure you post code that is free from typos and other errors. – Claus Wilke Jan 15 '18 at 20:23
  • 1
    @Claus Wilke typo has now been corrected, thanks for letting know (and lines rearranged so it can be run straight away, by copy and paste)! – Alex Jan 15 '18 at 20:28