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
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