Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

r - Plotting a histogram with two vectors next to each other

I have data as follows:

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

I figure out how to do this:

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

But I prefer to have the bars separate like this (answer by Len Greski). I posted the code from his answer below. But I cannot figure out how to apply his answer to my data. Can anyone help me?

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

question from:https://stackoverflow.com/questions/65951039/plotting-a-histogram-with-two-vectors-next-to-each-other

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...