rggplot2ggridgesridgeline-plot

Combining geom_vridgeline with geom_line in one plot


I would like to combine geom_vridgeline with a (connected) line chart.

The values for geom_line (and geom_point) come from a different dataset. I.e. it is not a moment (mean, median etc.) of the observations used in geom_vridgeline.

I tried the simple thing geom_vridgeline(...) + geom_line(data...)+geom_point(..) as well as several tweaks (adding second x-axis etc).

library(tidyverse)
library(ggridges)

#The original chart
ggplot(iris, aes(x=Species, y=Sepal.Width, width = ..density..,     fill=Species)) +
geom_vridgeline(stat="ydensity", trim=FALSE, alpha = 0.85, scale = 0.25)

# another dataset
df<-iris%>%
group_by(Species)%>%
summarise(newvar=mean(Sepal.Width))

# add line from new dataset (doesn't work)
ggplot(iris, aes(x=Species, y=Sepal.Width, width = ..density..,     fill=Species)) +
geom_vridgeline(stat="ydensity", trim=FALSE, alpha = 0.85, scale = 0.25)+
geom_line(data=df,aes(x=factor(Species),y=newvar))

Something like this

enter image description here

https://www.dropbox.com/s/1v7ycl23014hau8/Rplot.png?dl=1

NOTE: In my real example the x axis is actually continuous. I assume that once I've figured out how to add geom_line I can also add geom_point

Thanks a lot!


Solution

  •  ggplot() + 
        geom_line(data=df,mapping=aes(x=Species,y=newvar,group = 1)) +
        geom_vridgeline(data=iris, aes(x=Species, y=Sepal.Width, width = ..density..,     fill=Species),stat="ydensity", trim=FALSE, alpha = 0.85, scale = 0.25)