plot - R ggplot, two scales together -


i want y-axis scaled:

  1. by log10, used:

    scale_y_log10(breaks = trans_breaks("log10",function(x) 10^x))

  2. more ticks:

    scale_y_continuous(breaks=pretty_breaks(10))

but error message, there can 1 scale. there way have both of 2 scales?

if understand question right, want have y axis on log scale more ticks 1, 10, 100, etc. here's reproducible example describing tried:

library(ggplot2) library(scales) dat <- data.frame(x=1:3, y=c(2,50,10000)) ggplot(data=dat, aes(x=x, y=y)) + geom_point() +    scale_y_log10(breaks=trans_breaks("log10",function(x) 10^x)) +   scale_y_continuous(breaks=pretty_breaks(10))  ## error: scale 'y' present. adding scale 'y', replace existing scale. 

ok, error says we're allowed 1 call scale_y_something. since want log scale, let's keep scale_y_log10. our approach here depends on want achieve - work have more evenly spaced ticks along axis, if aren't nice round numbers? if so, can let pretty() add additional breaks choosing higher-than-normal value n (which ?trans_breaks promises passed pretty):

ggplot(data=dat, aes(x=x, y=y)) + geom_point() +    scale_y_log10(breaks=trans_breaks("log10", function(x) 10^x, n=20)) 

flexible, maybe, ugly. can explore trans_breaks doing applying result data (dat$y in example). note value of n taken suggestion rather command:

trans_breaks("log10", function(x) 10^x, n=2 )(dat$y) [1]     1   100 10000  trans_breaks("log10", function(x) 10^x, n=10)(dat$y) [1]     1.000     3.162    10.000    31.623   100.000   316.228  1000.000  3162.278 10000.000 

or maybe you'd prefer ticks of form 1, 3, 10, 30, 100, etc.? if so:

ggplot(data=dat, aes(x=x, y=y)) + geom_point() +    scale_y_log10(breaks=c(1,3,10,30,100,300,1000,3000,10000)) 

my guess these direct specifications best bet. if need flexibility arbitrary y range, take hint https://stackoverflow.com/a/14258838/3203184 , add labels argument:

ggplot(data=dat, aes(x=x, y=y)) + geom_point() +    scale_y_log10(breaks=trans_breaks("log10", function(x) 10^x, n=20),                  labels=trans_format("log10", math_format(10^.x))) 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -