position="dodge" not working with geom_text in 0.90?

4,451 views
Skip to first unread message

igreg

unread,
Mar 10, 2012, 7:12:45 PM3/10/12
to ggplot2
hi ggploters

please have a look at my code (worked in 0.89 but not anymore in 0.90)

library (ggplot2)
library (reshape)
library (scales)

set.seed (66)
income <- rep(c("high","medium","low"), 5)
variable <- rep(c("USA","Switzerland","Spain","Greece","Japan"),3)
mean.value <- rnorm (15,5,2)
m.data <- data.frame (income,variable,mean.value)

pl <- ggplot(data=m.data, aes(x=variable, y=mean.value,ymax=10))
pl <- pl + geom_bar(aes(fill=income),position="dodge")
pl <- pl + geom_text(aes(label=round(mean.value,
2)),position="dodge",hjust=-0.5)
pl <- pl + coord_flip()
pl

the numbers in the plot are not in dodge position like the bars. they
appear all in the same line.

is it my system or is it a bug in 0.9?

thanks for your help!

greg

R version 2.14.2 (2012-02-29)
Platform: x86_64-pc-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=German_Switzerland.1252 LC_CTYPE=German_Switzerland.
1252
[3] LC_MONETARY=German_Switzerland.1252
LC_NUMERIC=C
[5] LC_TIME=German_Switzerland.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods
base

other attached packages:
[1] ggplot2_0.9.0 scales_0.2.0 reshape_0.8.4 plyr_1.7.1

loaded via a namespace (and not attached):
[1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.1
grid_2.14.2
[5] MASS_7.3-17 memoise_0.1 munsell_0.3
proto_0.3-9.2
[9] RColorBrewer_1.0-5 reshape2_1.2.1 stringr_0.6
tools_2.14.2

Winston Chang

unread,
Mar 11, 2012, 3:26:35 AM3/11/12
to igreg, ggplot2
I couldn't get your code to dodge the text in 0.8.9, until I changed the code from position="dodge" to position=position_dodge(0.9). This is probably because text objects are treated like points, and don't have a real width/height.

After making that change to your code, I tracked down where the change in behavior happened... it looks like it happened with this commit:

I believe the change is actually a feature, not a bug. :)

Typically, a variable is mapped to fill or colour, and each level of that variable indicates a separate group. (Actually, all the discrete variables are crossed to form the groups, but that's not important here.) Next, all objects that have the same x position, but are in different groups, get dodged. 

With the geom_bar, 'income' was mapped to 'fill', so each level of income was a separate group; since each of the bars is in a different group, they are dodged. With the geom_text, there was no grouping variable, and so all of the text at each x was in a single group. Since the text objects were all in one group, they should NOT have been dodged, but the dodging happened anyway. This is because the old code made some erroneous assumptions about the structure of the data.

To make it work now, you have to specify the grouping for the text, which can be done  with 'aes(group=income)'. (You could also use 'colour=income', but you probably don't want to change the text color.)

This code should do the trick:

library(ggplot2)
set.seed (66)
income <- rep(c("high","medium","low"), 5)
variable <- rep(c("USA","Switzerland","Spain","Greece","Japan"),3)
mean.value <- rnorm (15,5,2)
m.data <- data.frame (income,variable,mean.value)

pl  <- ggplot(data=m.data, aes(x=variable, y=mean.value, ymax=10))
pl  <- pl + geom_bar(aes(fill=income),position=position_dodge(0.9))
pl  <- pl + geom_text(aes(group=income, label=round(mean.value, 2)), 
                      position=position_dodge(0.9), hjust=-0.5)
pl  <- pl + coord_flip()
pl


-Winston


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

igreg

unread,
Mar 11, 2012, 3:47:14 PM3/11/12
to ggplot2

thank you so much for your explanations and your proposed code,
Winston!
now, it all works fine :-) i'll specify the "grouping" in cases like
this in my futers codes.
- greg

Roey Angel

unread,
Jun 13, 2012, 5:57:22 AM6/13/12
to ggp...@googlegroups.com, igreg
Hi,
I also have a problem with position="dodge" in geom_text in ggplot0.9 (though I don't know if it worked in previous versions).
The code is bit complicated to give a  minimal example, but I confirmed the problem on other codes as well.
the line is:
geom_text(aes(x= X, y= Y, label = name), data = coords, position = "dodge")

which gives the message:
ymax not defined: adjusting position using y instead

and the dodging doesn't take effect.

I tried also:
position = position_dodge(width=1))
position = position_dodge(width=1, height=1))
or as suggested here:
position=position_dodge(0.9)

But none of them worked (actually adding a numeric value granted me with another warning message):
Warning message:
position_dodge requires non-overlapping x intervals

Any suggestions?

Thanks in advance
Roey

Winston Chang

unread,
Jun 13, 2012, 4:41:55 PM6/13/12
to Roey Angel, ggp...@googlegroups.com, igreg
It's possible to dodge text. There was a bug in 0.9.0 where text would't be properly dodged, though. I think it's fixed in 0.9.1.

Here's are two examples:

dat <- data.frame(class = c("a","a","b","b"), 
                  val = round(runif(4),2),
                  g = c("group1", "group2"))
dat$text_labels <- as.character(sprintf("%2.2f", df$val))


ggplot(data = dat, aes(x=class, y=val, colour=g)) +
  geom_bar(position=position_dodge(width=0.9)) + 
  geom_text(aes(label = text_labels, y = val + 0.1), angle = 45,
            position = position_dodge(width=0.9))


# With a group= variable, instead of colour=
ggplot(data = dat, aes(x=class, y=val)) +
  geom_bar(aes(colour=g), position=position_dodge(width=0.9)) + 
  geom_text(aes(label = text_labels, y = val + 0.1, group = g),
            angle = 45, position = position_dodge(width=0.9))


-Winston

Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

 
To post: email ggp...@googlegroups.com

Jianan He

unread,
May 2, 2016, 11:59:19 AM5/2/16
to ggplot2, zoor...@gmx.net
Thank you very much for your group explanation! I've been quite confused about this issue for a long time! Thanks again!
Reply all
Reply to author
Forward
0 new messages