首页 > 解决方案 > 使用 ggplot,使用刻度中的 unit_format 和 Dollar_format 来标记文本

问题描述

我创建了以下 ggplot 来突出我的问题:

mydf = data.frame(x = c(1,2,3,4,5), y = c(1,2,3,4,5))
ggplot(data = mydf) + 
  geom_point(aes(x = x, y = y)) + 
  scale_x_continuous(labels = scales::dollar_format()) + 
  scale_y_continuous(labels = scales::unit_format(unit = "M"))

它给出了以下惊人的高级 ggplot 图:

在此处输入图像描述

我的问题很简单——我怎样才能使一个轴同时具有 $ 和 M 单位标签,以便标签显示为 $1M $2M 等。这可能吗?是否也可以减少数字和M符号之间的差距,使其显示5M而不是5 M

一如既往的感谢!

标签: rggplot2

解决方案


哈克,但有效:

ggplot(data = mydf) + 
  geom_point(aes(x = x, y = y)) + 
  scale_x_continuous(labels = scales::dollar_format()) + 
  scale_y_continuous(labels = scales::dollar_format(prefix="$", suffix = "M"))

推荐阅读