# ggplot2 geom_bar position = "dodge" does not dodge

15,121

## Solution 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')
``````

``````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.

## Solution 2

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

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')
``````
Share:
15,121

Author by

### geodex

Updated on July 13, 2022

• geodex 11 months

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.

• joran almost 11 years
Right, I was going to point out that without a natural grouping aesthetic like `fill` or `colour`, dodging won't do much.
• Justin almost 11 years
@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.
• geodex almost 11 years
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.
• Justin almost 11 years
I'm not sure what you mean by dodging together... what you described is exactly what `position='dodge'` does. do you mean `position='stack'`?
• geodex almost 11 years
@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.
• Justin almost 11 years
@geodex see my edit. But the plot shows exactly the number of elements in each group.
• geodex almost 11 years
@Justin Thanks justin but not the plot I was looking for.