10

I have the dataframe p3 below:

         test     result
    1      1    26.87778
    2      1    24.52598
    3      1    24.02202
    4      1    20.32632
    5      1    22.00618
    6      2    19.84013
    7      2    19.68983
    8      2    19.84013
    9      2    19.23892
    10     2    19.23892
    11     3    34.36430
    12     3    33.28196
    13     3    33.82313
    14     3    33.82313
    15     3    32.47020
    16     4    25.55169
    17     4    26.90442
    18     4    25.40138
    19     4    24.19895
    20     4    25.85230
    21     4    25.70199
    22     4    24.95047
    23     5    18.64646
    24     5    18.64646
    25     5    17.80653
    26     5    18.64646
    27     5    18.31049

I am trying to make a barchart with dodged results using the code:

    ggplot(p3, aes(x = test, y = result))+ geom_bar(position="dodge", stat="identity")

but it doesn't work at all. I don't understand why it is not working since I used the same code before and it worked.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
geodex
  • 1,219
  • 3
  • 13
  • 22

2 Answers2

12
ggplot(p3, aes(x = test, y = result, group = result)) + 
    geom_bar(position="dodge", stat="identity")

you can see what is happening if you change the group argument to color.

ggplot(p3, aes(x = test, y = result, color = result)) + 
    geom_bar(position="dodge", stat="identity")

Edited for comments:

It looks like there are odd numbers of groups because there are. Group 4 has 7 elements in it in the data you supplied. group 3 has 5 but 2 of them are identical. The plot shows the height correctly and is grouping like elements together. its like you called unique on each group.

I think plotting:

ggplot(p3, aes(x=test, y=result, group=result, color=result)) + 
  geom_bar(position='dodge', stat='identity')

displays this quite well. As far as each group having 5 elements, that isn't the case. Group 4 has 7. To see what you're describing you could do something like:

ggplot(p3, aes(x=as.integer(row.names(p3)), y=result, fill=factor(test))) +   
  geom_bar(position='dodge', stat='identity')
Justin
  • 42,475
  • 9
  • 93
  • 111
  • Right, I was going to point out that without a natural grouping aesthetic like `fill` or `colour`, dodging won't do much. – joran Jun 12 '12 at 22:54
  • @Joran Good point. However, even with `fill` or `colour` added to the aes, the `position='dodge'` does not happen. You need to use `group` so the `dodge` knows what things its dodging. `stat='identity'` doesn't provide for automated grouping like other stats do. – Justin Jun 12 '12 at 22:59
  • this doesnt work. I was thinking of dodging by test so all test = 1 would be dodging together and so on... With that graph, test 1 has 5 elements, test 2 has 3, test 3 has 4, test 4 has 7, and test 5 3. – geodex Jun 13 '12 at 03:42
  • I'm not sure what you mean by dodging together... what you described is exactly what `position='dodge'` does. do you mean `position='stack'`? – Justin Jun 13 '12 at 14:11
  • @Justin what i meant was test1 should be besides each other then followed by test2 and so on...each test is supposed to have 5 items. The graph produced from your code has uneven number of elements, which should not be. – geodex Jun 14 '12 at 08:58
  • @geodex see my edit. But the plot shows exactly the number of elements in each group. – Justin Jun 14 '12 at 14:20
  • @Justin Thanks justin but not the plot I was looking for. – geodex Jun 15 '12 at 08:10
1

This was answered by Dennis Murphy:

p3$test <- factor(p3$test)
p3$fac <- factor(unlist(sapply(as.vector(table(p3$test)), seq_len)))
ggplot(p3, aes(x = test, y = result, fill = fac)) + 
      geom_bar(position = 'dodge', stat = 'identity')

Adjusting the variables:

ggplot(p3, aes(x = test, y = result, color = fac, fill = test)) +
    geom_bar(position = 'dodge', stat = 'identity', linetype = 0)

I almost got what I wanted, except that the color (outline) should be the same. but it was close enough.

geodex
  • 1,219
  • 3
  • 13
  • 22