Tuesday, 15 March 2011

r - How to transform ggplot2 axis by constant factor? -



r - How to transform ggplot2 axis by constant factor? -

i need modify scale of y-axis in ggplot2 graphic : want express y-axis in thousands , not in units. example, labels have 0 ; 1,000 ; 2,000 ; 3,000 instead of 0 ; 1000000 ; 2000000 ; 3000000.

please, don't tell me split info 1000 !

my question same ggplot2 axis transformation constant factor. solution provided here modifies lables parameter of scale_y_continuous function, whereas need parameter set comma. solution next breaks : 0 ; 1000 ; 2000 ; 3000 ... breaks expressed in thousands , not in millions , point, loose comma labels. want see 1,000 ; 2,000 ; 3,000 , not 1000 ; 2000 ; 3000...

so modifying lables parameter of scale_y_continuous function isn't useful. that's why think have work trans parameter of scale_y_continuous function instead of labels parameter.

there lot of built-in transformation match trans parameter , solve similar problems in scales bundle (look @ log_trans example). tried build own homothetic transformation, code below.

library(ggplot2) var0 <- c(1:100) var1 <- 1000000*rnorm(100) homothetic_breaks<- function (n = 5, base of operations = 1000) { function(x) { rng <- (range(x, na.rm = true)/base) min <- floor(rng[1]) max <- ceiling(rng[2]) if (max == min) return(base*min) <- floor((max - min)/n) + 1 base*seq(min, max, = by) } } homothetic_trans <- function(base = 1000) { trans <- function(x) x/base inv <- function(x) x*base trans_new(paste0("diviseur_par_", format(base)), trans, inv, homothetic_breaks(base=base), domain = c(-inf, inf)) } info <- data.frame(var0,var1) p <- ggplot(data,aes(var0,var1))+geom_path() p <- p + scale_y_continuous(trans=homothetic_trans,labels = comma) p

when run code next message : "error: input str_c should atomic vectors", , breaks of y axis arethe same ones when run next code :

library(ggplot2) var1 <- 1000*rnorm(100) var0 <- c(1:100) info <- data.frame(var0,var1) p <- ggplot(data,aes(var0,var1))+geom_path() p

r ggplot2 scale

No comments:

Post a Comment