r - adding text to ggplot geom_jitter points that match a condition -
how can add text points rendered geom_jittered label them? geom_text not work because don't know coordinates of jittered dots. capture position of jittered points can pass geom_text?
my practical usage plot boxplot geom_jitter on show data distribution , label outliers dots or ones match condition (for example lower 10% values used color plots).
one solution capture xy positions of jittered plots , use later in layer, possible?
[update]
from joran answer, solution calculate jittered values jitter function base package, add them data frame , use them geom_point. filtering used ddply have filter column (a logic vector) , use subsetting data in geom_text.
he asked minimal dataset. modified example (a unique identifier in label colum)
dat <- data.frame(x=rep(letters[1:3],times=100),y=runif(300), lab=paste('id_',1:300,sep=''))
this result of joran example data , lowering display of ids lowest 1%
and modification of code have colors variable , displaying values of variable (the lowest 1% each group):
library("ggplot2") #create example data dat <- data.frame(x=rep(letters[1:3],times=100),y=runif(300), lab=paste('id_',1:300,sep=''),quality= rnorm(300)) #create copy of data , jittered version of x variable datjit <- dat datjit$xj <- jitter(as.numeric(factor(dat$x))) #create indicator variable picks out # obs in lowest 1% x datjit <- ddply(datjit,.(x),.fun=function(g){ g$grp <- g$y <= quantile(g$y,0.01); g$top_q <- g$qual <= quantile(g$qual,0.01); g}) #create boxplot, overlay jittered points , # label bottom 1% points ggplot(dat,aes(x=x,y=y)) + geom_boxplot() + geom_point(data=datjit,aes(x=xj,colour=quality)) + geom_text(data=subset(datjit,grp),aes(x=xj,label=lab)) + geom_text(data=subset(datjit,top_q),aes(x=xj,label=sprintf("%0.2f",quality)))
your question isn't clear; example, mention labeling points @ 1 point mention coloring points, i'm not sure mean, or perhaps both. reproducible example helpful. using little guesswork on part, following code think you're describing:
#create example data dat <- data.frame(x=rep(letters[1:3],times=100),y=runif(300), lab=rep('label',300)) #create copy of data , jittered version of x variable datjit <- dat datjit$xj <- jitter(as.numeric(factor(dat$x))) #create indicator variable picks out # obs in lowest 10% x datjit <- ddply(datjit,.(x),.fun=function(g){ g$grp <- g$y <= quantile(g$y,0.1); g}) #create boxplot, overlay jittered points , # label bottom 10% points ggplot(dat,aes(x=x,y=y)) + geom_boxplot() + geom_point(data=datjit,aes(x=xj)) + geom_text(data=subset(datjit,grp),aes(x=xj,label=lab))
Comments
Post a Comment