问题描述
下面的代码示例产生了接近我想要实现的目标。例如,
library("ggalluvial")
par(mar = c(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,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
包执行此操作的示例。
我尝试在 ifelse
中对 aes
的调用中使用 for 循环和 geom_text_repel
语句(按照 Solution 2 here),但无法弄清楚代码.任何人都可以提供帮助或有更好的解决方案来实现这一目标吗?
使用 ggrepel
的失败代码示例,例如
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 的内容。
> 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
解决方法
也许这符合您的需要。第一的。由于我对 ggalluvial
不太熟悉,因此我更改了数据设置以遵循链接中示例中的代码,首先扩展数据集并将其转换为长格式。这样做使我能够在 stratum
和 alluvium
aes 上制作地图,并可以更轻松地以 Condition
变量为条件。
这样做之后,您可以使用默认的 geom_text
通过使用 ifelse
来对齐标签。或者您可以使用两个 geom_text_repel
层将标签放在层的左侧或右侧。
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.
q +
ggrepel::geom_text_repel(
aes(label = ifelse(after_stat(x) == 1,as.character(after_stat(stratum)),"")),size = 4,direction = "y",nudge_x = -.6) +
ggrepel::geom_text_repel(
aes(label = ifelse(after_stat(x) == 2,nudge_x = .6
)