r - 使用缺少绘图的数据标签文本大小

标签 r ggplot2

我正在使用函数 plot_missing 来显示数据集中的 NA 数量。由于我的数据集有 50 个变量,我需要调整文本大小。我设法更改了轴的文本大小,但没有更改数据标签的文本大小。有什么建议?

使用示例数据集:

library(ggplot2)
library(DataExplorer)

df <- data.frame(matrix(data=runif(1000, 0, 100), ncol=50))
df[df>80] <- NA

plot_missing(df, theme_config =list(axis.text=element_text(size=6)))

最佳答案

可能有更优雅的方法来做到这一点,但在这里我修改了 plot_missing功能:

plot_missing_smaller_labels <-
function (data, title = NULL, ggtheme = theme_gray(), 
          theme_config = list(legend.position = c("bottom")))
{
    pct_missing <- NULL
    missing_value <- profile_missing(data)

    output <- ggplot(missing_value, aes_string(x = "feature",
                                               y = "num_missing", fill = "group")) +
      geom_bar(stat = "identity") +
      geom_text(aes(label = paste0(round(100 * pct_missing, 2), "%")), size = 2) + 
      scale_fill_manual("Group", values = c(Good = "#1a9641",
                        OK = "#a6d96a", Bad = "#fdae61", Remove = "#d7191c"),
                        breaks = c("Good", "OK", "Bad", "Remove")) + 
      coord_flip() +
      xlab("Features") +
      ylab("Missing Rows")

    class(output) <- c("single", class(output))
    plotDataExplorer(plot_obj = output, title = title, ggtheme = ggtheme,
                     theme_config = theme_config)
}

我已添加 size = 2geom_text()功能。

新款plot_missing_smaller_labels函数被这样调用:
plot_missing_smaller_labels(df, theme_config=list(axis.text=element_text(size = 6)))

这使标签具有较小的文本大小。

关于r - 使用缺少绘图的数据标签文本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54891109/

相关文章:

r - 使用 R 计算时间序列中的间隙大小

r - R 包 tidymodels 中的 tune_grid 函数出错

RMarkdown Chunk - 不要回显最后一个表达式

r - 使用 R 在坐标系中绘制节点和边

r - 如何更改ggplot条形图中的原点线位置?

R bookdown - 封面页和附录

r - 使用 dplyr/tidyverse 进行类似扫描的操作

r - ggplot - 比例堆积面积图

r - 如何在 gganimate 动画中左对齐绘图标签?

删除中央白色圆圈?