首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用ggrepel (或其他方式)对齐并标记ggalluvial中的地层

如何使用ggrepel (或其他方式)对齐并标记ggalluvial中的地层
EN

Stack Overflow用户
提问于 2021-07-22 23:26:58
回答 1查看 220关注 0票数 1

我在R中使用ggalluvial生成了一些冲积图。

下面的代码示例产生了一些接近我想要实现的东西。例如,

代码语言:javascript
复制
library("ggalluvial")
par(mar = c(1,1,1,1)*12, cex = 0.6, xpd=NA)

# generate some example data
somelongnames <- c("homo sapiens", "homo sapiens", letters[18],
                   "some other long name", letters[seq(4)])

df <- data.frame(x = factor(somelongnames),
                 y = factor(c("this label is long", "Golgi", 
                       letters[13:18])),
                 count = c(2, 10, 4, 5, 5, 1, 9, 3))

ll <- unique(c(as.character(df$x), as.character(df$y)))
grid.col <- rainbow(length(ll))
grid.col <- setNames(grid.col, ll)

# set colours for alluvial plot (this is a little tricky as strata 
# colour ordering is required for ggalluvial strata)
names(df) <- c("Condition1", "Condition2", "value")
levs1 <- levels(df$Condition1) 
levs2 <- levels(df$Condition2)
res1 <- unique(df$Condition1)
res2 <- unique(df$Condition2)
cond1_cols <- grid.col[levs1[levs1 %in% res1]]
cond2_cols <- grid.col[levs2[levs2 %in% res2]]
columnCols <- c(cond1_cols, cond2_cols)
stratCols <- c(rev(cond1_cols), rev(cond2_cols))

# plot alluvial diagram
q <- ggplot(df,
            aes(y = value, axis1 = Condition1, axis2 = Condition2)) +
  geom_alluvium(aes(fill = Condition1), width = 0) +
  scale_fill_manual(values = columnCols) +
  geom_stratum(width = 1/8, fill = paste0(stratCols), color = "white") +
  scale_x_discrete(limits = c("Condition1", "Condition2"), 
                   expand = c(.09, .09)) +
  scale_y_continuous(breaks = NULL) +
  theme_minimal() +
  theme(axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        panel.grid.major.y = element_blank(),
        panel.grid.major.x = element_blank()) +
  theme(legend.position = "none") +
  ylab(NULL)

# add labels
  q +
    geom_label(stat = "stratum", aes(label = after_stat(stratum)),
               color = stratCols, fontface = "bold", size = 3)     

我想移动标签,使它们位于地层的左右两边,这样它们就不会与绘图重叠(当标签很长时,这尤其令人讨厌)。我见过使用ggrepel包完成此操作的示例。

我尝试过在geom_text_repel中使用for循环和ifelse语句来调用aes (根据Solution 2 here),但是不能很好地理解代码。有没有人可以帮助或有更好的解决方案来实现这一点?

使用ggrepel的失败代码示例,例如

代码语言:javascript
复制
q + ggrepel::geom_text_repel(
    aes(label = ifelse(Condition1 == as.character(res1)[1],as.character(res1)[1], NA)),
        stat = "stratum", size = 4, 
        direction = "y", nudge_x = -.5
) 

理想情况下,我希望生成像Jason Cory Brunson示例中的solution 2这样的内容。

代码语言:javascript
复制
> sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] parallel  stats4    stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ggalluvial_0.12.3    RColorBrewer_1.1-2   archivist_2.3.6      circlize_0.4.12      dplyr_1.0.5          viridis_0.6.0       
 [7] viridisLite_0.4.0    pheatmap_1.0.12      pRolocdata_1.29.1    ggplot2_3.3.3        bandle_1.0           pRoloc_1.30.0       
[13] BiocParallel_1.24.1  MLInterfaces_1.70.0  cluster_2.1.1        annotate_1.68.0      XML_3.99-0.6         AnnotationDbi_1.52.0
[19] IRanges_2.24.1       MSnbase_2.16.1       ProtGenerics_1.22.0  mzR_2.24.1           Rcpp_1.0.6           Biobase_2.50.0      
[25] S4Vectors_0.28.1     BiocGenerics_0.36.0  BiocStyle_2.18.1    
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-23 01:09:03

也许这能满足你的需要。第一。因为我不太熟悉ggalluvial,所以我更改了数据的设置,以遵循链接中示例中的代码,首先展开数据集并将其转换为长格式。这样做可以让我在stratumalluvium上进行映射,并且更容易对Condition变量进行条件调整。

这样做之后,您可以通过使用ifelse来使用默认的geom_text来对齐标签。或者,您可以使用两个geom_text_repel层将标签放在层的左侧或右侧。

代码语言:javascript
复制
library(tidyr)
library(dplyr)

df_expanded <- df[rep(row.names(df), df$value), ]
df_expanded <- df_expanded %>%
  mutate(id = row_number()) %>%
  pivot_longer(-c(value, id), names_to = "Condition", values_to = "label")

# plot alluvial diagram
q <- ggplot(df_expanded, aes(x = Condition, stratum = label, alluvium = id, fill = label)) +
  geom_flow(width = 0) +
  scale_fill_manual(values = columnCols) +
  scale_color_manual(values = stratCols) +
  geom_stratum(width = 1 / 8, color = "white") +
  scale_x_discrete(
    expand = c(.25, .25)
  ) +
  scale_y_continuous(breaks = NULL) +
  theme_minimal() +
  theme(
    axis.ticks.y = element_blank(),
    axis.text.y = element_blank(),
    panel.grid.major.y = element_blank(),
    panel.grid.major.x = element_blank()
  ) +
  theme(legend.position = "none") +
  ylab(NULL)

q +
  geom_text(
    aes(
      label = after_stat(stratum),
      hjust = ifelse(Condition == "Condition1", 1, 0),
      x = as.numeric(factor(Condition)) + .075 * ifelse(Condition == "Condition1", -1, 1),
      color = after_stat(stratum)
    ),
    stat = "stratum", fontface = "bold", size = 3
  )
#> Warning: The `.dots` argument of `group_by()` is deprecated as of dplyr 1.0.0.

代码语言:javascript
复制
q +
  ggrepel::geom_text_repel(
  aes(label = ifelse(after_stat(x) == 1, as.character(after_stat(stratum)), "")),
  stat = "stratum", size = 4, direction = "y", nudge_x = -.6) +
  ggrepel::geom_text_repel(
    aes(label = ifelse(after_stat(x)  == 2, as.character(after_stat(stratum)), "")),
    stat = "stratum", size = 4, direction = "y", nudge_x = .6
  )

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68487536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档