R ggplot 堆积条顶部的字符标签具有透明背景,而不是白色背景

Character labels on top of R ggplot stacked bar have transparent background, instead of white background

我正在尝试使用 R 和 ggplot 堆叠条显示特定语料库中音素的相对频率。我无法在带有白色背景和黑色线条的条形图顶部显示每个音素的标签。标签显示为透明背景!

这是我正在使用的data.frame:

phones  <- c("i", "e", "ə", "a", "o", "u", "p", "t̪", "t", "t͡ʃ", "ʈ͡ʂ", "k", "m", "n̪", "n", "ɲ", "ŋ", "f", "θ", "s", "ʃ", "ɻ", "j", "ɣ", "w", "l̪", "l", "ʎ")
mode    <- c(rep("vowel", 6), rep("stop", 3), rep("affricate", 2), "stop", rep("nasal", 5), rep("fricative", 4), rep("approximant", 4), rep("lateral", 3))
freq    <- c(3629, 6301, 8967, 7204, 2403, 5120, 2798, 74, 2095, 1359, 1767, 5233, 3059, 353, 9769, 1439, 2061, 1994, 1338, 97, 353, 1652, 1597, 361, 3435, 409, 3731, 1378)
data <- data.frame(phones, mode, freq)

这是我用来生成图表的代码。

library(ggplot2) #for ggplot
ggplot(data, aes(fill = reorder(phones, freq), y = freq, x = mode, label = phones)) + 
  geom_bar(position = "fill", stat = "identity", colour = "white") +
  xlab("") +
  ylab("") +
  labs(title = "Frecuencia de fonemas del diccionario Augusta", fill = "Phones", caption  = "Elaboración propia" ) +
  scale_y_continuous(breaks = seq(0, 1, 0.25), labels = function(x){paste0(x * 100,  "%") } ) +
  coord_flip() +
  geom_label(position = position_fill(vjust = 0.5), colour="black", size = 2) +
  theme(legend.position="None") + 
  labs(subtitle="")

对于正确显示标签(白色背景 + 黑色文本和线条)的任何帮助,我将不胜感激。

实际上,事实证明您的标签背景不透明;它们与条形颜色相同。 (我们可以在 [t̪] 的标签中看到这一点,它超出了其栏的边界。)这是因为 geom_label 继承了 fill 美学。

我们可以通过指定 fill = "white" 强制标签的填充颜色为不同的颜色。如果我们这样做,我们还必须明确指定 group 美学,以便 position_fill() 将标签放在正确的位置。

ggplot(data, aes(fill = reorder(phones, freq), group = reorder(phones, freq),
                 y = freq, x = mode, label = phones)) +
  geom_bar(position = "fill", stat = "identity", colour = "white") +
  xlab("") +
  ylab("") +
  labs(title = "Frecuencia de fonemas del diccionario Augusta", fill = "Phones", caption  = "Elaboración propia" ) +
  scale_y_continuous(breaks = seq(0, 1, 0.25), labels = function(x){paste0(x * 100,  "%") } ) +
  coord_flip() +
  geom_label(position = position_fill(vjust = 0.5), fill = "white", colour="black", size = 2) +
  theme(legend.position="None") +
  labs(subtitle="")