does geom_text jitter when using hjust?

71 views
Skip to first unread message

Andreas Christoffersen

unread,
Oct 10, 2009, 6:24:48 AM10/10/09
to ggplot2
This problem pertains to my previous post to this list (How to use
"expand_range"), but since it's another question I hope it is the
correct approach to ask two seperate questions.

When I call ggplot with
ggplot(dm, aes(x=variable,y=value,group=Type,label=Type,size=I.alt))+
geom_line(aes(col=value2))+
geom_text(data=subset(dm, variable=="idx08"),hjust=-0.2,
size=3,position=position_jitter(w=0, h=0))+
theme_bw()+
scale_x_discrete("", labels=c("2006", "2007", "2008"), expand=c(0,1))+
scale_y_continuous("", formatter="percent")+
opts(legend.position="none")+
scale_colour_manual(values=c("green","red"))

I notice that geom_text is not alligned properbly. I am assuming is
some kind of jittering - but I can't turn it off. I can increase the
jittering (cf the call to geom_text) - but not turn it off. Whats
going on?

This does not happen with hjust=0.

data

dm <- structure(list(Type = structure(c(8L, 21L, 23L, 20L, 6L, 14L,
3L, 24L, 2L, 28L, 32L, 22L, 15L, 29L, 1L, 17L, 18L, 33L, 25L,
13L, 30L, 11L, 26L, 9L, 12L, 4L, 5L, 27L, 16L, 19L, 10L, 31L,
7L, 8L, 21L, 23L, 20L, 6L, 14L, 3L, 24L, 2L, 28L, 32L, 22L, 15L,
29L, 1L, 17L, 18L, 33L, 25L, 13L, 30L, 11L, 26L, 9L, 12L, 4L,
5L, 27L, 16L, 19L, 10L, 31L, 7L, 8L, 21L, 23L, 20L, 6L, 14L,
3L, 24L, 2L, 28L, 32L, 22L, 15L, 29L, 1L, 17L, 18L, 33L, 25L,
13L, 30L, 11L, 26L, 9L, 12L, 4L, 5L, 27L, 16L, 19L, 10L, 31L,
7L), .Label = c("Alvorligere vold", "Andre strafferetlige særlove",
"Andre tyverier", "Bedrageri", "Brandstiftelse", "Butikstyverier m.v.",
"Dokumentfalsk", "Færdselslovovertræd. i øvrigt", "Færdselsuheld med spiritus",
"Falsk forklaring i øvrigt", "Forbr. mod off. myndighed m.v.",
"Freds- og ærekrænkelser", "Hæleri", "Hærværk", "Indbrud i bank,
forretn. m.v.",
"Indbrud i fritidshuse, garager mv", "Indbrud i villaer, lejligheder mv",
"Love vedr. forsvaret og lign.", "Love vedr. spil, bev., næring",
"Lov om euforiserende stoffer", "Mangler ved køretøj", "Røveri",
"Simpel vold", "Spiritus- og promillekørsel", "Trusler",
"Tyv./brugstyv. af andet",
"Tyv./brugstyv. af cykel", "Tyv./brugstyv. af indr. køretøj",
"Tyv/brugstyv. af knallert", "Tyveri fra bil, båd m.v.", "Ulovlig
omgang med hittegods",
"Våbenloven", "Vold o.l. mod off. myndighed"), class = "factor"),
I.alt = c(16137L, 9519L, 5930L, 5502L, 4887L, 3582L, 3101L,
1738L, 1660L, 1649L, 1551L, 1412L, 1338L, 1164L, 1154L, 1057L,
931L, 907L, 857L, 724L, 681L, 644L, 641L, 505L, 450L, 419L,
405L, 328L, 324L, 324L, 320L, 281L, 262L, 16137L, 9519L,
5930L, 5502L, 4887L, 3582L, 3101L, 1738L, 1660L, 1649L, 1551L,
1412L, 1338L, 1164L, 1154L, 1057L, 931L, 907L, 857L, 724L,
681L, 644L, 641L, 505L, 450L, 419L, 405L, 328L, 324L, 324L,
320L, 281L, 262L, 16137L, 9519L, 5930L, 5502L, 4887L, 3582L,
3101L, 1738L, 1660L, 1649L, 1551L, 1412L, 1338L, 1164L, 1154L,
1057L, 931L, 907L, 857L, 724L, 681L, 644L, 641L, 505L, 450L,
419L, 405L, 328L, 324L, 324L, 320L, 281L, 262L), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("idx06", "idx07",
"idx08"), class = "factor"), value = c(1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 0.972675591417568, 0.766866371450899,
0.931743805516597, 0.813047711781889, 0.88728323699422, 0.96420233463035,
0.855743544078362, 1.03710247349823, 0.751470588235294, 0.90443686006826,
0.808403361344538, 0.902834008097166, 0.718181818181818,
0.904555314533623, 1.02717391304348, 0.68957345971564, 1.10324483775811,
0.93030303030303, 0.805309734513274, 0.843003412969283, 0.74914089347079,
0.824786324786325, 1.04060913705584, 1.09150326797386, 0.977941176470588,
0.892405063291139, 0.966666666666667, 0.828125, 0.696, 0.813559322033898,
0.697841726618705, 0.88235294117647, 0.62280701754386, 0.986612873647533,
0.712685595207085, 0.840579710144927, 0.865628042843233,
0.93757225433526, 0.823346303501945, 0.905609973285841, 1.03356890459364,
0.689705882352941, 0.909556313993174, 0.798319327731092,
0.955465587044534, 0.714545454545455, 0.620390455531453,
1.10869565217391, 0.815165876777251, 0.64306784660767, 0.818181818181818,
0.722713864306785, 0.627986348122867, 0.59106529209622, 0.927350427350427,
1.21319796954315, 1.20915032679739, 1.33088235294118, 0.759493670886076,
1.40833333333333, 0.734375, 0.896, 0.932203389830508, 0.60431654676259,
0.872549019607843, 0.675438596491228), Type = structure(c(8L,
21L, 23L, 20L, 6L, 14L, 3L, 24L, 2L, 28L, 32L, 22L, 15L,
29L, 1L, 17L, 18L, 33L, 25L, 13L, 30L, 11L, 26L, 9L, 12L,
4L, 5L, 27L, 16L, 19L, 10L, 31L, 7L, 8L, 21L, 23L, 20L, 6L,
14L, 3L, 24L, 2L, 28L, 32L, 22L, 15L, 29L, 1L, 17L, 18L,
33L, 25L, 13L, 30L, 11L, 26L, 9L, 12L, 4L, 5L, 27L, 16L,
19L, 10L, 31L, 7L, 8L, 21L, 23L, 20L, 6L, 14L, 3L, 24L, 2L,
28L, 32L, 22L, 15L, 29L, 1L, 17L, 18L, 33L, 25L, 13L, 30L,
11L, 26L, 9L, 12L, 4L, 5L, 27L, 16L, 19L, 10L, 31L, 7L), .Label =
c("Alvorligere vold",
"Andre strafferetlige særlove", "Andre tyverier", "Bedrageri",
"Brandstiftelse", "Butikstyverier m.v.", "Dokumentfalsk",
"Færdselslovovertræd. i øvrigt", "Færdselsuheld med spiritus",
"Falsk forklaring i øvrigt", "Forbr. mod off. myndighed m.v.",
"Freds- og ærekrænkelser", "Hæleri", "Hærværk", "Indbrud i bank,
forretn. m.v.",
"Indbrud i fritidshuse, garager mv", "Indbrud i villaer, lejligheder mv",
"Love vedr. forsvaret og lign.", "Love vedr. spil, bev., næring",
"Lov om euforiserende stoffer", "Mangler ved køretøj",
"Røveri", "Simpel vold", "Spiritus- og promillekørsel",
"Trusler", "Tyv./brugstyv. af andet", "Tyv./brugstyv. af cykel",
"Tyv./brugstyv. af indr. køretøj", "Tyv/brugstyv. af knallert",
"Tyveri fra bil, båd m.v.", "Ulovlig omgang med hittegods",
"Våbenloven", "Vold o.l. mod off. myndighed"), class = "factor"),
I.alt = c(16137L, 9519L, 5930L, 5502L, 4887L, 3582L, 3101L,
1738L, 1660L, 1649L, 1551L, 1412L, 1338L, 1164L, 1154L, 1057L,
931L, 907L, 857L, 724L, 681L, 644L, 641L, 505L, 450L, 419L,
405L, 328L, 324L, 324L, 320L, 281L, 262L, 16137L, 9519L,
5930L, 5502L, 4887L, 3582L, 3101L, 1738L, 1660L, 1649L, 1551L,
1412L, 1338L, 1164L, 1154L, 1057L, 931L, 907L, 857L, 724L,
681L, 644L, 641L, 505L, 450L, 419L, 405L, 328L, 324L, 324L,
320L, 281L, 262L, 16137L, 9519L, 5930L, 5502L, 4887L, 3582L,
3101L, 1738L, 1660L, 1649L, 1551L, 1412L, 1338L, 1164L, 1154L,
1057L, 931L, 907L, 857L, 724L, 681L, 644L, 641L, 505L, 450L,
419L, 405L, 328L, 324L, 324L, 320L, 281L, 262L), variable2 =
structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("farve1", "farve2",
"farve3"), class = "factor"), value2 = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L,
2L, 1L, 1L, 2L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA), .Label = c("green", "red"), class =
"factor")), .Names = c("Type",
"I.alt", "variable", "value", "Type", "I.alt", "variable2", "value2"
), row.names = c(NA, -99L), class = "data.frame")

hadley wickham

unread,
Oct 11, 2009, 5:14:28 PM10/11/09
to Andreas Christoffersen, ggplot2
The hjust parameter is proportional to the width of the text. You
probably want something like:

ggplot(dm, aes(variable, value,group=Type,label=Type,size=I.alt))+
geom_line(aes(col=value2))+
geom_text(aes(as.numeric(variable) + 0.1), data=subset(dm,
variable=="idx08"),hjust= 0,
size=3)

Hadley
--
http://had.co.nz/
Reply all
Reply to author
Forward
0 new messages