Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Euro-sign in ggplot: scales package

Tags:

r

ggplot2

I've noticed that, using the scales package, one can display dollars on an axis, using the scales = dollar option inside, for instance, scale_y_log10(). An option like scales = euro seems to be lacking. Is there an easy way for me to achieve the same effect?

like image 713
Gerrit Jan Avatar asked Aug 13 '12 13:08

Gerrit Jan


Video Answer


2 Answers

You can use the prefix and suffix arguments of dollar_format

For example like this:

library(ggplot2)
library(scales)       
ggplot(diamonds) + geom_point(aes(x = carat, y =  price)) + 
   scale_y_continuous(labels = dollar_format(suffix = "€", prefix = ""))
like image 135
fc9.30 Avatar answered Sep 29 '22 15:09

fc9.30


It's easy to modify dollar_format and change the symbol to euro. Run this and put it in the code like you would call 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))
  }
}
like image 28
Luciano Selzer Avatar answered Sep 29 '22 15:09

Luciano Selzer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!