最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - ggfittext::geom_bar_text does not place text in centre when fontface = "bold" is involved, also values are

programmeradmin1浏览0评论

I want to use ggfittext::geom_bar_text() to plot values into a horizontal stacked bar plot with a certain value in bold font. There is a fontface aesthetic available. The problem is: As soon as I use a bold fontface, the labels are not centered anymore and the values get mixed up.

Please have a look at this MRE:

library(ggplot2)
library(ggfittext)
library(scales)

This is the case without any bold labels:

df2 <- data.frame(cat1 = rep(c("A", "B", "C"), each = 3),
                 cat2 = rep(c("D", "E", "F"), 3),
                 perc = c(0, .6, .4,
                          .26, .24, .5,
                          .3, .3, .4),
                 fface = "plain")

ggplot(df2, aes(y = cat1, fill = cat2, x = perc, fontface = fface)) +
  geom_col() +
  geom_bar_text(aes(label = percent(perc, accuracy = .1),
                    fontface = fface),
                position = "stack", place = "centre")

Now for the problematic case:

df <- data.frame(cat1 = rep(c("A", "B", "C"), each = 3),
                 cat2 = rep(c("D", "E", "F"), 3),
                 perc = c(0, .6, .4,
                          .26, .24, .5,
                          .3, .3, .4),
                 fface = c("plain", "bold", "plain",
                           "bold", "plain", "plain",
                           "plain", "plain", "bold"))

ggplot(df, aes(y = cat1, fill = cat2, x = perc, fontface = fface)) +
  geom_col() +
  geom_bar_text(aes(label = percent(perc, accuracy = .1),
                    fontface = fface),
                position = "stack", place = "centre")

What is happening? df is exactly identical except for the three bold cases in column fface and the ggplot() calls are also exactly identical. But the labels for cat1 == "C" are off and they are switched, too.

Am I missing something? Is this a bug? Thank you for your help!

I want to use ggfittext::geom_bar_text() to plot values into a horizontal stacked bar plot with a certain value in bold font. There is a fontface aesthetic available. The problem is: As soon as I use a bold fontface, the labels are not centered anymore and the values get mixed up.

Please have a look at this MRE:

library(ggplot2)
library(ggfittext)
library(scales)

This is the case without any bold labels:

df2 <- data.frame(cat1 = rep(c("A", "B", "C"), each = 3),
                 cat2 = rep(c("D", "E", "F"), 3),
                 perc = c(0, .6, .4,
                          .26, .24, .5,
                          .3, .3, .4),
                 fface = "plain")

ggplot(df2, aes(y = cat1, fill = cat2, x = perc, fontface = fface)) +
  geom_col() +
  geom_bar_text(aes(label = percent(perc, accuracy = .1),
                    fontface = fface),
                position = "stack", place = "centre")

Now for the problematic case:

df <- data.frame(cat1 = rep(c("A", "B", "C"), each = 3),
                 cat2 = rep(c("D", "E", "F"), 3),
                 perc = c(0, .6, .4,
                          .26, .24, .5,
                          .3, .3, .4),
                 fface = c("plain", "bold", "plain",
                           "bold", "plain", "plain",
                           "plain", "plain", "bold"))

ggplot(df, aes(y = cat1, fill = cat2, x = perc, fontface = fface)) +
  geom_col() +
  geom_bar_text(aes(label = percent(perc, accuracy = .1),
                    fontface = fface),
                position = "stack", place = "centre")

What is happening? df is exactly identical except for the three bold cases in column fface and the ggplot() calls are also exactly identical. But the labels for cat1 == "C" are off and they are switched, too.

Am I missing something? Is this a bug? Thank you for your help!

Share Improve this question asked Mar 14 at 15:07 swolfswolf 1,1657 silver badges23 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

That's simply a matter of the default groupìng which in your case also takes the fontface into account. To fix you issue map explicitly on the group= aes to group and stack by the fill variable only:

library(ggplot2)
library(ggfittext)
library(scales)

ggplot(df, aes(y = cat1, fill = cat2, x = perc)) +
  geom_col() +
  geom_bar_text(
    aes(
      label = percent(perc, accuracy = .1),
      fontface = fface,
      group = cat2
    ),
    position = "stack", place = "centre"
  )

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论