首页 > 解决方案 > 将id标签添加到ggplot中限制线以上的点

问题描述

我有一个这样的数据框

id <- c(5738180,51845,167774,517814,1344920)
amount <- c(3.76765976,0.85195407,1.96821355,0.01464609,0.57378284)
outlier <- c("TRUE","FALSE","FALSE","FALSE","FALSE")
df.sample <- data.frame(id,amount,outlier) 

我正在尝试绘制这些点并将id标签添加到任何超出限制的点。在这种情况下(id=5738180)

我正在这样策划

library(tidyverse)    
library(ggplot2)
library(ggrepel)  # help avoid overlapping text labels
library(gridExtra) # adds custom table inside ggplot
library(scales)  # breaks and labels for axes and legends 
library(ggthemes)  # Adding desired ggplot themes

df.sample %>%
        ggplot(aes(x = as.numeric(row.names(df.sample)),
                   y = amount, label = as.character(id))) + 
        geom_point(alpha = 0.6, position = position_jitter(w = 0.05, h = 0.0), 
                   aes(colour = (amount < 3)), size = 2) +
        geom_hline(aes(yintercept = 3, linetype = "Limit"), 
                   color = 'black', size = 1) +      
        geom_text(aes(y = 3,x = amount[4],
                      label = paste("Limit = ", round(3, 3)),
                      hjust = 0, vjust = 1.5)) +
        geom_text_repel(data          = subset(df.sample, outlier == 'TRUE'),
                        nudge_y       = 0.75,
                        size          = 4,
                        box.padding   = 1.5,
                        point.padding = 0.5,
                        force         = 100,
                        segment.size  = 0.2,
                        segment.color = "grey50",
                        direction     = "x") +
        geom_label_repel(data          = subset(df.sample, outlier == 'TRUE'),
                         nudge_y       = 0.75,
                         size          = 4,
                         box.padding   = 0.5,
                         point.padding = 0.5,
                         force         = 100,
                         segment.size  = 0.2,
                         segment.color = "grey50",
                         direction     = "x") 
      labs(title = "Outlier Detection",
           y = "amount",
           x = "") +
        theme_few() +
        theme(legend.position = "none",
              axis.text = element_text(size = 10, face = "bold"), 
              axis.title = element_text(size = 10, face = "bold"),
              plot.title = element_text(colour = "blue", hjust = 0.5,
                                        size = 15, face = "bold"),
              strip.text = element_text(size = 10, face = "bold")) +
        scale_colour_manual(values = c("TRUE" = "green","FALSE" = "red"))

我遇到了一个错误"Error: Aesthetics must be either length 1 or the same as the data (1): x"

有人可以指出我正确的方向吗?

标签: rggplot2

解决方案


问题在于geom_text_repel()geom_label_repel()。您对数据进行了子集化,现在仅包含 1 行,但aes()它们继承自具有 5 行的原始数据,因此出现错误。要解决此问题,请在调用之外对数据进行子集化ggplot(),并为其更改美学。您还缺少一个+aftergeom_label_repel()并且下面的结果修改了nudge_ytonudge_x并删除了geom_text_repel().

outliers <- subset(df.sample, outlier == TRUE) 

ggplot(data = df.sample, 
       aes(x = as.numeric(row.names(df.sample)),
           y = amount, 
           label = as.character(id))) + 
  geom_point(alpha = 0.6, 
             position = position_jitter(w = 0.05, h = 0.0), 
             aes(colour = (amount < 3)), 
             size = 2) +
  geom_hline(aes(yintercept = 3, 
                 linetype = "Limit"), 
             color = 'black', 
             size = 1) +      
  geom_text(aes(y = 3,x = amount[4],
                label = paste("Limit = ", 
                              round(3, 3)),
                hjust = 0, 
                vjust = 1.5)) +
  geom_label_repel(data = outliers,
                   aes(x = as.numeric(rownames(outliers)),
                       y = amount,
                       label = amount),
                   nudge_x       = 0.75,
                   size          = 4,
                   box.padding   = 0.5,
                   point.padding = 0.5,
                   force         = 100,
                   segment.size  = 0.2,
                   segment.color = "grey50",
                   direction     = "x",
                   inherit.aes = F) +
  labs(title = "Outlier Detection",
       y = "amount",
       x = "") +
  theme_few() +
  theme(legend.position = "none",
        axis.text = element_text(size = 10, face = "bold"), 
        axis.title = element_text(size = 10, face = "bold"),
        plot.title = element_text(colour = "blue", hjust = 0.5,
                                  size = 15, face = "bold"),
        strip.text = element_text(size = 10, face = "bold")) +
  scale_colour_manual(values = c("TRUE" = "green","FALSE" = "red"))

在此处输入图像描述


推荐阅读