Introduction to Horizontal Bar Plots and Sectioning
In this article, we will explore how to create a horizontal bar plot with grouped data and add section titles between tick labels in R using the ggplot2 library.
Background on ggplot2 and Facet Grid
ggplot2 is a powerful data visualization library for R that provides a consistent grammar of graphics. The facet grid function allows us to divide plots into multiple panels or facets, which are useful for comparing groups within a dataset.
Creating a Horizontal Bar Plot with Grouped Data
To create a horizontal bar plot with grouped data, we first need to prepare our data in the correct format. In this case, we have a dataframe called status
that contains three columns: varname
, type
, and percent
.
library(ggplot2)
# Create a sample dataset
questions <- str_wrap(c('Blah blah blah blah blah blah B?',
'Blbbity blah blibbity blah C?',
'Blah blah blibbity blah blah blah D?',
'Blah blah blah A?',
'Blah blah blah blibbity E?',
'Blah blah blibbity blah I?'),15)
status <- data.frame(matrix(data=NA, nrow=18,ncol=3))
names(status) <- c('varname','type','percent')
status['varname'] <- factor(c(rep(1,3),rep(2,3),rep(3,3),rep(4,3),rep(5,3),rep(6,3)),labels=questions)
status['type'] <- c(rep(c('Cohabiting','Married','Divorced'),6))
status['percent'] <- c(rnorm(18,.5,.2))
# Create a horizontal bar plot with grouped data
ggplot(status, aes(varname, percent)) +
geom_col(aes(fill = type), position = "dodge", stat="identity") + coord_flip() +
scale_fill_manual(values=c('cadetblue1','cadetblue3','darkcyan')) +
ggtitle('By couple type') + labs(y='Percent') + ylim(0,1)
Sectioning the Plot with Facet Grid
To add section titles between tick labels in our horizontal bar plot, we can use the facet grid function. However, simply using facet_grid()
will create separate panels for each category, which is not what we want.
Instead, we need to use the scales = 'free_y'
argument to allow the y-axis to be scaled separately for each panel, and then use the space = 'free_y'
argument to make sure that the space between panels is consistent. We also need to remove the background color of the facet strip labels using strip.background.y = element_blank()
.
library(dplyr)
library("ggplot2")
library("stringr")
# Create a new alp variable
status <- status %>%
# edit: shamelessly steal from Maurits's answer :-)
mutate(alp = if_else(str_detect(varname, "(I|E|A)\\?$"), "Vowels", "Consontants"))
# Create a horizontal bar plot with grouped data and section titles
ggplot(status, aes(varname, percent)) +
geom_col(aes(fill = type), position = "dodge") +
facet_grid(alp ~ ., scales = 'free_y', space = 'free_y', switch = 'y') +
coord_flip() +
scale_fill_manual(values = c("cadetblue1", "cadetblue3", "darkcyan"),
# increase the spacing between legend key text
labels = stringr::str_pad(status$type, 5, "right"),) +
ggtitle("By couple type") + labs(y = "Percent") +
scale_x_discrete(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 1)) +
theme_classic(base_size = 14, base_family = 'mono') +
theme(axis.title.y = element_blank(),
legend.spacing.x = unit(0.25, unit = "cm"),
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
legend.position = "top") +
theme(panel.grid.minor.x = element_blank()) +
# switch the facet strip label to outside
theme(strip.placement = 'outside',
strip.text.y = element_text(face = 'bold'),
strip.background.y = element_rect(colour = NA, fill = 'grey80'))
Conclusion
In this article, we have explored how to create a horizontal bar plot with grouped data and add section titles between tick labels using the ggplot2 library. We discussed the importance of preparing our data in the correct format and using the facet grid function correctly to achieve the desired layout.
We also provided an example code that demonstrates how to create a horizontal bar plot with grouped data and section titles, along with explanations of each step and the relevant arguments used in the ggplot2 function calls.
Last modified on 2025-01-26