I’m working on a project where I have multiple response variables and want to show ordered caterpillar plots. This is fairly straightforward with the base R plotting capabilities
#generate random data frame
set.seed(42)
my_df<-data.frame(x=rnorm(100), y=runif(100,-2,2), z=rpois(100, 10))
#3 panels of plots
par(mfrow=c(1,3))
#note the abline to show an axis at y=0
sapply(c("x", "y", "z"), function(i){ plot(sort(my_df[[i]])); abline(0,0)})
But I am at a loss as to how to do this with ggplot2. To put together the three panels, I know I have to melt the data frame, but then…how does one do the ordering by variable and plotting with a 0 axis later? All I have so far is…
melt_df<-melt(my_df)
qplot(1:100, value, data=melt_df, geom="point",facets=~variable)+theme_bw(base_size=16)
Building off jebyrnes answer – you can set the y-axis to have free scales as an argument in
facet_wrap. We can add a horizontal line withgeom_hline():You can get equivalent results by using
stat_qq()and avoid the sorting withddplybeforehand. The difference is that we only need to pass in thesampleargument toaes: