Reputation: 6861
The basic chart functionality in R makes it relatively simple to produce a bar chart with data columns side-by-side, using the beside
flag. For instance, this code:
test1 <- c(2,4)
test2 <- c(4,5)
data <- data.frame(test1, test2)
barplot(
as.matrix(data),
cex.lab = 1.5,
cex.main = 1.4,
beside=TRUE
)
Produces this chart:
I would like to produce a similar chart with ggplot2
, but the geom_bar
function does not have a beside
flag. Can a similar bar chart be produced with ggplot2
?
Upvotes: 2
Views: 6718
Reputation: 23129
Try this:
library(reshape2)
data$ID <- as.factor(1:nrow(data))
ggplot(melt(data, id='ID'), aes(variable, value, fill=ID, group=ID)) +
geom_bar(stat='identity', position='dodge')
Upvotes: 4