2012-08-13 55 views
5

我注意到,使用scales包,可以使用内部的scales = dollar选项在轴上显示美元,例如scale_y_log10()。像scales = euro这样的选项似乎缺乏。有没有一种简单的方法可以达到同样的效果?ggplot中的欧元符号:秤包

回答

12

很容易修改dollar_format并将符号更改为欧元。运行这个并把它放在这样的代码,你会打电话dollar_format

euro_format <- function(largest_with_cents = 100000) { 
    function(x) { 
    x <- round_any(x, 0.01) 
    if (max(x, na.rm = TRUE) < largest_with_cents & 
     !all(x == floor(x), na.rm = TRUE)) { 
     nsmall <- 2L 
    } else { 
     x <- round_any(x, 1) 
     nsmall <- 0L 
    } 
    str_c("€", format(x, nsmall = nsmall, trim = TRUE, big.mark = ",", scientific = FALSE, digits=1L)) 
    } 
} 
+3

非常感谢,这工作正常!对于那些仍然感到困惑的人来说,除了'ggplot'和'scales'之外,您还需要加载'reshape'和'stringr'包来完成这个工作。 – 2012-08-14 06:29:25

+1

在有限的测试中,我发现以下工作,它只依赖于''ggplot2''和'''':''euroFrance < - function(x)paste0(format(x,big.mark =“ “,decimal.mark =”,“,trim = TRUE, scientific = FALSE),”€“)}''(我选择了法国风格与Luciano的例子进行对比,并展示了可能性的范围)。或者换句话说,如果你不需要担心舍入或修整小数点,一个简单的函数就可以做到。哦,你会这样称呼它:''+ scale_y_continuous(labels = euroFrance,breaks = etc.'' – PatrickT 2014-12-20 18:08:27

6

您可以使用dollar_format

prefixsuffix参数例如像这样:

library(ggplot2) 
library(scales)  
ggplot(diamonds) + geom_point(aes(x = carat, y = price)) + scale_y_continuous(labels = dollar_format(suffix = "€", prefix = "")) 
+0

我得到一个错误,不是所有的字符都在〜/ R/stuff.Rmd中使用ISO8859-1进行编码要使用不同的编码进行保存,请从主菜单中选择“File | Save with Encoding ...”。您对解决方案的建议是什么编码? – DaveRGP 2016-03-30 13:22:25

+0

这对我很有用。 – andrii 2018-02-13 18:36:28