【问题标题】:How to display label using ggrepel on sorted x and y-axis value如何在排序的 x 和 y 轴值上使用 ggrepel 显示标签
【发布时间】:2017-10-10 10:05:51
【问题描述】:

我有以下代码:

library(tidyverse)
all_annot_df <- as.tibble(iris)  %>% 
  mutate(Obs = paste0("Obs_", row_number())) %>% 
  select(Obs, Species, Sepal.Length)

signif_thres <- 7.5
all_annot_df["Significant"] <- ifelse((all_annot_df$Sepal.Length > signif_thres),"Signif","NotSignif")
p <- all_annot_df %>% 
  ggplot(aes(reorder(Obs,Sepal.Length), Sepal.Length, colour=Species)) +
  geom_point() +
  theme_bw() + 
  xlab("Observation") +
  theme(axis.text.x=element_blank(),
        axis.ticks.x=element_blank())   + 
  geom_hline(aes(yintercept=signif_thres, colour='red'))


p 

它产生以下情节:

如上图所示,我想使用ggrepel 显示标签。 我该怎么做?

【问题讨论】:

    标签: r ggplot2 dplyr


    【解决方案1】:
    library(ggrepel)
    library(tidyverse)
    

    指定您想要注释的观察结果

    observs = paste0( "Obs_", c(106, 118, 119, 123, 132))
    

    相应地过滤数据

    p + 
    geom_text_repel(data = all_annot_df %>%
                      filter(Obs %in% observs),
                    aes(x = reorder(Obs,Sepal.Length),
                        y = Sepal.Length,
                        label = Obs),
                    color= "black")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-29
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 2020-04-20
      • 2019-07-31
      相关资源
      最近更新 更多