如何控制条形图中值标签的小数位

How to control decimal places for value labels in bar charts

考虑下图:

这个问题是关于如何将堆叠条形图中的所有值标签保留一位小数,以保持一致性。因此,将-1、2和5表示为-1.0、2.0和5.0与其他一致。这是一个示例数据

df  <- data.frame(group=c("satisfied", "satisfied",
                      "unsatisfied","unsatisfied",
                      "satisfied", "satisfied", 
                      "unsatisfied","unsatisfied"
                      ), 
                cost=c("low","high","low","high",
                      "low","high","low","high"),     
                treatment=c("treated","treated","treated","treated",
                            "untreated","untreated",
                      "untreated","untreated") ,
                value=c(2.3,8.7,5.0,3.1,9.4,3.1,2.0,-1.0)) 

生成图形的代码是

#REORDER
df$group <- factor(df$group, levels = c("satisfied", 
                            "unsatisfied"))

ggplot(data=df,aes(y = value, x = group, fill = cost)) +
geom_bar(stat="identity",position='stack') + 
ylab("Y label") +
theme(legend.direction = "horizontal",legend.position = "bottom",
    legend.spacing.x = unit(0.1, 'cm'))+
theme(legend.title=element_blank())+
geom_text(aes(label = ifelse(value !=0, value, "")), 
         position = position_stack(vjust=0.5))+
facet_grid( ~ treatment)

之后,我试图通过引入

来解决这个问题
sprintf("%0.1f", round(value, digits = 2))

进入 ggplot 函数,但这不会产生所需的输出。我非常感谢对此的任何帮助。

问题是您使用 sprintf 在 之前 舍入(并因此转换为 character,丢失数字信息)。要格式化数字,您需要提供 sprintf 个数字,而不是字符串。它将自行处理舍入。试试这个:

label = ifelse(value !=0, sprintf("%0.1f", value), "")

制作整个代码:

ggplot(data = df, aes(y = value, x = group, fill = cost)) +
  geom_bar(stat = "identity", position = 'stack') +
  ylab("Y label") +
  theme(
    legend.direction = "horizontal",
    legend.position = "bottom",
    legend.spacing.x = unit(0.1, 'cm')
  ) +
  theme(legend.title = element_blank()) +
  geom_text(aes(label = ifelse(value !=0, sprintf("%0.1f", value), "")),
            position = position_stack(vjust = 0.5)) +
  facet_grid(~ treatment)


由于ifelse,上面的内容有点奇怪。更标准的 ggplot2 解决方案会让您以另一种方式摆脱 0 - 也许在绘图之前将其过滤掉,将 data = filter(df, y != 0) 提供给 ggplot()。然后你可以使用scales函数

label = scales::label_number(accuracy = 0.1)(value)

整个代码如下,结果相同:

ggplot(data = dplyr::filter(df, value != 0), aes(y = value, x = group, fill = cost)) +
  geom_bar(stat = "identity", position = 'stack') +
  ylab("Y label") +
  theme(
    legend.direction = "horizontal",
    legend.position = "bottom",
    legend.spacing.x = unit(0.1, 'cm')
  ) +
  theme(legend.title = element_blank()) +
  geom_text(aes(label = scales::label_number(accuracy = 0.1)(value)),
            position = position_stack(vjust = 0.5)) +
  facet_grid(~ treatment)