Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two-factor bar plot

Tags:

r

ggplot2

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)

enter image description here

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!

like image 361
Drosera aliciae Avatar asked Aug 02 '12 05:08

Drosera aliciae


People also ask

How do you describe a two bar graph?

A double bar graph is a graphical display of information using two bars besides each other at various heights. The bars can be arranged vertically or horizontally. We can use a double bar graph to compare two data groups. A double bar graph has two axes.


1 Answers

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

enter image description here You can rearrange the levels if you want.

like image 167
MYaseen208 Avatar answered Oct 19 '22 17:10

MYaseen208