【问题标题】:adding text to ggplot geom_jitter points that match a condition将文本添加到匹配条件的 ggplot geom_jitter 点
【发布时间】:2011-07-01 17:16:52
【问题描述】:

如何将文本添加到使用 geom_jittered 渲染的点以标记它们? geom_text 不起作用,因为我不知道抖动点的坐标。您能否捕获抖动点的位置,以便我可以传递给 geom_text?

我的实际用法是绘制一个带有 geom_jitter 的箱线图以显示数据分布,我想标记异常点或符合特定条件的点(例如,用于给图上色)。

一种解决方案是捕获抖动图的 xy 位置并稍后在另一层中使用它,这可能吗?

[更新]

从 Joran 的回答中,一个解决方案是使用基本包中的 jitter 函数计算抖动值,将它们添加到数据框并与 geom_point 一起使用。为了过滤,他使用 ddply 有一个过滤列(一个逻辑向量),并将其用于 geom_text 中的数据子集。

他要求一个最小的数据集。我刚刚修改了他的示例(标签列中的唯一标识符)

dat <- data.frame(x=rep(letters[1:3],times=100),y=runif(300),
                      lab=paste('id_',1:300,sep='')) 

这是 joran 示例与我的数据并将 id 的显示降低到最低 1% 的结果

这是对代码的修改,使另一个变量具有颜色并显示该变量的一些值(每组的最低 1%):

library("ggplot2")
#Create some 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 a copy of the data and a jittered version of the x variable
datJit <- dat
datJit$xj <- jitter(as.numeric(factor(dat$x)))

#Create an indicator variable that picks out those
# obs that are in lowest 1% by 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 a boxplot, overlay the jittered points and
# label the 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)))

【问题讨论】:

    标签: r ggplot2 plyr


    【解决方案1】:

    您的问题并不完全清楚;例如,您在某处提到 labeling 点,但也提到 coloring 点,所以我不确定您的真正意思是什么,或者两者兼而有之。一个可重现的例子将非常有帮助。但是我稍微猜测了一下,下面的代码就是我认为你所描述的:

    #Create some example data
    dat <- 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 variable
    datJit <- dat
    datJit$xj <- jitter(as.numeric(factor(dat$x)))
    
    #Create an indicator variable that picks out those 
    # obs that are in lowest 10% by x
    datJit <- 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% 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))        
    

    【讨论】:

    • 感谢 joran,+1 的完美猜测和想象的 +1 用于外部抖动解决方案,另一个额外的想象 +1 用于 ddply 的使用。我接受您的回答,但如果可能的话,我希望使用集成的 geom_jitter 而不是外部抖动选项来查看其他答案。很高兴知道 ggplot 具有允许不同数据到每一层的灵活性。
    【解决方案2】:

    只是对 Joran 精彩解决方案的补充: 当我尝试使用 facet_wrap() 在多面图中使用时,我遇到了 x 轴定位问题。问题是,ggplot2 使用 1 作为每个方面的 x 值。解决方法是创建一个抖动的 1 的向量:

    datJit$xj <- jitter(rep(1,length(dat$x)),amount=0.1)
    

    【讨论】:

      猜你喜欢
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 2018-10-27
      • 1970-01-01
      • 2020-02-27
      • 2019-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多