Reputation: 9332
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)
Upvotes: 3
Views: 3765
Reputation: 2738
######## 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 ###########
######## 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 ###########
Upvotes: 1
Reputation: 69151
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)
Upvotes: 5
Reputation: 9332
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.
Upvotes: 1