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 

enter image description here

another option using ggplot_build extract data ggplot , use determine points should labeled.

## use data curve info <- ggplot_build(gg)[[1]][[2]] 

Comments

Popular posts from this blog

apache - setting document root in antoher partition on ubuntu -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -

php - laravel retrieving profile firstname from model returns Object of class Illuminate\Database\Eloquent\Builder could not be converted to string -