r - Reorder grouped bar plot within facets -
this question has answer here:
- reorder bars in geom_bar ggplot2 1 answer
my third question today, learning lot all.
i trying reorder bar groups within each facet curr.data$gap
variable. gap difference between bars each y label, , need order within each facet go smallest largest gap.
slightly different other reorder questions need consider within-facet ordering.
normally simple bar plot factoring data set levels, though cannot make work here.
data:
structure(list(dept = structure(c(2l, 2l, 2l, 2l, 2l, 2l, 2l, 1l, 1l, 1l, 1l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 1l, 1l, 1l, 1l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l), .label = c("distribution centre services", "it", "marketing", "merchandise & inventory", "operations , communication" ), class = "factor"), label = c("test 25", "test 23", "test 24", "test 27", "test 26", "test 28", "test 29", "test 31", "test 33", "test 30", "test 32", "test 38", "test 36", "test 37", "test 43", "test 34", "test 35", "test 40", "test 39", "test 42", "test 41", "test 25", "test 23", "test 24", "test 27", "test 26", "test 28", "test 29", "test 31", "test 33", "test 30", "test 32", "test 38", "test 36", "test 37", "test 43", "test 34", "test 35", "test 40", "test 39", "test 42", "test 41"), gap = c(-0.16, -0.18, -0.21, -0.22, -0.27, -0.29, -0.31, -0.31, -0.35, -0.39, -0.42, -0.15, -0.15, -0.2, -0.21, -0.22, -0.27, -0.29, -0.29, -0.31, -0.36, -0.16, -0.18, -0.21, -0.22, -0.27, -0.29, -0.31, -0.31, -0.35, -0.39, -0.42, -0.15, -0.15, -0.2, -0.21, -0.22, -0.27, -0.29, -0.29, -0.31, -0.36), section = c(2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l), impeff = structure(c(2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), .label = c("effectiveness", "importance"), class = "factor"), score = c(0.77, 0.79, 0.82, 0.8, 0.83, 0.9, 0.91, 0.94, 0.89, 0.94, 0.91, 0.82, 0.74, 0.78, 0.81, 0.83, 0.85, 0.82, 0.81, 0.8, 0.83, 0.61, 0.61, 0.61, 0.58, 0.56, 0.61, 0.6, 0.63, 0.54, 0.55, 0.49, 0.67, 0.59, 0.58, 0.6, 0.61, 0.58, 0.53, 0.52, 0.49, 0.47)), .names = c("dept", "label", "gap", "section", "impeff", "score"), row.names = c(23l, 24l, 25l, 26l, 27l, 28l, 29l, 30l, 31l, 32l, 33l, 34l, 35l, 36l, 37l, 38l, 39l, 40l, 41l, 42l, 43l, 66l, 67l, 68l, 69l, 70l, 71l, 72l, 73l, 74l, 75l, 76l, 77l, 78l, 79l, 80l, 81l, 82l, 83l, 84l, 85l, 86l), class = "data.frame")
code:
curr.plot <- ggplot(data = curr.data, aes(x = label, y = score)) + geom_bar(aes(fill = impeff),stat = "identity", position = "dodge",width = .7) + geom_text(aes(label=percent(score),group=impeff), position= position_dodge(width=.7),vjust=.4,hjust=-.1) + facet_grid(dept~., switch = "y", scales = "free_y", space = "free") + theme(legend.position = "bottom")+ coord_flip()+ guides(fill = guide_legend(reverse=true,title=null))+ theme(axis.title.x = element_blank()) + theme(axis.title.y = element_blank()) + theme(axis.text.y = element_text(colour="black"))+ theme(strip.text.y = element_text(size = 12, colour = "black"))+ scale_y_continuous(labels=percent,limits = c(0,1))
replace x = label
x = reorder(label,gap)
Comments
Post a Comment