使用 ggplot,使用 scales 中的 unit_format 和 Dollar_format 进行刻度文本标记

Can*_*ice 3 r ggplot2

我创建了以下 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"))
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

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

一如既往的感谢!

Rom*_*man 6

Hacky,但有效:

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"))
Run Code Online (Sandbox Code Playgroud)