Home > Mobile >  correctly displaying columns with subgrouping using geom_col
correctly displaying columns with subgrouping using geom_col

Time:09-16

I have a data set df1 with 4 columns (see below). I want to visualize the data by temperature (x-axis) and number of tests done at each temperature (y-axis). I also want to see the banding of tests done within each temperature level. I simply can't get it to work:

library(tidyverse)
library(lubridate)

df1 <- tibble::tribble(
  ~date,   ~type2, ~temp, ~tests,
  "09/06/2021", "band_1",  71.5,  4136L,
  "09/06/2021", "band_1",    72, 39656L,
  "09/06/2021", "band_2", 72.25,  1500L,
  "09/06/2021", "band_3",  72.5, 76134L,
  "09/06/2021", "band_1",  72.5, 13715L,
  "09/06/2021", "band_1", 72.75, 10322L,
  "09/06/2021", "band_3", 72.75,  1847L,
  "09/06/2021", "band_1", 72.75,  2277L,
  "09/06/2021", "band_2",    73,    20L,
  "09/06/2021", "band_2",    73,  1836L,
  "09/06/2021", "band_3",    73,  5958L,
  "09/06/2021", "band_1",    73,  5207L,
  "09/06/2021", "band_3",    73, 13542L,
  "09/06/2021", "band_2", 73.25,  1155L,
  "09/06/2021", "band_1", 73.25,   183L,
  "09/06/2021", "band_2", 73.25,  3188L,
  "09/06/2021", "band_3", 73.25,   918L,
  "09/06/2021", "band_2",  73.5,  5342L,
  "09/06/2021", "band_3",  73.5, 16677L,
  "09/06/2021", "band_3",  73.5,   542L,
  "09/07/2021", "band_3", 71.75,  5205L,
  "09/07/2021", "band_2", 71.75,  5444L,
  "09/07/2021", "band_1",    72, 13209L,
  "09/07/2021", "band_3",    72,  2145L,
  "09/07/2021", "band_1",    72,  2315L,
  "09/07/2021", "band_3",    72,    57L,
  "09/07/2021", "band_3", 72.25,   281L,
  "09/07/2021", "band_1", 72.25,   576L,
  "09/07/2021", "band_3", 72.25,   645L,
  "09/07/2021", "band_2",  72.5, 14813L,
  "09/07/2021", "band_3",  72.5,  1964L,
  "09/07/2021", "band_1",  72.5,  1536L,
  "09/07/2021", "band_2",  72.5,  5168L,
  "09/07/2021", "band_3",  72.5,  1986L,
  "09/07/2021", "band_1", 72.75,   990L,
  "09/07/2021", "band_3", 72.75,  2072L,
  "09/07/2021", "band_1", 72.75,  1500L,
  "09/07/2021", "band_2",    73,  3200L,
  "09/07/2021", "band_3",    73, 10923L,
  "09/07/2021", "band_1",    73,  1950L
) %>% 
  mutate(date = mdy(date))


# this is not working
df1 %>% 
  ggplot(aes(x = temp, y = tests) )  
  geom_col(aes(fill = type2), width = 1)  
  facet_grid(.~date, scales = "free_x")

CodePudding user response:

Removing width should make it. Are you looking for this solution?

df1 %>% 
    ggplot(aes(x = temp, y = tests) )  
    geom_col(aes(fill = type2))  
    facet_grid(.~date, scales = "free_x")

enter image description here

  • Related