Re: dodged labels

289 views
Skip to first unread message

Ista Zahn

unread,
Jun 13, 2012, 7:30:44 AM6/13/12
to Roey Angel, ggp...@googlegroups.com, igreg
Hi Roey,

It's not clear what you expect should happen when you dodge the
labels. Can you give a reproducible example and more clearly state
what result you're looking for?

Also, I've changed the subject line as this seems to be different
issue than the OP described.

Best,
Ista

On Wed, Jun 13, 2012 at 5:57 AM, Roey Angel <an...@mpi-marburg.mpg.de> wrote:
> 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
>
>
>
> On Sunday, March 11, 2012 8:26:35 AM UTC+1, winston wrote:
>>
>> 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:
>>   https://github.com/hadley/ggplot2/commit/ac91fe4
>>
>> 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
>>
>> On Sat, Mar 10, 2012 at 6:12 PM, igreg <zoor...@gmx.net> wrote:
>>>
>>> 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
>>>
>>> --
>>> 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
>>
>>
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Roey Angel

unread,
Jun 13, 2012, 9:33:49 AM6/13/12
to ggp...@googlegroups.com, Roey Angel, igreg
Hi Ista,
Thanks for the quick reply.
What I'd like is for the text labels to not overlap each other as they do at the moment.
That is, I'd like the position to be roughly what is given by X and Y but then to be shifted somewhat so that the labels have a minimal overlap.
I thought that's what position_dodge is supposed to do; isn't it?!

Thanks again,
Roey
>>> To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
>>> More options: http://groups.google.com/group/ggplot2
>>
>>
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Ista Zahn

unread,
Jun 13, 2012, 10:17:12 AM6/13/12
to Roey Angel, ggp...@googlegroups.com, igreg
Hi Roey,

Someone may correct me, but I don't think position_dodge is up to the
task. Your best bet is probably the directlabels package -- see
http://drunks-and-lampposts.com/2012/02/22/labelling-a-ggplot-scatterplot/
for an example.

Best,
Ista
>> >>> To unsubscribe: email ggplot2+u...@googlegroups.com
>> >>> More options: http://groups.google.com/group/ggplot2
>> >>
>> >>
>> > --
>> > You received this message because you are subscribed to the ggplot2
>> > mailing
>> > list.
>> > Please provide a reproducible example:
>> > https://github.com/hadley/devtools/wiki/Reproducibility
>> >
>> >
>> > To post: email ggp...@googlegroups.com
>> > To unsubscribe: email ggplot2+u...@googlegroups.com
>> > More options: http://groups.google.com/group/ggplot2
>
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages