我有一些ggalluvial
在 R 中使用的冲积地块。
下面的代码示例产生了接近我想要实现的东西。例如,
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
包来执行此操作的示例。
我尝试在对ifelse
in 的调用中aes
使用for 循环和语句geom_text_repel
(根据此处的解决方案 2),但无法完全弄清楚代码。任何人都可以帮助或有更好的解决方案来实现这一目标吗?
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 的这个例子中产生类似解决方案 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