Search code examples
rggplot2bar-chartfacet-grid

Multipanel barcharts using facetgrid in R


I have a data as follows. I want to create a multipanel chart, where the two vertical panels are site and the horizontal panel (on left), are Time, and Y ticks for each time are Name, and the count are the horizontal bar chart on X axis and finally the colour of each bars are the Active period. I tried lots of ways to write but each time I get more error. I also want to show the counts on bars.

Name | Active_period| site |Time | count
A | Last_year| north| mornings |10
A | Last_year| south| mornings |20
A | Last_year| north| evenings|45
A | Last_year| south| evenings|35
A | this_year| north| mornings |80
A | this_year| south| mornings |60
A | this_year| north| evenings|95
A | this_year| south| evenings|120
A | study_year| north| mornings |100
A | study_year| south| mornings |400
A | study_year| north| evenings|220
A | study_year| south| evenings|32
B | Last_year| north| mornings |10
B | Last_year| south| mornings |45
B | Last_year| north| evenings|25
B | Last_year| south| evenings|20
B | this_year| north| mornings |300
B | this_year| south| mornings |250
B | this_year| north| evenings|140
B | this_year| south| evenings|20
B | study_year| north| mornings |10
B | study_year| south| mornings |20
B | study_year| north| evenings|10
B | study_year| south| evenings|20

Solution

  • Here is an approach using facet_grid and a geom_col:

    library(ggplot2)
    
    ggplot(dat, aes(count, Name, fill = Active_period)) +
      geom_col(position = "dodge") +
      facet_grid(Time ~ site)
    

    DATA

    dat <- structure(list(Name = c(
      "A ", "A ", "A ", "A ", "A ", "A ", "A ",
      "A ", "A ", "A ", "A ", "A ", "B ", "B ", "B ", "B ", "B ", "B ",
      "B ", "B ", "B ", "B ", "B ", "B "
    ), Active_period = c(
      " Last_year",
      " Last_year", " Last_year", " Last_year", " this_year", " this_year",
      " this_year", " this_year", " study_year", " study_year", " study_year",
      " study_year", " Last_year", " Last_year", " Last_year", " Last_year",
      " this_year", " this_year", " this_year", " this_year", " study_year",
      " study_year", " study_year", " study_year"
    ), site = c(
      " north",
      " south", " north", " south", " north", " south", " north", " south",
      " north", " south", " north", " south", " north", " south", " north",
      " south", " north", " south", " north", " south", " north", " south",
      " north", " south"
    ), Time = c(
      " mornings ", " mornings ", " evenings",
      " evenings", " mornings ", " mornings ", " evenings", " evenings",
      " mornings ", " mornings ", " evenings", " evenings", " mornings ",
      " mornings ", " evenings", " evenings", " mornings ", " mornings ",
      " evenings", " evenings", " mornings ", " mornings ", " evenings",
      " evenings"
    ), count = c(
      10L, 20L, 45L, 35L, 80L, 60L, 95L, 120L,
      100L, 400L, 220L, 32L, 10L, 45L, 25L, 20L, 300L, 250L, 140L,
      20L, 10L, 20L, 10L, 20L
    )), class = "data.frame", row.names = c(
      NA,
      -24L
    ))