I have the following code which produces the following plot:
#make data frame
phone <- c("p", "b", "t", "d", "k", "g", "ts", "dz", "tʃ", "dʒ", "f", "v", "s", "z")
count <- c(1088, 211, 1260, 208, 2596, 1041, 15, 13, 1262, 51, 22, 9, 765, 8)
prop <- c(0.84, 0.16, 0.86, 0.14, 0.71, 0.29, 0.53, 0.46, 0.96, 0.04, 0.71, 0.29, 0.99, 0.01)
voicing <- c("unvoiced", "voiced", "unvoiced", "voiced", "unvoiced", "voiced", "unvoiced", "voiced", "unvoiced", "voiced", "unvoiced", "voiced", "unvoiced", "voiced")
p_m <- c("Bilabial Plosive (N=1299)", "Bilabial Plosive (N=1299)", "Dental Plosive (N=1468)", "Dental Plosive (N=1468)", "Velar Plosive (N=3637)", "Velar Plosive (N=3637)", "Alveolar Affricate (N=28)", "Alveolar Affricate (N=28)", "Post-alveolar Affricate (N=1313)", "Post-alveolar Affricate (N=1313)", "Labiodental Fricative (N=31)", "Labiodental Fricative (N=31)", "Alveolar Fricative (N=773)", "Alveolar Fricative (N=773)")
consonant_df <- data.frame(phone = factor(phone, levels = phone), count, prop, voicing, p_m)
# plot
library(ggplot2)
ggplot(data = consonant_df, aes(x = phone, y = prop, fill = voicing)) +
geom_bar(stat = "identity", position = position_dodge()) +
labs(x = "Obstruent", y = "Proportion", fill = "Voicing") +
guides(fill = guide_legend(title = "Voicing")) + facet_grid(cols = vars(factor(p_m, level=c('Bilabial Plosive (N=1299)', 'Dental Plosive (N=1468)', 'Velar Plosive (N=3637)', 'Alveolar Affricate (N=28)', 'Post-alveolar Affricate (N=1313)', 'Labiodental Fricative (N=31)', 'Alveolar Fricative (N=773)'))), scales ="free") + theme(legend.position="bottom")
The text runs out of the grid label. The solution given here suggests using produces using ggplot2's facet_wrap
function.
I tried this in my code as such and got the following plot:
#with facet wrap
ggplot(data = consonant_df, aes(x = phone, y = prop, fill = voicing)) +
geom_bar(stat = "identity", position = position_dodge()) +
labs(x = "Obstruent", y = "Proportion", fill = "Voicing") +
guides(fill = guide_legend(title = "Voicing")) + facet_grid(cols = vars(factor(p_m, level=c('Bilabial Plosive (N=1299)', 'Dental Plosive (N=1468)', 'Velar Plosive (N=3637)', 'Alveolar Affricate (N=28)', 'Post-alveolar Affricate (N=1313)', 'Labiodental Fricative (N=31)', 'Alveolar Fricative (N=773)'))), scales ="free") + theme(legend.position="bottom") + facet_wrap(~ p_m, nrow = 1, labeller = label_wrap_gen(width=10))
This worked well for the grid labels, but for some reason, this added empty columns from the x-axis in each facet. How can I get rid of these empty columns?
(I also want to keep the y-axes in each face equivalent)
The issue is that you missed to add scales="free"
to facet_wrap
in your second code. Also note that facet_grid
also allows to use a labeller=
:
library(ggplot2)
consonant_df$p_m <- factor(p_m, level = c(
"Bilabial Plosive (N=1299)", "Dental Plosive (N=1468)", "Velar Plosive (N=3637)",
"Alveolar Affricate (N=28)", "Post-alveolar Affricate (N=1313)",
"Labiodental Fricative (N=31)", "Alveolar Fricative (N=773)"
))
ggplot(data = consonant_df, aes(x = phone, y = prop, fill = voicing)) +
geom_bar(stat = "identity", position = position_dodge()) +
labs(x = "Obstruent", y = "Proportion", fill = "Voicing") +
guides(fill = guide_legend(title = "Voicing")) +
facet_grid(
cols = vars(p_m),
scales = "free_x",
labeller = label_wrap_gen(width = 10)
) +
theme(legend.position = "bottom")