Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 geom_bar position = "dodge" does not dodge

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.

like image 385
geodex Avatar asked Jun 12 '12 22:06

geodex


People also ask

What does position Dodge do in ggplot2?

Dodging preserves the vertical position of an geom while adjusting the horizontal position. position_dodge() requires the grouping variable to be be specified in the global or geom_* layer.

What is the difference between Geom_bar and Geom_col?

There are two types of bar charts: geom_bar() and geom_col() . geom_bar() makes the height of the bar proportional to the number of cases in each group (or if the weight aesthetic is supplied, the sum of the weights). If you want the heights of the bars to represent values in the data, use geom_col() instead.

What does stat mean in Geom_bar?

For this, we have to specify three arguments within the geom_bar function: position = “dodge” stat = “summary” fun = “mean”

What is position identity?

Identity positioning is understood as the key conceptual link between the person and wider social contexts. In other words, identity bridges the particularities of individual career trajectories and their historic, socio-cultural and local contexts (via discourses and narratives).


2 Answers

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')
like image 157
Justin Avatar answered Oct 05 '22 23:10

Justin


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.

like image 24
geodex Avatar answered Oct 05 '22 23:10

geodex