I'm trying to use reorder
in a facet-wrapped plot that also uses scales = free_x
in ggplot2, but the reorder function isn't reordering the x-axis properly. Here's what I'm running:
library(ggplot2)
df <- read.table("speaking_distribution_by_play.txt",
header = F,
sep = "\t")
ggplot(df, aes(x=reorder(V2, V3), y=V3)) +
geom_bar(stat = "identity") +
facet_wrap(~V1, ncol = 4, scales = "free_x") +
opts(title = "Distribution of Speakers in Shakespearean Drama") +
xlab("Speaking Role") +
ylab("Words Spoken") +
opts(axis.text.x=theme_text(angle=90, hjust=1))
Running that code on the data frame read from this tab-separated file yields a plot in which the x-axis of each faceted plot is only partially ordered. Someone else on SO asked a very similar question, but the only proposed solution was to use grid arrange. Because my data set is quite a bit larger than the data set in that question, though, this won't be a terribly swift operation, so I wanted to ask: Is there a way to reorder the x axis of each faceted plot so as to show the bars in increasing (or decreasing) order of size? I would be very grateful for any help others can offer on this question.