gpt4 book ai didi

r - 更改我的 ggplot 树状图中的字体

转载 作者:行者123 更新时间:2023-12-05 03:37:36 25 4
gpt4 key购买 nike

我尝试绘制一个彩色树状图以包含在我的论文中,除一件事外一切正常:更改字体。我以前画过很多树状图,改变字体从来都不是问题,但在这段代码中突然是......我可能试图在任何可能的空间写 family="serif",但 idoes 不起作用。我的代码:

dencw3m <- as.dendrogram(aggl.clust.manhattan.ward.cw3m)

dendro.col.cw3m <- dencw3m %>%
set("branches_k_color", k = 5, value = c("blue", "red", "limegreen", "gold", "darkorange")) %>%
set("branches_lwd", 0.6) %>%
set("labels_colors",
value = c("darkslategray")) %>%
set("labels_cex", 0.5)

gg.cw3m <- as.ggdend(dendro.col.cw3m)

ggplot(gg.cw3m, theme = theme_minimal()) +
labs(x = "Fall", y = "Homogenität", title = "Dendrogramm cw3m", family = "serif", cex = 0.4, hang = -1)

最佳答案

我不熟悉 dendextend 包,所以不确定它是否提供了一个简单的选项来通过参数设置字体系列。但是,由于最终结果是 ggplot 对象,实现所需结果的一种选择是手动设置 geom_textfamily 参数用于添加标签的图层。

使用 ?ggdend 中的默认示例:

library(dendextend)
library(ggplot2)

dend <- iris[1:30, -5] %>%
dist() %>%
hclust() %>%
as.dendrogram() %>%
set("branches_k_color", k = 5, value = c("blue", "red", "limegreen", "gold", "darkorange")) %>%
set("branches_lwd", 0.6) %>%
set("labels_colors",
value = c("darkslategray")) %>%
set("labels_cex", 0.5)

ggd1 <- as.ggdend(dend, theme = theme_minimal())

p <- ggplot(ggd1)

查看 p 的层,我们看到 geom_text 是第三层:

p$layers
#> [[1]]
#> mapping: xend = ~xend, yend = ~yend, colour = ~col, linetype = ~lty, size = ~lwd, x = ~x, y = ~y
#> geom_segment: arrow = NULL, arrow.fill = NULL, lineend = square, linejoin = round, na.rm = TRUE
#> stat_identity: na.rm = TRUE
#> position_identity
#>
#> [[2]]
#> mapping: colour = ~col, shape = ~pch, size = ~cex, x = ~x, y = ~y
#> geom_point: na.rm = TRUE
#> stat_identity: na.rm = TRUE
#> position_identity
#>
#> [[3]]
#> mapping: label = ~label, colour = ~col, size = ~cex, x = ~x, y = ~y
#> geom_text: parse = FALSE, check_overlap = FALSE, na.rm = FALSE
#> stat_identity: na.rm = FALSE
#> position_identity

因此,要切换字体系列,我们可以像这样设置该层的 aes_paramsfamily 参数:

p$layers[[3]]$aes_params$family <- "serif"
p

关于r - 更改我的 ggplot 树状图中的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69229552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com