R.Sav
R.Sav

Reputation: 73

Data labels text size using plot missing

I am using the function plot_missing to show the number of NAs in my dataset. Since my dataset has 50 variables I need to adjust the text size. I managed to change the text size of the axis but not of the data labels. Any suggestions?

Using a sample dataset:

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)))

Upvotes: 4

Views: 500

Answers (1)

makeyourownmaker
makeyourownmaker

Reputation: 1833

There are probably more elegant ways to do this, but here I've modified the plot_missing function:

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)
}

I've added size = 2 in the geom_text() function.

The new plot_missing_smaller_labels function is called like so:

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

Which gives labels with smaller text size.

Upvotes: 1

Related Questions