Reputation: 11
sorry for the question, I am still new in this. I found a nice code in the internet which shades areas between graphs with polygon (I cannot use geom_ribbon). I have to rewrite it into the ggplot format, but I dont know which parameters I have to plug in.
#sample code
x1 <- seq(-3,3,0.01)
y1 <- dnorm(x,0,1)
y2 <- 0.5*dnorm(x,0,1)
plot(x1,y1,type="l",bty="L")
polygon(c(x1,rev(x1)),c(y2,rev(y1)),col="skyblue")
#I need the code in this format
d=data.frame(x1=x1, y1=y1, y2=y2)
ggplot(data= d) +
geom_line(aes(x=x1,y=y1))+
geom_line(aes(x=x1,y=y2))+
geom_polygon(?????)
Does anyone know a solution? Thank you very much in advance for your help.
Upvotes: 1
Views: 363
Reputation: 24272
Here is a solution based on the use of a second data frame (d2
):
x1 <- seq(-3,3,0.01)
y1 <- dnorm(x1,0,1)
y2 <- 0.5*dnorm(x1,0,1)
d1 <- data.frame(x1=x1, y1=y1, y2=y2)
idx <- order(x1)
d2 <- data.frame(x=c(x1[idx],x1[rev(idx)]),
y=c(y1[idx],y2[rev(idx)]))
ggplot(data=d1) +
geom_polygon(data=d2, aes(x=x, y=y), fill="skyblue", color="black") +
theme_bw()
Upvotes: 1