【问题标题】:Plotting a histogram with two vectors next to each other绘制具有相邻两个向量的直方图
【发布时间】:2021-01-29 08:26:38
【问题描述】:

我的数据如下:

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")

【问题讨论】:

  • 您想要使用base R plot 或ggplot2 得到答案?
  • 我认为 ggplot2 有我的偏好,但我也会对 base 感到满意。

标签: r ggplot2


【解决方案1】:

试试这个:

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))

【讨论】:

  • 不客气!如果您愿意,请考虑接受答案
  • 我会 ;),你知道如何让 x 轴显示所有值而不是四分位数吗?
  • 添加scale_x_continuous(breaks = c(1:9), labels = c(1:9))!
【解决方案2】:

而是使用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)

或转置版本:

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

【讨论】:

  • 非常感谢!使用此解决方案是否也可以将每个数字并排放置?
  • @Tom 并排的每个数字对您来说到底是什么意思?
  • 就像莱昂纳多建议的答案(它工作得很好,所以它不是绝对必要的,只是出于好奇)。
  • @Tom 是的!这更简单,只需 transpose 表格(编辑)。
猜你喜欢
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
  • 2018-05-07
相关资源
最近更新 更多