r - Plotting minor breaks on a log scale with ggplot -
to ggplot
plot minor breaks correctly on logarithmic scale, had thing:
faceplant1 <- function(x) { return (c(x[1]*10^.25, x[2]/10^.25)) } faceplant2 <- function(x) { return (rep(seq(1,9),5)*rep(10^seq(-6,-2), each=9)) } ggplot(mydata, aes(x=myseries)) + geom_density() + scale_x_log10(limits=c(1e-6, 1e-1), breaks=10^seq(-6,-1), minor_breaks=trans_breaks(faceplant1, faceplant2, n=45))
is there simpler way achieve this?
the end result should like:
here's solution problem:
library(ggplot2) log10_minor_break = function (...){ function(x) { minx = floor(min(log10(x), na.rm=t))-1; maxx = ceiling(max(log10(x), na.rm=t))+1; n_major = maxx-minx+1; major_breaks = seq(minx, maxx, by=1) minor_breaks = rep(log10(seq(1, 9, by=1)), times = n_major)+ rep(major_breaks, each = 9) return(10^(minor_breaks)) } } mydata = data.frame(myseries = 10^(rnorm(1e4, mean=0, sd=0.5))) myplot = ggplot(mydata, aes(x=myseries))+ geom_density()+ scale_x_log10(minor_breaks=log10_minor_break())+ theme(panel.grid.major.x = element_line(size=1.0), panel.grid.minor.x = element_line(size=2)) myplot
it similar you've done applies generally. , minor improvement: expand minor breaks below 1e-6 , above1e-1 in example.
i have started looking @ function trans_break
, reduced fundamental element.
it worth considering annotation_logticks() function:
myplot+annotation_logticks(side="b")
Comments
Post a Comment