awkward behaviour of ggplot legend

41 views
Skip to first unread message

Dr. Hans Hansen

unread,
Jan 3, 2013, 12:14:18 PM1/3/13
to ggp...@googlegroups.com
Dear ggplotters,

i found a kind of an akward behaviour in ggplot's legends. When comparing my two plots (code below), the order of the both legends is reversed (instrument top/bottom), although the only difference between both plots is that the text is shorter. Is there any way to fix the order of legends? Thanks in advance, Hans

Depr_data = structure(list(ProbandenCode = c(1013L, 1016L, 1021L, 1035L, 
1036L, 1053L, 1055L, 1073L, 1077L, 1082L, 1092L, 1093L, 1096L, 
1108L, 1133L, 1142L, 1153L, 1164L, 1171L, 1191L), domain = c("Depression measures", 
"Depression measures", "Depression measures", "Depression measures", 
"Depression measures", "Depression measures", "Depression measures", 
"Depression measures", "Depression measures", "Depression measures", 
"Depression measures", "Depression measures", "Depression measures", 
"Depression measures", "Depression measures", "Depression measures", 
"Depression measures", "Depression measures", "Depression measures", 
"Depression measures"), instrument = c("D-CAT", "PHQ-9", "PHQ-9", 
"D-CAT", "D-CAT", "PROMIS-D", "PROMIS-D", "PROMIS-D", "HADS-D", 
"PROMIS-D", "HADS-D", "HADS-D", "D-CAT", "D-CAT", "PHQ-9", "HADS-D", 
"D-CAT", "HADS-D", "D-CAT", "D-CAT"), est = c(64.4771238155766, 
30.8535300468852, 86.1683620263858, 56.9560536764661, 52.9707017754472, 
60.3676149524244, 53.1275710701918, 75.5248001041676, 71.5627547786529, 
61.0422264868669, 24.9100323780759, 52.985966965781, 64.5696460322587, 
60.681890071965, 49.8207016657427, 38.8278633094552, 46.7382290718199, 
58.0175066560819, 43.4385524728252, 68.5689840678163), se = c(0.184034072822827, 
1.41933530902089, 0.472613034782868, 0.220887802652718, 0.291507186158107, 
0.226139763742554, 0.390288416473865, 0.212134522793905, 0.437160224580254, 
0.218584213055146, 1.78669022732551, 0.596654554899663, 0.202060897282499, 
0.212487158847873, 0.549838635745393, 1.0178823356537, 0.447134920850865, 
0.52190913376693, 0.541445617353796, 0.207487443977494), CI = c(3.60706782732741, 
27.8189720568094, 9.26321548174421, 4.32940093199328, 5.71354084869889, 
4.43233936935406, 7.64965296288775, 4.15783664676054, 8.56834040177298, 
4.28425057588086, 35.01912845558, 11.6944292760334, 3.96039358673697, 
4.1647483134183, 10.7768372606097, 19.9504937788125, 8.76384444867696, 
10.2294190218318, 10.6123341001344, 4.06675390195888), Depression = structure(c(1L, 
1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("No Depression", "Depression"), class = "factor"), 
    Anxiety = structure(c(1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c("No Anxiety Disorder", 
    "Anxiety Disorder"), class = "factor")), .Names = c("ProbandenCode", 
"domain", "instrument", "est", "se", "CI", "Depression", "Anxiety"
), row.names = c(101L, 127L, 167L, 269L, 277L, 416L, 432L, 568L, 
598L, 640L, 718L, 726L, 749L, 845L, 1047L, 1118L, 1205L, 1294L, 
1349L, 1501L), class = "data.frame")

ggplot(Depr_data, aes(x = est)) +
geom_density(aes(y=..density.. * 300, group = Depression, fill = Depression), alpha = .3) +
scale_fill_grey("Distribution of theta estimates from patients\nwith and without Depression:", start=.6, end = .3) +
geom_line(aes(y = CI, linetype = instrument), size = .75)

ggplot(Depr_data, aes(x = est)) +
geom_density(aes(y=..density.. * 300, group = Depression, fill = Depression), alpha = .3) +
scale_fill_grey("Distribution of theta", start=.6, end = .3) +
geom_line(aes(y = CI, linetype = instrument), size = .75)

Ista Zahn

unread,
Jan 3, 2013, 12:29:38 PM1/3/13
to Dr. Hans Hansen, ggp...@googlegroups.com
Hi Hans,

That is strange! I don't see why the length of the title should change
the order. Fortunately you can specify the order using guides(), e.g.,

ggplot(Depr_data, aes(x = est)) +
geom_density(aes(y=..density.. * 300, group = Depression, fill =
Depression), alpha = .3) +
scale_fill_grey("Distribution of theta", start=.6, end = .3) +
geom_line(aes(y = CI, linetype = instrument), size = .75) +
guides(fill=guide_legend(order=2), linetype=guide_legend(order=1))

Best,
Ista

Brian Diggs

unread,
Jan 3, 2013, 4:50:15 PM1/3/13
to Ista Zahn, Dr. Hans Hansen, ggplot2
On 1/3/2013 9:29 AM, Ista Zahn wrote:
> Hi Hans,
>
> That is strange! I don't see why the length of the title should change
> the order. Fortunately you can specify the order using guides(), e.g.,
>
> ggplot(Depr_data, aes(x = est)) +
> geom_density(aes(y=..density.. * 300, group = Depression, fill =
> Depression), alpha = .3) +
> scale_fill_grey("Distribution of theta", start=.6, end = .3) +
> geom_line(aes(y = CI, linetype = instrument), size = .75) +
> guides(fill=guide_legend(order=2), linetype=guide_legend(order=1))

If I recall, the order of the guides was deterministic, but dependent on
some hash of the whole legend (or something like that) that was
sensitive to any changes in the legend and could not be (easily)
predicted. The order parameter was included so that the order could be
set when that was a relevant consideration.

> Best,
> Ista
--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

Dr. Hans Hansen

unread,
Jan 4, 2013, 3:55:12 AM1/4/13
to ggp...@googlegroups.com, Ista Zahn, Dr. Hans Hansen

Thanks!
Reply all
Reply to author
Forward
0 new messages