Update: The major difference which is non-intuitive to me compared to the question answered in Order Bars in ggplot2 bar graph is re-ordering each individual panel's type separately according the the median calculated by ggplot2, while preserving the colour scheme.
In the following dataset, different treatments may show different arrangements of types along the x-axis when their median values are calculated.
I am trying to produce a plot showing how the median values of the types swap positions when arranged in ascending median values
To begin, I am plotting all the different treatments into separate panels with boxplots, and furthermore I am letting ggplot2 assign the colour palette for the types.
This will produce a mock-up of the dataset I have.
df = data.frame(
type = rep(c
(
"piece",
"work",
"man",
"noble",
"reason",
"infinite",
"faculty",
"form",
"moving",
"express",
"admirable",
"action",
"apprehension",
"god",
"beauty",
"world",
"paragon",
"animals"
), 52),
treatment = sample(x=c("alpha", "beta", "charlie", "delta"), 234, replace = T),
value = abs(rnorm(936, 0.5, 1))
)
And this code will generate the graph that I am nearly looking for
ggplot(
data=df,
aes(x = type, y = value, fill = type)) +
geom_boxplot() +
xlab("Soliloquy of Man") +
ylab("Value")+
facet_wrap(~treatment, ncol = 2) +
theme(axis.text.x = element_blank(),
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA))
What I would like to do is to
- reorder all the traits in each individual panel so that the median values increase along the x-axis,
- using the same colour palette for each specific type,
- for the first panel in the top left hand corner, fix the colour to each type such that it looks like a smooth transition from one end of the colour spectrum to the other.
This will allow me to check and show visually whether the types have swapped positions in reference to the first panel, following which I shall run a non-parametric rank test.