I recognize that this question is a close duplicate of this one, but the solution there no longer works (using method="last.qp"
), so I'm asking it again.
The basic issue is that I'd like to use directlabels
(or equivalent) to label smoothed means for each group (from stat_smooth()
), rather than the actual data. The example below shows as close as I've gotten, but the labels aren't recognizing the grouping, or even the smoothed line. Instead, I'm getting the label at the last point. What I'd like is colour-coordinated text at the end of each stat_smooth()
, rather than the legend on the right of the plot. This post provides an approach for labelling the last data point (the behaviour I'm seeing), but I'm looking for an approach to label automatically-generated summaries, if possible.
Here's an example:
library(ggplot2)
library(directlabels)
## Data
set.seed(10)
d <- data.frame(x=seq(1,100,1), y=rnorm(100, 3, 0.5))
d$z <- ifelse(d$y>3,1,0)
## Plot
p <- ggplot(d, aes(x=x, y=y, colour=as.factor(z))) +
stat_smooth(inherit.aes=T, se=F, span=0.8, show.legend = T) +
geom_line(colour="grey50") +
scale_x_continuous(limits=c(0,110)) +
geom_dl(label="text", method="maxvar.points", inherit.aes=T)
p
geom_label_repel
seems to work across facet panels easily, which is helpful for my actual problem. Thanks! – Alliterative