Remove variable section from faceted bar chart

42 views Asked by At

Every time I make a fasted bar chart, if there is a section with no data ggplot will still make a spot for it in its aim for symmetry and thus it looks like these variables were tested and received a value of 0 which is not the case. Is there a way to override ggplot. Below is an example where the section 8 A3 looks like it record a 0 value but actually it was not investigated in the data set.

library(ggplot2)
library(dplyr)

# Your data
data <- data.frame(A = c(1, 2, 3, 1, 2, 3, 1, 2, 1, 2), 
                   B = c(5, 34, 5, 34, 34, 56, 3, 3, 6, 35), 
                   C = c("Light","Light", "Light", "Dark", "Dark", "Dark", "Light","Light", "Dark", "Dark"), 
                   D = c(4, 4, 4, 4, 4, 4, 8, 8, 8, 8))


# Create plots
Plot1 <- ggplot(data, aes(x = A, y = B, fill = C)) +
  geom_bar(stat = "identity", position = position_dodge2(preserve = "single"))  +
  theme_minimal() + theme(legend.position = "right") +
  facet_wrap(~D)

Plot1

enter image description here

2

There are 2 answers

0
David Hodge On BEST ANSWER

scales = "free_x" drops the unused x value

If you want to keep all the bars the same size use facet_grid with the space argument.

Solution provided below in ggplot2 - and also ggblanket

library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

# Your data
data <- data.frame(A = c(1, 2, 3, 1, 2, 3, 1, 2, 1, 2), 
                   B = c(5, 34, 5, 34, 34, 56, 3, 3, 6, 35), 
                   C = c("Light","Light", "Light", "Dark", "Dark", "Dark", "Light","Light", "Dark", "Dark"), 
                   D = c(4, 4, 4, 4, 4, 4, 8, 8, 8, 8))


# Create plots
Plot1 <- ggplot(data, aes(x = A, y = B, fill = C)) +
  geom_bar(stat = "identity", position = position_dodge2(preserve = "single"))  +
  theme_minimal() + 
  theme(legend.position = "right") +
  facet_grid(.~D, scales = "free_x", space = "free_x") +
  scale_x_continuous(breaks = 1:3)

Plot1


library(ggblanket)

data |> 
  gg_col(
    x = A, 
    y = B, 
    col = C,
    facet = D,
    position = position_dodge2(preserve = "single"),
    facet_layout = "grid",
    facet_scales = "free_x",
    facet_space = "free_x",
    x_breaks = 1:3,
  )
#> Warning in gg_blanket(data = data, geom = "col", stat = stat, position = position, : Ignoring unknown parameters: `contour`
#> Ignoring unknown parameters: `contour`

Created on 2024-02-24 with reprex v2.1.0

0
stefan On

Use scales="free_x" in facet_wrap to drop unused categories:

library(ggplot2)

ggplot(data, aes(x = factor(A), y = B, fill = C)) +
  geom_bar(
    stat = "identity",
    position = position_dodge2(preserve = "single")
  ) +
  theme_minimal() +
  theme(legend.position = "right") +
  facet_wrap(~D, scales = "free_x")