我试图绘制一个geom\u条,其堆栈顺序取决于每个条中每组的累积值.因此,每个条应有不同的顺序.这是一个可复制的示例
set.seed(8)
#create dataframe
name <- c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z", "aa")
group <- c("1","1","1","2","2", "2", "1","1","1","1","1","1","1","1","1","1","1","1","2","2","2","2","2","2","2","2")
subgroup <- c("C", "C", "C", "C", "C", "C", "A", "A", "A","A","A","A","B","B","B","B","B","B","B","B", "C", "C", "C", "C", "C", "C")
value <- runif(26, min = 0, max = 10)
df <- data.frame(name, group, subgroup,value)
#plot
ggplot(data=df, aes(x=group, y=value)) +
geom_bar(stat="identity",aes(fill=reorder(subgroup, value)))
The latter results in the following plot:
请注意,很明显,B子组在组1中表示的值高于C,但它绘制在C上方.我希望,对于每个组,子组都是根据其在组中的累积值排序的.有人知道如何做到这一点吗?提前感谢!