0

I am making a bar graph, that is split into three groups (Dft, dt, and ft). In each group, there should be three time periods, which each have two bars (one for male and female).

I've been able to mostly achieve this, except it seems like the values for the male and female groups are combined, although the y axis is set to only the y values.

Here is my table:

> my.table
   my.condition my.periods       my.prevalence my.gender
1           dft  1988-1994                0.95      Male
2            dt  1988-1994                1.06      Male
3            ft  1988-1994                0.68      Male
4           dft  1999-2004                0.65      Male
5            dt  1999-2004                0.27      Male
6            ft  1999-2004                 0.4      Male
7           dft  2011-2014                1.24      Male
8            dt  2011-2014  1.0900000000000001      Male
9            ft  2011-2014                 0.7      Male
10          dft  1988-1994                0.71    Female
11           dt  1988-1994 0.55000000000000004    Female
12           ft  1988-1994                0.38    Female
13          dft  1999-2004                1.06    Female
14           dt  1999-2004                1.05    Female
15           ft  1999-2004 0.28000000000000003    Female
16          dft  2011-2014                0.32    Female
17           dt  2011-2014                0.78    Female
18           ft  2011-2014                0.74    Female

And here is the code for producing the bar-graph.

ggplot(my.table, aes(y=my.table$my.prevalence, x=my.table$my.periods, fill=my.table$my.gender)) + 
  geom_bar( stat="identity") +    
  facet_wrap(~my.table$my.condition)

Here is what it looks like. enter image description here

I am trying to get the red and blue bars to sit next to each other basically.

Community
  • 1
  • 1
Mousa
  • 23
  • 5
  • 3
    add `position = "dodge"` inside your `geom_bar` call – Gregor Thomas Jun 21 '19 at 15:37
  • 4
    Also, don't use `$` inside `aes()` or the `facet_wrap` formula - this causes bugs. `ggplot(my.table, aes(y=my.prevalence, x=my.periods, fill=my.gender)) + geom_bar( stat="identity", position = 'dodge') + facet_wrap(~my.condition)` – Gregor Thomas Jun 21 '19 at 15:39
  • 3
    Also, `my.prevalence` looks like a factor when I suspect it should be numeric. If you run `my.table$my.preference = as.numeric(as.character(my.table$my.preference))` first, the y axis should look should be more like you expect. – Jon Spring Jun 21 '19 at 15:39
  • Possible duplicate of [How do I plot male and female data separately?](https://stackoverflow.com/questions/55851821/how-do-i-plot-male-and-female-data-separately) – TheSciGuy Jun 21 '19 at 16:57

0 Answers0