使用ggrepel标记单个点

yv5phkfx  于 2023-11-14  发布在  其他
关注(0)|答案(3)|浏览(147)

我尝试使用geom_label_repel为图上的几个数据点添加标签。在这种情况下,它们恰好是箱线图上的离群值。我已经运行了大部分代码,我可以标记离群值,但由于某种原因,我得到了多个标签(等于整个数据集的样本大小)Map到该点。我希望这个离群值只有一个标签。
示例:

以下是我的数据:

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

字符串
我的代码

  1. ####################################################################################################
  2. # Time to pupation statistical analysis
  3. ####################################################################################################
  4. ## linear model
  5. pupation_Model=lm(sus_dev_data$days_to_pupation~sus_dev_data$time_point)
  6. pupationANOVA=aov(pupation_Model)
  7. summary(pupationANOVA)
  8. # Tukey test to study each pair of treatment :
  9. pupationTUKEY <- TukeyHSD(x=pupationANOVA, which = 'sus_dev_data$time_point',
  10. conf.level=0.95)
  11. ## Function to generate significance labels on box plot
  12. generate_label_df <- function(pupationTUKEY, variable){
  13. # Extract labels and factor levels from Tukey post-hoc
  14. Tukey.levels <- pupationTUKEY[[variable]][,4]
  15. Tukey.labels <- data.frame(multcompLetters(Tukey.levels, reversed = TRUE)['Letters'])
  16. #I need to put the labels in the same order as in the boxplot :
  17. Tukey.labels$treatment=rownames(Tukey.labels)
  18. Tukey.labels=Tukey.labels[order(Tukey.labels$treatment) , ]
  19. return(Tukey.labels)
  20. }
  21. #generate labels using function
  22. labels<-generate_label_df(pupationTUKEY , "sus_dev_data$time_point")
  23. #rename columns for merging
  24. names(labels)<-c('Letters','time_point')
  25. # obtain letter position for y axis using means
  26. pupationyvalue<-aggregate(.~time_point, data=sus_dev_data, max)
  27. #merge dataframes
  28. pupationfinal<-merge(labels,pupationyvalue)
  29. ####################################################################################################
  30. # Time to pupation plot
  31. ####################################################################################################
  32. # Plot of data
  33. (pupation_plot <- ggplot(sus_dev_data, aes(time_point, days_to_pupation)) +
  34. Alex_Theme +
  35. geom_boxplot(fill = "grey80", outlier.size = 0.75) +
  36. geom_text(data = pupationfinal, aes(x = time_point, y = days_to_pupation,
  37. label = Letters),vjust=-2,hjust=.5, size = 4) +
  38. #ggtitle(expression(atop("Days to pupation"))) +
  39. labs(y = 'Days to pupation', x = 'Weeks post-hatch') +
  40. scale_y_continuous(limits = c(0, 200)) +
  41. scale_x_discrete(labels=c("3" = "13", "8" = "18",
  42. "12" = "22")) +
  43. geom_label_repel(aes(x = 1, y = 9),
  44. label = '1')
  45. )

kknvjkwl

kknvjkwl1#

这里有一个简短的例子来演示这是怎么回事。本质上,你的标签是beng回收到与数据相同的长度。

  1. df = data.frame(x=1:5, y=1:5)
  2. ggplot(df, aes(x,y, color=x)) +
  3. geom_point() +
  4. geom_label_repel(aes(x = 1, y = 1), label = '1')

字符串


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

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

展开查看全部
igsr9ssn

igsr9ssn2#

根据您的数据,您有3个离群值(每组一个),您可以通过应用John Tukey对离群值的经典定义(上限:Q3+1.5IQR和下限:Q1-1.5IQR)手动识别它们(但您可以自由设置自己的规则来定义离群值)。您可以使用函数quantileIQR来获得这些点。
在这里,我使用dplyr包将它们合并到管道序列中:

  1. library(tidyverse)
  2. Outliers <- sus_dev_data %>% group_by(time_point) %>%
  3. mutate(Out_up = ifelse(days_to_pupation > quantile(days_to_pupation,0.75)+1.5*IQR(days_to_pupation), "Out","In"))%>%
  4. mutate(Out_Down = ifelse(days_to_pupation < quantile(days_to_pupation,0.25)-1.5*IQR(days_to_pupation), "Out","In")) %>%
  5. filter(Out_up == "Out" | Out_Down == "Out")
  6. # A tibble: 3 x 4
  7. # Groups: time_point [3]
  8. time_point days_to_pupation Out_up Out_Down
  9. <fct> <int> <chr> <chr>
  10. 1 3 9 In Out
  11. 2 8 122 Out In
  12. 3 12 60 Out In

字符串
正如@dww所提到的,如果你想让你的离群值被单个标记,你需要传递一个新的嵌套框给geom_label_repel。所以,这里我们使用嵌套框Outliers来填充geom_label_repel函数:

  1. library(ggplot2)
  2. library(ggrepel)
  3. ggplot(sus_dev_data, aes(time_point, days_to_pupation)) +
  4. #Alex_Theme +
  5. geom_boxplot(fill = "grey80", outlier.size = 0.75) +
  6. geom_text(data = pupationfinal, aes(x = time_point, y = days_to_pupation,
  7. label = Letters),vjust=-2,hjust=.5, size = 4) +
  8. #ggtitle(expression(atop("Days to pupation"))) +
  9. labs(y = 'Days to pupation', x = 'Weeks post-hatch') +
  10. scale_y_continuous(limits = c(0, 200)) +
  11. scale_x_discrete(labels=c("3" = "13", "8" = "18",
  12. "12" = "22")) +
  13. geom_label_repel(inherit.aes = FALSE,
  14. data = Outliers,
  15. aes(x = time_point, y = days_to_pupation, label = "Out"))


你会得到下面的图表:
x1c 0d1x的数据
我希望它能帮助你弄清楚如何标记你所有的离群值。

展开查看全部
uqdfh47h

uqdfh47h3#

@asd-tm的答案given here很好,但是标签隐藏了一些点。为了避免这种情况,技巧在于为其他点添加一个空标签

  1. library(ggplot2)
  2. library(ggrepel)
  3. dat <- rbind(
  4. mtcars[, c("wt", "mpg")],
  5. `my car` = c(2.5, 23.92395)
  6. )
  7. dat$label <- "" # the trick
  8. dat$label[nrow(dat)] <- "My car prediction"
  9. ggplot(dat, aes(wt, mpg, label = label)) +
  10. geom_point() +
  11. geom_label_repel()

字符串
x1c 0d1x的数据

展开查看全部

相关问题