[英]plot stacked bar plot in R
I would like to create histogram [stacked bar plot] in R with data like this: 我想在R中创建直方图[堆积条形图],数据如下:
Period = c(1,1,2,2,3,3,4,4)
Sample = c("A","B","A","B","A","B","A","B")
Value1 = c(3,2,6,7,3,2,1,2)
Value2 = c(1,0,5,2,2,0,2,5)
x <- data.frame(Period,Sample,Value1,Value2)
Is it possible to have "Period" and "Sample" on the X axes and The values ("Value1" and "Value2") in "Stacked Bar Plot". 是否可以在X轴上设置“Period”和“Sample”,在“Stacked Bar Plot”中设置值(“Value1”和“Value2”)。 So the hight of first histogram would be 4 (separate for Value1 and Value2) Thank you for your help in advance!
所以第一个直方图的高度为4(Value1和Value2分开)感谢您的帮助! Best regards.
最好的祝福。
You are describing a stacked bar chart, not a histogram. 您正在描述堆积条形图,而不是直方图。 With
ggplot
you can do it as follows: 使用
ggplot
您可以按如下方式执行此操作:
library(ggplot2)
library(reshape2)
x <- data.frame(
Period = c(1,1,2,2,3,3,4,4),
Sample = c("A","B","A","B","A","B","A","B"),
Value1 = c(3,2,6,7,3,2,1,2),
Value2 = c(1,0,5,2,2,0,2,5)
)
mx <- melt(x, id.vars=1:2)
ggplot(mx, aes(x=Period, y=value, fill=variable)) +
geom_bar(stat="identity") +
facet_grid(~Sample)
It's not as pretty as the ggplot solution but 它不像ggplot解决方案那么漂亮但是
v <- rbind(Value1,Value2)
barplot(v,beside=FALSE,names=levels(interaction(Period,Sample)),legend=TRUE)
seems to work. 似乎工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.