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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")
question from:
https://stackoverflow.com/questions/65951039/plotting-a-histogram-with-two-vectors-next-to-each-other