To get ggplot
为了在对数刻度上正确绘制小断点,我必须这样做:
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))
有没有更简单的方法来实现这一目标?
最终结果应如下所示:
这是我对该问题的解决方案:
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
它与您已经做过的非常相似,但普遍适用。
还有一个小改进:它将在您的示例中扩展 1e-6 以下和 1e-1 以上的小中断。
我是从看函数开始的trans_break
并将其简化为最基本的要素。
还值得考虑annotation_logticks() 函数:
myplot+annotation_logticks(side="b")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)