【问题标题】:Data labels text size using plot missing使用缺少绘图的数据标签文本大小
【发布时间】:2019-02-26 17:32:23
【问题描述】:

我正在使用函数 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)))

【问题讨论】:

  • 使用 v0.8,您可以这样做:plot_missing(df, geom_label_args = list(size = 2, label.padding = unit(0.1, "lines")))

标签: r ggplot2


【解决方案1】:

可能有更优雅的方法可以做到这一点,但这里我修改了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)
}

我在geom_text() 函数中添加了size = 2

新的plot_missing_smaller_labels函数调用如下:

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

这会提供具有较小文本大小的标签。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多