用两个相邻的向量绘制直方图

时间:2021-01-29 08:26:38

标签: r ggplot2

我有如下数据:

A <- structure(c(9, 7, 9, 9, 9, 8, 9, 6, 4, 7, 9, 9, 9, 8, 7, 7, 9, 
8, 8, 9, 5, 5, 8, 7, 5, 9, 9, 7, 7, 9, 8, 7, 8, 9, 4, 7, 9, 8, 
6, 7, 7, 4, 8, 6, 9, 9, 8, 1, 9, 9, 9, 8, 9, 9, 6, 7, 4, 7, 9, 
6, 6, 9, 9, 8, 6, 8, 7, 7, 7, 5, 9, 5, 7, 9, 8, 4, 9, 8, 8, 8, 
5, 8, 1, 7, 7, 5, 6, 9, 5, 9, 6, 9, 6, 9, 9, 9, 8, 9, 9, 9, 9, 
4, 6, 4, 8, 6, 8, 8, 7, 4, 6, 7, 4, 8, 8, 8, 7, 9, 3, 8, 8, 6, 
9, 8, 8, 6, 5, 8, 3, 8, 6, 8, 7, 7, 6, 9, 5, 9, 8, 7, 9, 7, 9, 
9, 8, 9, 6, 8, 9, 8, 6, 8, 9, 9, 9, 4, 8, 8, 5, 8, 7, 8, 8, 9, 
9, 6, 8, 5, 9, 8, 7, 9, 9, 7, 6, 8, 7, 7, 8, 9, 6, 7, 8, 9, 7, 
6, 6, 9, 7, 7, 8, 7, 7, 2, 4, 9, 9, 7, 7, 9, 7, 6, 9, 9, 8, 5, 
5), label = NA_character_, class = c("labelled", "numeric"))

B <- structure(c(9, 9, 9, 8, 8, 9, 6, 9, 8, 8, 6, 9, 9, 9, 6, 7, 9, 
7, 8, 9, 7, 9, 9, 8, 7, 9, 8, 7, 8, 9, 8, 9, 9, 9, 9, 7, 9, 7, 
8, 9, 7, 7, 8, 4, 6, 9, 7, 7, 9, 9, 9, 8, 9, 8, 9, 9, 4, 8, 9, 
8, 7, 9, 9, 8, 7, 8, 9, 8, 2, 7, 8, 8, 8, 8, 8, 6, 4, 9, 9, 8, 
3, 7, 3, 8, 8, 9, 7, 9, 5, 6, 7, 8, 9, 8, 9, 9, 9, 9, 9, 9, 9, 
7, 3, 7, 9, 7, 7, 7, 8, 8, 9, 9, 8, 8, 9, 6, 9, 9, 6, 7, 8, 7, 
8, 9, 9, 7, 6, 8, 7, 9, 6, 5, 8, 8, 7, 9, 8, 9, 9, 7, 9, 7, 9, 
8, 7, 9, 4, 8, 7, 7, 9, 9, 9, 9, 9, 4, 9, 9, 6, 7, 6, 7, 8, 9, 
8, 9, 5, 9, 8, 8, 8, 9, 9, 6, 8, 8, 8, 8, 8, 8, 7, 8, 9, 9, 9, 
7, 4, 8, 7, 7, 9, 8, 8, 7, 5, 8, 9, 8, 8, 9, 8, 5, 8, 9, 8, 9, 
7), label = NA_character_, class = c("labelled", "numeric"))

我想出了如何做到这一点:

hist(A, breaks=9, col=rgb(0,0,1,0.5), xlim=c(1, 9), xlab = "Personal Norm", main = paste("Distribution of the Personal Norm"))
hist(B, breaks=9,col=rgb(1,0,0,0.5), xlim=c(1, 9), add=T)
legend("topleft", c("tax", "truth"), fill=c(rgb(0,0,1,0.5), rgb(1,0,0,0.5)))

但我更喜欢将条形分开,如 this(Len Greski 的回答)。我在下面发布了他的回答中的代码。但我无法弄清楚如何将他的答案应用于我的数据。有人可以帮我吗?

rawData <-                                          
"sector  Year2003    Year2004    Year2005    Year2006    Year2007
Agriculture   532918    543230        532043      562146    585812
Mining        1236807   1258769     1263937      1250930    1235517
Construction 1505948    1598346      1645017     1785796    1874591
Manufacturing 6836256   7098173     7302589      7731867    7844533
Wholesale      8635763  918174       966467       1037362   1070758"

library(reshape2)

gdpData <- read.table(textConnection(rawData),header=TRUE,
                      sep="",stringsAsFactors=TRUE)

gdpMelt <- melt(gdpData,id="sector",
            measure.vars=c("Year2003","Year2004","Year2005","Year2006","Year2007"))

gdpMelt$year <- as.factor(substr(gdpMelt$variable,5,8))

library(ggplot2)
ggplot(gdpMelt, aes(sector, value, fill = year)) + 
     geom_bar(stat="identity", position = "dodge") + 
     scale_fill_brewer(palette = "Set1")

enter image description here

2 个答案:

答案 0 :(得分:2)

试试这个:

library(ggplot2)

df <- data.frame(value = c(A, B), 
                 variable = rep(c("tax", "truth"), each = length(A)))

ggplot(df) + 
  geom_bar(aes(value, fill = variable), position = "dodge") + 
  scale_fill_manual(values = c(rgb(0,0,1,0.5), rgb(1,0,0,0.5))) + 
  theme(legend.title = element_blank(), legend.position = c(0.1, 0.85))

答案 1 :(得分:1)

而是使用 barplot。只需 table A-B 数据框的每一列,并使用可能的值作为 levels=factor

tb <- sapply(data.frame(A, B), function(x) table(factor(x, levels=sort(unique(unlist(d))))))
clr <- c(rgb(0,0,1,0.5), rgb(1,0,0,0.5))

b <- barplot(tb, beside=T, col=rep(clr, each=nrow(tb)), xaxt="n")
axis(1, as.vector(b), rep(1:nrow(tb), 2))
legend("topleft", c("tax", "truth"), fill=clr)

enter image description here

或转置版:

barplot(t(tb), beside=T, col=clr)
legend("topleft", c("tax", "truth"), fill=clr)

enter image description here

相关问题