Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

r - Separate ordering in ggplot facets

so I have a simple example--a fully crossed three treatmentthree context experiment, where a continuous effect was measured for each treatmentcontext pair. I want to order each treatment by effect, separately according for each context, but I'm stuck on ggplot's faceting.

here's my data

df <- data.frame(treatment = rep(letters[1:3], times = 3),
                 context = rep(LETTERS[1:3], each = 3),
                 effect = runif(9,0,1))

and I can get something very close if I collapse treatment and context into a single 9 point scale, as such:

df$treat.con <- paste(df$treatment,df$context, sep = ".")
df$treat.con <- reorder(df$treat.con, -df$effect, )

ggplot(df, aes(x = treat.con, y = effect)) +
           geom_point() +
           facet_wrap(~context, 
                      scales="free_x",
                      ncol = 1)

very close to what i want

except to achieve the separate ordering in each facet, the new x variable I created is potentially misleading, since it doesn't demonstrate that we've used the same treatment in all three contexts.

Is this solved via some manipulation of the underlying factor, or is there a ggplot command for this situation?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Faceting isn't really the right tool for what you want to do, since it's really designed for situations with shared scales.

It might make more sense to make each plot separately and then arrange them each using grid.arrange from the gridExtra package. (Be warned, the following code might seem a bit inscrutable if you're not familiar with these tools!)

#I use stringsAsFactors simply to ensure factors on
# my system.
df <- data.frame(treatment = rep(letters[1:3], times = 3),
                 context = rep(LETTERS[1:3], each = 3),
                 effect = runif(9,0,1),stringsAsFactors = TRUE)

require(gridExtra)
#One "master" plot (to rule them all)
p <- ggplot(df,aes(x = treatment,y = effect)) + 
        geom_point() + 
        facet_wrap(~context)

#Split data set into three pieces    
df_list <- split(df,df$context)
#...and reorder the treatment variable of each one
df_list <- lapply(df_list,function(x){x$treatment <- reorder(x$treatment,-x$effect); x})

#"Re-do" the plot p using each of our three smaller data sets
# This is the line that might be the most mysterious            
p_list <- lapply(df_list,function(dat,plot){plot %+% dat},plot = p)

#Finally, place all three plots on a single plot
do.call(grid.arrange,p_list)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...