I want to make a plot that looks like this in ggplot2:
Here's a dummy dataset:
set.seed(1)
dat <- data.frame(x = exp(rnorm(6)),
f1 = factor(c("a","b","c","c","d","d")),
f2 = factor(c("","","1","2","1","2")))
And a non-working example:
ggplot(data=dat, aes(x=f1, y=x, fill= f2)) +
geom_bar(stat="identity", width=0.9)+
scale_fill_manual(values=c("red", "blue", rep("green",4)))
I could do this by brute force in base
graphics, but I'm unsure how to go about it in ggplot2
, and I need to use ggplot2
so as to keep this plot consistent with the theme I'm using throughout the project.
So how do I make the factors 1
and 2
go side by side? And how do I center the labels like I have in the drawing? And how do I make the colors behave?