removing geom_text redundancies

1,612 views
Skip to first unread message

Avram Aelony

unread,
Oct 3, 2009, 3:56:36 AM10/3/09
to ggplot2
Hello,

I'm getting repeated labels (one per each level in variable) where one
would suffice: e.g. qplot( data=m, x=date, y=value, colour=variable,
label=major_event_desc ) + scale_y_continuous(formatter = "comma") +
geom_text( angle=90, size=3, colour='purple' ) +stat_smooth()

For example, with 2 levels of variable, I get 2 lines but also 2
overlapping indications of "independence day" on july 4th...

Is there any way to specify that the label should occur exactly once?


Here is a reproducible example:

data(economics)
m <- melt(data=economics[, c(1,2,4,5,6)], id.vars=c('date') )
m$idates <- ifelse(m$date=="1987-07-31", "random date of importance",
"")
qplot(data=m, x=date, y=value, colour=variable, geom=c('line','point'),
label=idates )+ geom_text( angle=90, size=4, colour='purple' ) +
stat_smooth()


Notice that the text "random date of importance" is repeated and
superimposed on itself once for each level of "variable" (i.e. pce,
psavert,
uempmed, unemploy ) making it unreadable. It would be great to have
something that indicates that it should be rendered only if it has
not been
rendered already.

Best regards,
Avram

baptiste auguie

unread,
Oct 3, 2009, 7:19:22 AM10/3/09
to Avram Aelony, ggplot2
Hi,

Can you not use annotate?

date = "1987-07-31"
condition = m$date==date

qplot(data=m, x=date, y=value, colour=variable, geom=c('line','point')) +
annotate("text", x=unique(m$date[condition]),y=0, label="random date
of importance",
angle=90, size=4, colour='purple' , hjust = 0)


HTH,

baptiste

Avram Aelony

unread,
Oct 3, 2009, 2:25:36 PM10/3/09
to baptiste auguie, ggplot2


I don't think so. The example is only meant to illustrate (pun
intended) the point... In effect, the labels come from a separate
data set full of such dates and the mechanism produces many graphs in
an automated fashion.

It would be great to instruct geom_text to do it's work only for the
first level of the colour=variable...

-A

hadley wickham

unread,
Oct 4, 2009, 9:20:08 AM10/4/09
to Avram Aelony, ggplot2
> Notice that the text "random date of importance" is repeated and
> superimposed on itself once for each level of "variable" (i.e. pce,
> psavert,
> uempmed, unemploy ) making it unreadable.  It would be great to have
> something that indicates that it should be rendered only if it has
> not been
> rendered already.

Only plot the data that you want in the text layer:

library(ggplot2)


m <- melt(data=economics[, c(1,2,4,5,6)], id.vars=c('date') )

imp_date <- subset(m, date == "1987-07-31" & variable == "pce")
imp_date$label <- "My important date"

ggplot(m, aes(date, value)) +
geom_line(aes(colour = variable)) +
geom_point(aes(colour = variable)) +
geom_text(aes(label = label), data = imp_date, angle = 90, size = 4)


Hadley


--
http://had.co.nz/

Avram Aelony

unread,
Oct 6, 2009, 10:14:56 PM10/6/09
to hadley wickham, ggplot2

Thanks for this...
Regards,
-Avram
Reply all
Reply to author
Forward
0 new messages