3

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)

3 Answers 3

5

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 with geom_hline():

ggplot(melt_df, aes(1:100, value)) + geom_point() + 
    facet_wrap(~ variable, ncol = 3, scales = "free_y") +
    geom_hline(aes(intercept = 0), linetype = 2) +
    theme_bw(base_size = 16)

You can get equivalent results by using stat_qq() and avoid the sorting with ddply beforehand. The difference is that we only need to pass in the sample argument to aes:

ggplot(melt_df, aes(sample = value)) + geom_point(stat = "qq") + 
    facet_wrap(~ variable, ncol = 3, scales = "free_y") +
    geom_hline(aes(intercept = 0), linetype = 2) +
    theme_bw(base_size = 16)
1
  • Hrm. Adding a geom_linerange to that seems...tricky. It doesn't want to use the sample as x values, and if you just use sample, it seems to not like providing ymin and ymax in the toplevel aes. Hrm...
    – jebyrnes
    Sep 14, 2012 at 22:29
1

Step 1: The ordering is done by ddply.

melt_df<-melt(my_df)

melt_df<-ddply(melt_df, .(variable), summarize, value=sort(value))

qplot(1:100, value, data=melt_df, geom="point",facets=~variable)+theme_bw(base_size=16)

The axis part is still puzzling to me, though, particularly given the 3 facets.

1
######## Begin: base R approach ###########
#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)})
######## End: base R approach ###########

rbase

######## Begin: data.table + ggplot2 approach ###########
library(data.table)
library(ggplot2)
melt_df<-melt(my_df)
melt_dt<-melt_df
setDT(melt_dt)
setkey(melt_dt, variable, value)
catey <- ggplot(melt_dt, aes(x=rep(1:100,3), y=value)) + geom_point() + 
facet_wrap(~ variable, ncol = 3, scales = "free_y") +
 geom_hline(aes(intercept = 0), linetype = 2) +
 theme_bw(base_size = 16)
## catepillar:
catey
## with line ranges:
melt_dt[, minvalue:= -2*value+value]
melt_dt[, maxvalue:=  2*value+value]
butterfly <- catey + geom_linerange(aes(ymin=minvalue, ymax=maxvalue))
butterfly
######## End: data.table + ggplot2 approach ###########

caterpillar

butterfly

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.