I know this question has been asked/answered other places but I am very confused with how/where to apply code to reorder variables on my y-axis in a facet grid.
Data:
df <- data.frame(
type = c("Small", "X-large", "Medium", "Large", "Small", "X-large", "Medium", "Large", "Small", "X-large", "Medium", "Large"),
group = c("A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C"),
value = c(22, 40, 31, 60, 26, 24, 22, 18, 30, 70, 60, 50)
)
Figure:
plot <- ggplot(df, aes(y=type, size = 15)) + facet_grid(group ~ ., scales="free_y", space="free_y")
plot <- plot + geom_point(aes(x=value),
size=3)
plot
What I would like is for the variables on the y-axis for each facet to go in a different order (small, medium, large, x-large) from top to bottom (instead of current order: x-large, small, medium, large). How do I change this? I understand my answer should look something like this:
df$new = factor(df$type, levels=c("Small","Medium","Large","X-large"), labels=c("Small","Medium","Large","X-large"))
but I am not sure where to put this into my figure code. I tried putting it where 'type' is but that didn't work... Any help wold be greatly appreciated!