1

我有一组看起来像这样的数据:

col1  col2   col3   col4     cr
84  88.242  9.833   4.194     A
94  107.571 10.917  3.708     B
188 240.288 16.917  6.333     A
245 371.005 22.333  10.389    A
114 131.599 9.167   4.25      A
71  100.751 8.167   3         B
118 138.543 11.167  4.278     A
162 203.435 14.667  6.444     B
123 152.032 12.167  4.639     B
115 126.945 11.667  5.056     A
125 134.178 10      4.639     B
119 138.926 9.5     4.222     A
106 129.19  9.833   3.833     A
146 162.319 9.833   4.118     A

我尝试使用简单的barplot命令绘制数据,但它没有给出我真正想要的图表。我希望生成一个图,假设每列有 10 个条形图(每个条形图代表一个范围,例如 0-20、20-40 等),X轴为column valuesY轴为% frequency(ofAB) . A and B stacked用不同的颜色(请注意,条形的高度必须相同,因为 Y 轴是百分比频率)。

这就是我想要生成的

google_image_stacked_barplot

每列 1 条...知道我应该为此使用什么命令。

(请忽略照片中的坐标轴名称,这只是我在谷歌上找到的一张照片,代表我需要的)

谢谢,

4

2 回答 2

5

请尝试以易于复制和粘贴的格式发布您的数据,就像我在下面所做的那样:

mydata <- structure(list(col1 = c(84L, 94L, 188L, 245L, 114L, 71L, 118L, 
162L, 123L, 115L, 125L, 119L, 106L, 146L), col2 = c(88.242, 107.571, 
240.288, 371.005, 131.599, 100.751, 138.543, 203.435, 152.032, 
126.945, 134.178, 138.926, 129.19, 162.319), col3 = c(9.833, 
10.917, 16.917, 22.333, 9.167, 8.167, 11.167, 14.667, 12.167, 
11.667, 10, 9.5, 9.833, 9.833), col4 = c(4.194, 3.708, 6.333, 
10.389, 4.25, 3, 4.278, 6.444, 4.639, 5.056, 4.639, 4.222, 3.833, 
4.118), cr = structure(c(1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 
1L, 2L, 1L, 1L, 1L), .Label = c("A", "B"), class = "factor")), .Names = c("col1", 
"col2", "col3", "col4", "cr"), class = "data.frame", row.names = c(NA, 
-14L))

现在。来解决你的问题。您需要先aggregate获取数据,然后将其转换为 a matrix,然后将矩阵中的每个值计算为该列中总和的比例(使用prop.table):

mydataAgg <- aggregate(cbind(col1, col2, col3, col4) ~ cr, mydata, sum)
mydata2 <- as.matrix(mydata1[-1])
rownames(mydata2) <- mydataAgg[[1]]
mydata2
#   col1     col2    col3   col4
# A 1235 1527.057 110.250 46.673
# B  575  697.967  55.918 22.430
prop.table(mydata2, 2)
#        col1      col2      col3      col4
# A 0.6823204 0.6863103 0.6634851 0.6754121
# B 0.3176796 0.3136897 0.3365149 0.3245879

绘图很容易:

barplot(prop.table(mydata2, 2))

或者,使用颜色:

barplot(prop.table(mydata2, 2), col = c("slateblue", "palevioletred"))

在此处输入图像描述

嗯。不是最有趣的情节,但我想绝对是一个清晰的比例模式......


lattice

@Arunggplot2以完整性的名义展示了解决方案,但如果是这种情况,那么我们至少应该barchart从“lattice”添加。;)

为此,我们需要转置prop.table(mydata2, 2)我们之前计算的输出:

barchart(t(prop.table(mydata2, 2)), stack = TRUE, horizontal = FALSE)

结果如下:

在此处输入图像描述

于 2013-01-30T12:28:17.487 回答
5

为了完整起见,这里是 ggplot2 解决方案(使用@AnandaMahto 的数据,感谢您的dput输出)。我melt先用,再data.table用来计算和获取比例(基本上都是内部计算):

require(ggplot2)
require(reshape2)
require(data.table)

df.m <- melt(df, names(df)[5], names(df)[1:4])
dt <- data.table(df.m)
setkey(dt, "cr", "variable")
dt.m <- dt[, list(count = sum(value)), by=list(cr,variable)]
dt.m <- dt.m[, list(cr=cr, prop = count/sum(count)), by=variable]
p <- ggplot(data = dt.m, aes(factor(variable))) + 
         geom_bar(aes(group = cr, weights=prop, fill=cr))
p <- p + scale_fill_brewer(palette = "Set1")
p

ggplot2_barplot_stacked

于 2013-01-30T13:30:57.100 回答