【发布时间】:2017-10-23 16:51:32
【问题描述】:
给定一个 2x2 列联表,其中包含单元格中的计数,我怎样才能获得具有堆叠百分比的分组条形图?
下面是一些示例代码:
species=c(rep("sorgho" , 1) , rep("poacee" , 1) )
condition=rep(c("normal" , "stress") ,1)
value=abs(rnorm(4 , 0 , 4))
data2=data.frame(species,condition,value)
library(ggplot2)
# Stacked Percent
ggplot(data2, aes(fill=condition, y=value, x=species)) +
geom_bar( stat="identity", position="fill")
但我的数据看起来像:
A B
Group C 4 5
D 1 2
情节应该告诉我:A 在 C 组中的百分比是多少,B 在 D 组中的百分比是多少?
谢谢。
【问题讨论】:
-
您只需将数据转换为正确的格式。