rr-data-explorer

DataExplorer plot_bar


I really like this package and the ability to quickly plot several graphs with a one-liner. However, I can't seem to get the plot_bar function to label the data with counts and I wonder if someone knows if this is possible?

library(DataExplorer)

data <- structure(list(PassengerId = c("0001_01", "0002_01", "0003_01", 
"0003_02", "0004_01", "0005_01"), HomePlanet = c("Europa", "Earth", 
"Europa", "Europa", "Earth", "Earth"), CryoSleep = c("False", 
"False", "False", "False", "False", "False"), Cabin = c("B/0/P", 
"F/0/S", "A/0/S", "A/0/S", "F/1/S", "F/0/P"), Destination = c("TRAPPIST-1e", 
"TRAPPIST-1e", "TRAPPIST-1e", "TRAPPIST-1e", "TRAPPIST-1e", "PSO J318.5-22"
), Age = c(39, 24, 58, 33, 16, 44), VIP = c("False", "False", 
"True", "False", "False", "False"), RoomService = c(0, 109, 43, 
0, 303, 0), FoodCourt = c(0, 9, 3576, 1283, 70, 483), ShoppingMall = c(0, 
25, 0, 371, 151, 0), Spa = c(0, 549, 6715, 3329, 565, 291), VRDeck = c(0, 
44, 49, 193, 2, 0), Name = c("Maham Ofracculy", "Juanna Vines", 
"Altark Susent", "Solam Susent", "Willy Santantines", "Sandie Hinetthews"
), Transported = c("False", "True", "False", "False", "True", 
"True")), row.names = c(NA, 6L), class = "data.frame")

plot_bar(data, by = "HomePlanet")

The other arguments I can find all seem to just adjust the theme but I want this (upper left corner but for all the plots) enter image description here

Instead of this enter image description here


Solution

  • There doesn't seem to be an option to do this from within the function. It looks like you'll need to add the text layer yourself:

    plot_bar(data, by = "HomePlanet")$page_1 +
      geom_text(aes(label = after_stat(y)), position = position_fill(0.5))
    

    enter image description here