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)
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?
To reorder the facets accordingly of the given ggplot2 plot, the user needs to reorder the levels of our grouping variable accordingly with the help of the levels function and required parameter passed into it, further it will lead to the reordering of the facets accordingly in the R programming language.
The facet_grid() function will produce a grid of plots for each combination of variables that you specify, even if some plots are empty. The facet_wrap() function will only produce plots for the combinations of variables that have values, which means it won't produce any empty plots.
facet_grid() forms a matrix of panels defined by row and column faceting variables. It is most useful when you have two discrete variables, and all combinations of the variables exist in the data.
The facet approach partitions a plot into a matrix of panels. Each panel shows a different subset of the data. This R tutorial describes how to split a graph using ggplot2 package. There are two main functions for faceting : facet_grid()
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With