本文介绍了向符合条件的ggplot geom_jitter点添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 如何将文本添加到使用geom_jittered呈现的点来标记它们? geom_text不起作用,因为我不知道抖动点的坐标。你能捕捉到抖动点的位置,所以我可以传递给geom_text吗? 我的实际用法是绘制一个箱形图,其上包含geom_jitter以显示数据分布并且我想标出离群点或符合某些条件的点(例如,用于彩色图的值的下限为10%)。 一种解决方案将捕获抖动情节的xy位置,并在稍后用于另一层,这是可能的吗? [更新] 从Joran答案中,解决方案是使用基础包中的抖动函数计算抖动值,将它们添加到数据框中并使用geom_point。为了过滤,他使用ddply来创建一个过滤列(一个逻辑向量),并将其用于geom_text中的数据子集化。 他要求提供一个最小数据集。 dat< - data.frame(x = rep(字母[1:3],次数= 100),y = runif(300), lab = paste('id _',1:300,sep ='')) 这是使用我的数据的joran示例的结果,并将id的显示降至最低1% 这是修改代码以使其他变量具有颜色并显示此变量的某些值(每组最低1%): #创建一些示例数据 dat lab = paste('id _',1:300,sep =''),quality = rnorm(300)) #创建数据副本和抖动版本的x变量 datJit< - dat datJit $ xj< - 抖动(as.numeric(factor(dat $ x))) #创建一个指示符变量,用于指出最低1%x datJit中的#obs < - ddply(datjit,。(x),fun = function(g){g $ grp< -g $ y< =分位数(g $ y,0.01); g $ top_q g}) #创建boxplot,叠加抖动点,#标记最低1%点 ggplot(dat,aes(x = x,y = y))+ geom_boxplot()+ geom_point(data = datJit,aes(x = xj,color = 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))) 解决方案您的问题并不完全清楚;例如,您在一点提及标记点,但也提及着色点,所以我不确定您是真正的意思,还是两者。一个可重复的例子会非常有帮助。但是,在我的部分中使用一些猜测,下面的代码会执行我认为您所描述的内容: #创建一些示例数据 dat lab = rep('label',300)) #创建数据的副本和x变量的抖动版本 datJit< - dat datJit $ xj< - 抖动(as.numeric(factor( dat $ x))) #创建一个指示变量,用于指定x datJit< - ddply(datJit,)中最低10%的#obs。 (x),fun = function(g){g $ grp #创建a boxplot,覆盖抖动点和#标记最低10%点 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)) How can I add text to points rendered with geom_jittered to label them? geom_text will not work because I don't know the coordinates of the jittered dots. Could you capture the position of the jittered points so I can pass to geom_text?My practical usage would be to plot a boxplot with the geom_jitter over it to show the data distribution and I would like to label the outliers dots or the ones that match certain condition (for example the lower 10% for the values used for color the plots).One solution would be to capture the xy positions of the jittered plots and use it later in another layer, is that possible?[update]From Joran answer, a solution would be to calculate the jittered values with the jitter function from the base package, add them to a data frame and use them with geom_point. For filtering he used ddply to have a filter column (a logic vector) and use it for subsetting the data in geom_text.He asked for a minimal dataset. I just modified his example (a unique identifier in the label colum)dat <- data.frame(x=rep(letters[1:3],times=100),y=runif(300), lab=paste('id_',1:300,sep=''))This is the result of joran example with my data and lowering the display of ids to the lowest 1%And this is a modification of the code to have colors by another variable and displaying some values of this variable (the lowest 1% for each group):library("ggplot2")#Create some example datadat <- data.frame(x=rep(letters[1:3],times=100),y=runif(300), lab=paste('id_',1:300,sep=''),quality= rnorm(300))#Create a copy of the data and a jittered version of the x variabledatJit <- datdatJit$xj <- jitter(as.numeric(factor(dat$x)))#Create an indicator variable that picks out those# obs that are in lowest 1% by xdatJit <- 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 a boxplot, overlay the jittered points and# label the bottom 1% pointsggplot(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 completely clear; for example, you mention labeling points at one point but also mention coloring points, so I'm not sure which you really mean, or perhaps both. A reproducible example would be very helpful. But using a little guesswork on my part, the following code does what I think you're describing:#Create some example datadat <- data.frame(x=rep(letters[1:3],times=100),y=runif(300), lab=rep('label',300))#Create a copy of the data and a jittered version of the x variabledatJit <- datdatJit$xj <- jitter(as.numeric(factor(dat$x)))#Create an indicator variable that picks out those# obs that are in lowest 10% by xdatJit <- ddply(datJit,.(x),.fun=function(g){ g$grp <- g$y <= quantile(g$y,0.1); g})#Create a boxplot, overlay the jittered points and# label the bottom 10% pointsggplot(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)) 这篇关于向符合条件的ggplot geom_jitter点添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 21:07