r - Labeling values in ggplot outside geom_smooth threshold -
i want label values in ggplot if outside interval of confidence (geom_smooth area). below example can label of them.
a <- sample(1:15) b <- sample(-6:-20) c <-sample(letters,15) x1 <- data.frame(a, b, c) gg <- ggplot(x1, aes(x = a, y = b)) + labs(x = "new x label", y= "new x label") + geom_point()+ geom_smooth(method=lm) + geom_text(aes(label=c),hjust=2, vjust=1)
you can make fit prior ggplot call , create variable labels points outside confidence region.
## fit, , make variable labels points outside conf. interval fit <- lm(b ~ a) dat <- predict(fit, interval="confidence") x1$inside <- ifelse(x1$b < dat[,"upr"] & x1$b > dat[,"lwr"], "", as.character(x1$c)) gg <- ggplot(x1, aes(x = a, y = b)) + labs(x = "new x label", y= "new x label") + geom_point() + geom_smooth(method=lm) + geom_text(aes(label=inside),hjust=2, vjust=1) gg another option using ggplot_build extract data ggplot , use determine points should labeled.
## use data curve info <- ggplot_build(gg)[[1]][[2]] 
Comments
Post a Comment