用ggrepel标记单个点

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用ggrepel标记单个点相关的知识,希望对你有一定的参考价值。

我正在尝试使用geom_label_repel向图上的几个数据点添加标签。在这种情况下,它们恰好在箱形图上是异常值。我已经完成了大部分代码的工作,可以标记离群值,但是由于某种原因,我将多个标签(等于整个数据集的样本量)映射到该点。我只想要一个离群值标签。

示例:enter image description here

这是我的数据:

dput(sus_dev_data)
structure(list(time_point = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L), .Label = c("3", "8", "12"), class = "factor"), 
    days_to_pupation = c(135L, 142L, 143L, 155L, 149L, 159L, 
    153L, 171L, 9L, 67L, 53L, 49L, 72L, 67L, 55L, 64L, 60L, 122L, 
    53L, 51L, 49L, 53L, 50L, 56L, 44L, 47L, 60L)), row.names = c(1L, 
2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 20L, 21L, 22L, 23L, 24L, 26L, 27L, 28L, 29L, 30L), class = "data.frame")

和我的代码...

####################################################################################################
# Time to pupation statistical analysis
####################################################################################################

## linear model
pupation_Model=lm(sus_dev_data$days_to_pupation~sus_dev_data$time_point)
pupationANOVA=aov(pupation_Model)
summary(pupationANOVA)

# Tukey test to study each pair of treatment :
pupationTUKEY <- TukeyHSD(x=pupationANOVA, which = 'sus_dev_data$time_point', 
                          conf.level=0.95)

## Function to generate significance labels on box plot
generate_label_df <- function(pupationTUKEY, variable){

  # Extract labels and factor levels from Tukey post-hoc 
  Tukey.levels <- pupationTUKEY[[variable]][,4]
  Tukey.labels <- data.frame(multcompLetters(Tukey.levels, reversed = TRUE)['Letters'])

  #I need to put the labels in the same order as in the boxplot :
  Tukey.labels$treatment=rownames(Tukey.labels)
  Tukey.labels=Tukey.labels[order(Tukey.labels$treatment) , ]
  return(Tukey.labels)
}

#generate labels using function
labels<-generate_label_df(pupationTUKEY , "sus_dev_data$time_point")

#rename columns for merging
names(labels)<-c('Letters','time_point')

# obtain letter position for y axis using means
pupationyvalue<-aggregate(.~time_point, data=sus_dev_data, max)

#merge dataframes
pupationfinal<-merge(labels,pupationyvalue) 

####################################################################################################
# Time to pupation plot
####################################################################################################

# Plot of data
(pupation_plot <- ggplot(sus_dev_data, aes(time_point, days_to_pupation)) +
  Alex_Theme +
  geom_boxplot(fill = "grey80", outlier.size = 0.75) +
  geom_text(data = pupationfinal, aes(x = time_point, y = days_to_pupation, 
                                      label = Letters),vjust=-2,hjust=.5, size = 4) +
  #ggtitle(expression(atop("Days to pupation"))) +
  labs(y = 'Days to pupation', x = 'Weeks post-hatch') +
  scale_y_continuous(limits = c(0, 200)) +
  scale_x_discrete(labels=c("3" = "13", "8" = "18",
                              "12" = "22")) +
    geom_label_repel(aes(x = 1, y = 9), 
                     label = '1')
)
答案

下面是一个简短的示例,以说明正在发生的事情。本质上,您的标签被回收以与数据相同的长度。

df = data.frame(x=1:5, y=1:5)

ggplot(df, aes(x,y, color=x)) +
  geom_point() +
  geom_label_repel(aes(x = 1, y = 1), label = '1')

enter image description here

您可以通过为ggrepel提供新数据来覆盖它

ggplot(df, aes(x,y, color=x)) +
  geom_point() +
  geom_label_repel(data = data.frame(x=1, y=1), label = '1')

enter image description here

以上是关于用ggrepel标记单个点的主要内容,如果未能解决你的问题,请参考以下文章

R语言可视化散点图(scatter plot)图为图中的部分数据点添加标签ggrepel包来帮忙

R语言可视化散点图(scatter plot)为每一个数据点都绘制指示线段或者都不绘制ggrepel包来帮忙

R语言可视化散点图(scatter plot)图为图中的部分数据点添加标签始终显示所有标签,即使它们有太多重叠ggrepel包来帮忙

如何实现 FragmentManager 和 FragmentTransaction 来替换单个片段?

R语言可视化散点图(scatter plot)图中的标签和数据点互相堆叠丑死了,ggrepel包来帮忙:文本标签(label)相互排斥,远离数据点,远离绘图区域的边缘。

入口点不能用“异步”修饰符标记