gpt4 book ai didi

Rlattice xyplot与网格与轴的刻度不匹配(不是多重图)

转载 作者:行者123 更新时间:2023-12-03 02:37:54 25 4
gpt4 key购买 nike

我正在尝试通过以下代码使用lattice的xyplot生成一个图:

set.seed(123) #### make it reproducible
df<-data.frame(x=runif(100,1,1e7),y=runif(100,0.01,.08),t=as.factor(sample(1:3,100,replace=T)))
png("xyplot_grid_misaligned.png",800,800)
p<-xyplot(y ~ x,groups=t,data=df,scales=list(x=list(log=10,equispaced.log=F)),auto.key=T,ylim=c(-.01,.1),grid=T)
print(p)
dev.off()

正如预期的那样,它产生了一个美丽的情节: xyplot with grid misaligned

我希望绘图上的网格与 equispaced.log=F 生成的刻度线对齐。 xyplot 的文档仅讨论与多个绘图相关的网格,SO 和其他站点中的其他一些线程也是如此(事实上,我从另一个站点获得了 grid=T 参数site: Using Lattice graphics in R ,即使在那里您也可以看到,当使用 equispaced.log=F 时,网格与刻度“未对齐”。

以防万一有人认为这是 SO: aligning-grid-lines-to-axis-ticks-in-lattice-graphics 的重复项,请注意其中的问题是如何在多图中对齐网格(并且当时该线程还没有答案)。

在使用equispaced.log=F时,如何让xyplot“对齐”网格线到x刻度?谢谢!

最佳答案

看来您需要使用自定义面板函数来执行此操作,并使用 panel.abline 而不是 panel.grid。我能想到的最好办法是半手动设置刻度点。

library(lattice)
set.seed(123) #### make it reproducible

df<-data.frame(x=runif(100,1,1e7),y=runif(100,0.01,.08),t=as.factor(sample(1:3,100,replace=T)))

# do this one by hand, since you had "equispaced.log=F"
x.at = c(5e3, 10e3, 5e4, 10e4, 5e5, 10e5, 5e6, 10e6)
# but this one is done with `pretty` as usual
y.at = pretty(df$y,4)

png("xyplot_grid_aligned.png",600,600)
p <- xyplot(y ~ x,groups=t,data=df,
scales=list(x=list(at=x.at,log=10),
y=list(at=y.at)),
auto.key=T,
ylim=c(-.01,.1),
panel = function(...) {
panel.abline(v=log10(x.at),h=y.at,col="lightgrey")
panel.xyplot(...)
}
)
print(p)
dev.off()

enter image description here

关于Rlattice xyplot与网格与轴的刻度不匹配(不是多重图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26847483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com