Reputation: 247
Working from the following dataframe:
> foo
species density day percent
1 species1 high 1 0.40
2 species1 low 1 0.20
3 species1 medium 1 0.40
4 species2 high 1 0.35
5 species2 low 1 0.10
6 species2 medium 1 0.55
7 species3 high 1 0.35
8 species3 low 1 0.20
9 species3 medium 1 0.45
10 species4 high 1 0.30
11 species4 low 1 0.20
12 species4 medium 1 0.50
13 species1 high 100 0.50
14 species1 low 100 0.40
15 species1 medium 100 0.10
16 species2 high 100 0.40
17 species2 low 100 0.05
18 species2 medium 100 0.55
19 species3 high 100 0.65
20 species3 low 100 0.05
21 species3 medium 100 0.30
22 species4 high 100 0.40
23 species4 low 100 0.20
24 species4 medium 100 0.40
I have created the following faceted bar graph:
require(ggplot2)
foo$density<-factor(foo$density,levels=c('low','medium','high'))
d <- ggplot(foo, aes(x=species, y=percent, fill=density)) +
geom_bar(aes(width=.65), stat="identity") +
facet_grid(. ~ day)
However, I would like to merge these graphs to create a single two-factor bar graph. On the x-axis, each day- 1 and 100 - would be grouped by species. Any suggestions on how to create this?
Many thanks!
Upvotes: 8
Views: 2953
Reputation: 14667
Here is another approach that incorporates @Joran's suggestion along with your requirements.
I have changed day
to a factor, but also changed the default x-axis label to "Species". Also, because density
is clearly a sequential factor, I have use a sequential color scale from Color Brewer (http://colorbrewer2.org/). You can experiment with color scales by changing palette
number or calling palette by name scale_fill_brewer(palette="GnBu")
. Find the palette names on the Color Brewer web page.
foo <- read.table(header=TRUE,
text="species density day percent
1 species1 high 1 0.40
2 species1 low 1 0.20
3 species1 medium 1 0.40
4 species2 high 1 0.35
5 species2 low 1 0.10
6 species2 medium 1 0.55
7 species3 high 1 0.35
8 species3 low 1 0.20
9 species3 medium 1 0.45
10 species4 high 1 0.30
11 species4 low 1 0.20
12 species4 medium 1 0.50
13 species1 high 100 0.50
14 species1 low 100 0.40
15 species1 medium 100 0.10
16 species2 high 100 0.40
17 species2 low 100 0.05
18 species2 medium 100 0.55
19 species3 high 100 0.65
20 species3 low 100 0.05
21 species3 medium 100 0.30
22 species4 high 100 0.40
23 species4 low 100 0.20
24 species4 medium 100 0.40")
foo$density <- factor(foo$density, levels=c("low", "medium", "high"))
foo$day <- factor(paste("Day", foo$day, sep="_"))
library(ggplot2)
d2 <- ggplot(foo, aes(x=day, y=percent, fill=density)) +
theme_bw() +
geom_bar(width=0.95, stat="identity") +
scale_fill_brewer(type="seq", palette=15) +
xlab("Species") +
opts(axis.text.x=theme_text(size=6)) +
facet_grid(. ~ species)
ggsave("barplot_1.png", d2, width=6, height=4)
One problem I have not solved is that the levels of density
do not stack in the proper order (for me or in @MYaseen208's answer). Stacking is correct in the original post. Does anyone know what the problem is?
Upvotes: 1
Reputation: 23898
Try this one
foo$species_day <- with(data = foo, expr = paste(species, day))
d <-ggplot(foo, aes(x=species_day, y=percent, fill=density)) +
geom_bar(aes(width=.65), stat="identity")
You can rearrange the levels if you want.
Upvotes: 4