You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在scale_y_continuous中设置德语区域获取数值缩写?

解决ggplot2德语数值标签缩写问题

要将y轴的"million"显示为"Mil."、"billion"显示为"Mrd.",有两种可靠的实现方式:

方法1:利用locale参数(依赖系统区域支持)

直接在scales::label_number()中指定德语区域,结合长刻度截断规则,前提是你的R环境已支持de_DE locale:

df_example <- tibble::tibble(
  year = c(2016:2023),
  tn = c(10:17),
  cost2 = c(10:17),
  cost = c(100, 1000, 10000, 100000, 1000000, 10000000, 20000000, 10000000000)
)

df_example |>
  ggplot2::ggplot(aes(y= cost, x= year)) +
  geom_bar(stat = "identity") +
  scale_y_continuous(labels = scales::label_number(
    locale = "de_DE",
    scale_cut = scales::cut_long_scale()
  ))

如果运行报错,说明系统未配置德语locale:

  • Linux:执行sudo locale-gen de_DE.UTF-8后重启R
  • Windows:在系统「区域设置」中添加德语(德国)并重启

方法2:自定义截断规则(无需系统依赖)

完全手动指定截断阈值和对应缩写,灵活性更高,不受系统locale限制:

# 定义德语风格的数值截断规则
de_cut <- scales::cut_short_scale(
  breaks = c(1e3, 1e6, 1e9, 1e12),
  labels = c("Tsd.", "Mil.", "Mrd.", "Bil.")
)

df_example |>
  ggplot2::ggplot(aes(y= cost, x= year)) +
  geom_bar(stat = "identity") +
  scale_y_continuous(labels = scales::label_number(scale_cut = de_cut))

这种方式可以根据需求自由调整截断点和缩写文本,确保完全符合预期。

内容的提问来源于stack exchange,提问作者Philipp Schulz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 04:40:14