2012-08-13 57 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