堆积的高度相同的条形图

Err*_*404 1 plot r graph ggplot2

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

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
Run Code Online (Sandbox Code Playgroud)

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

这是我想要生成的

google_image_stacked_barplot

每列1巴...任何想法我应该使用什么命令.

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

谢谢,

A5C*_*2T1 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))
Run Code Online (Sandbox Code Playgroud)

现在.解决你的问题.您需要首先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
Run Code Online (Sandbox Code Playgroud)

绘图很简单:

barplot(prop.table(mydata2, 2))
Run Code Online (Sandbox Code Playgroud)

或者,用颜色:

barplot(prop.table(mydata2, 2), col = c("slateblue", "palevioletred"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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


lattice

@Arun ggplot2以完整性的名义展示了解决方案,但如果是这种情况,那么我们至少应该barchart从"网格"中添加.;)

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

barchart(t(prop.table(mydata2, 2)), stack = TRUE, horizontal = FALSE)
Run Code Online (Sandbox Code Playgroud)

这是结果:

在此输入图像描述


Aru*_*run 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
Run Code Online (Sandbox Code Playgroud)

ggplot2_barplot_stacked