简体   繁体   English

在R中使用$和K作为数千美元的y轴标签

[英]in R use `$` and `K` as a y-axis labels for thousands of dollars

This older stackoverflow question explains how to change your y-axis to K for thousands instead of ,000 . 这个较早的stackoverflow问题说明了如何将y轴从数千更改为K而不是,000 It also explains how to put a dollar sign on the y-axis labels with scales::dollar . 它还说明了如何使用scales::dollar在y轴标签上放置美元符号。 My trouble is in combining the two ideas in R: 我的麻烦是将R中的两个想法结合起来:

library(tidyverse)
library(scales)
set.seed(200)
df <- tibble(Date = seq(as.Date("2018/1/1"), by = "month", length.out = 12),
             Values = c(runif(12, 200000, 800000)))

ggplot(df, aes(Date, Values)) + 
  geom_line() + 
  scale_y_continuous(label = unit_format(unit = "K", scale = 1e-3, sep = "")) + 
  # scale_y_continuous(labels = dollar) +  # line 10
  NULL

I can use either line 9 or line 10 above, but not both simultaneously. 我可以使用上面的第9行第10行,但不能同时使用。 How do I incorporate both line 9 and 10 into a single line? 如何将第9行和第10行都合并到一行中? I want to transfer, as an example, 115,000 into $115K . 例如,我想将115,000转移到$115K

You can use scales::dollar_format to achieve what you're going for: 您可以使用scales::dollar_format实现目标:

ggplot(df, aes(Date, Values)) + 
  geom_line() + 
  scale_y_continuous(labels = scales::dollar_format(scale = .001, suffix = "K"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM