【问题标题】:Increase font size while using fviz_nbclust?使用 fviz_nbclust 时增加字体大小?
【发布时间】:2019-12-27 05:05:12
【问题描述】:

我正在尝试增加使用fviz_nbclust 制作的绘图的字体/标签大小。我尝试查找“fviz_nbclust”、“size”、“font”、“increase”的各种组合,但没有找到任何东西。

我在this post 上看到了一个建议,当我只使用plot() 时它有效,但不能只与fviz_nbclust 一起使用。我还检查了包 factoextra (here) 的文档,但在第 49/50 页上并没有真正看到我认为可能有帮助的任何内容。

这是我的代码:

set.seed(123)
wss <- function(k) {
       kmeans(datT, k, nstart = 10)$tot.withinss
       }
k.values <- 1:15
wss_values <- map_dbl(k.values, wss)

pdf("within_clus_sum1.pdf",width = 11, height = 8) 
plot(k.values, wss_values,
       type="b", pch = 19, frame = FALSE, 
       xlab="Number of clusters K",
       ylab="Total within-clusters sum of squares",
       cex.lab=1.5, cex.axis=1.5, cex.main=1.5, cex.sub=1.5)

dev.off() 

pdf("within_clus_sum2.pdf",width = 11, height = 8) 
fviz_nbclust(datT, kmeans, method = "wss")
dev.off() 

如您所见,我只是想了解使用肘部方法确定的最佳聚类数是多少。

我希望做的是增加使用fviz_nbclust 生成的绘图中的标题、标签和刻度线大小,因为我在其中包含了一篇文章,如果我将绘图放在一行中,则为两个或三个,它们几乎难以辨认。任何人都可以提供任何帮助,我们将不胜感激。

【问题讨论】:

  • 如果您打算打印更小的widthheight,您是否尝试过使用它们?

标签: r plot cluster-analysis


【解决方案1】:

factoextra 包使用 ggplot2 生成绘图。这意味着您可以使用 ggplot2 主题更改绘图的外观。例如:

elbow.plot <- fviz_nbclust(datT, kmeans, method = "wss", k.max = 10, verbose = F, nboot = 100)
# change size and color of title and x axis labels
elbow.plot + theme(axis.text.x = element_text(size = 15, color = "red"), title = element_text(size = 15, color = "blue")

您可以了解有关 ggplot2 的更多信息,例如 here

如果你想改变线条的颜色和大小,我意识到这有点棘手,因为实际上 fviz_nbclust 函数调用了一个 ggpubr 函数,它是 ggplot2 的包装器(就这么简单)。您可以轻松更改的唯一参数是线条颜色:

fviz_nbclust(datT, kmeans, method = "wss", k.max = 10, verbose = F, nboot = 100, linecolor = "red")

要更改其他参数,您可以执行以下操作:

fviz_nbclust(datT, kmeans, method = "wss", k.max = 10, verbose = F, nboot = 100)  + 
theme(axis.text.x = element_text(size = 15))  + 
geom_line(aes(group = 1), color = "red", linetype = "dashed",size = 3) + 
geom_point(group = 1, size = 5, color = "blue")

这基本上是在顶部再次绘制情节,这不是最佳的,但作为一个快速修复效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 2016-01-14
    • 2018-07-16
    • 2015-02-01
    • 1970-01-01
    • 2012-11-07
    • 2011-12-03
    相关资源
    最近更新 更多