ggplot2:向分面网格标签添加回车符,而不生成额外的列

问题描述 投票:0回答:1

我有以下代码,可以生成以下图:

#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")

enter image description here

文本超出网格标签。 此处给出的解决方案建议使用 ggplot2 的

facet_wrap
函数生成。

我在我的代码中尝试了这个,得到了以下情节:

#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))

enter image description here

这对于网格标签效果很好,但由于某种原因,这在每个方面从 x 轴添加了空列。我怎样才能摆脱这些空列?

(我还想保持每个面上的 y 轴相等)

r ggplot2 facet-wrap facet-grid
1个回答
0
投票

问题是您错过了在第二个代码中将

scales="free"
添加到
facet_wrap
。另请注意,
facet_grid
还允许使用
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")

© www.soinside.com 2019 - 2024. All rights reserved.