bispire
bispire

Reputation: 35

how to create a proper geom_boxplot?

I am trying to create a regular boxplot chart, but am getting a weird dotted chart instead.

Could you please point out to me what I am doing wrong and how I can correct it?

Thank you very much.

box_plot <- comb_rpt %>% 
filter(!is.na(oracle_contract_desc.x), 
     service_mnth %in% c('2018-01', '2018-02', '2018-03', '2018-04',
                         '2018-05', '2018-06', '2018-07')) %>% 
ggplot(aes(x = service_mnth, y = var_est_to_actual)) +
geom_boxplot()

My Weird Boxplot looks like a dot:

img

Correct Boxplot looks like an actual box, with color fill

img

Upvotes: 1

Views: 346

Answers (1)

Maurits Evers
Maurits Evers

Reputation: 50678

My guess is you've got a lot of 0s in var_est_to_actual per service_mnth.

Let's reproduce the "issue".

First off, we generate data from a wide normal and show the boxplots.

set.seed(2018)
df <- setNames(data.frame(
    rnorm(100, sd = 100),
    rnorm(100, sd = 100)), c("2018-01", "2018-02"))    

library(tidyverse)
df %>%
    gather(service_mnth, var_est_to_actual) %>%
    ggplot(aes(service_mnth, var_est_to_actual)) +
    geom_boxplot()

enter image description here

We now replace 70% of the observations per service_mnth with 0s, and show the boxplot again for the revised data.

df %>%
    gather(service_mnth, var_est_to_actual) %>%
    group_by(service_mnth) %>%
    mutate(frac = (1:n()) / n()) %>%
    mutate(var_est_to_actual = if_else(frac < 0.7, 0, var_est_to_actual)) %>%
    ggplot(aes(service_mnth, var_est_to_actual)) +
    geom_boxplot()

enter image description here

Notice the similarity with the boxplot you're showing.

Upvotes: 2

Related Questions