'ggplot2' 绘图的对数网格

pra*_*avi 9 r scatter-plot ggplot2 r-grid

我正在尝试使用 ggplot2 创建一个对数间隔网格的图,如下图所示。我得到等距网格,但没有记录间隔网格。我知道我缺少一些我现在似乎没有得到的参数。我已经看到很多关于这个主题的问题,比如使用 ggplot2 (dynamic not manual) 对数正常刻度的漂亮刻度,但没有解决我正在寻找的问题。

set.seed(5)
x <- rlnorm(1000, meanlog=3.5, sdlog=1)
y <- rlnorm(1000, meanlog=4.0, sdlog=1)
d <- data.frame(x, y)

plot(x, y, log="xy", las=1)
grid(nx=NULL, ny=NULL, col= "blue", lty="dotted", equilogs=FALSE)
library(magicaxis)
magaxis(side=1:2, ratio=0.5, unlog=FALSE, labels=FALSE)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

library(ggplot2)
library(MASS)
library(scales)
a <- ggplot(d, aes(x=x, y=y)) + geom_point() +
     scale_x_log10(limits = c(1, NA), 
                   labels = trans_format("log10", math_format(10^.x)),
                   breaks=trans_breaks("log10", function(x) 10^x, n=4)) +
     scale_y_log10(limits = c(1, NA),
                   labels = trans_format("log10", math_format(10^.x)),
                   breaks=trans_breaks("log10", function(x) 10^x, n=4)) +
     theme_bw() + theme(panel.grid.minor = element_line(color="blue", linetype="dotted"), panel.grid.major = element_line(color="blue", linetype="dotted"))
a + annotation_logticks(base = 10)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Ian*_*ent 11

以 Gabor 的回答为基础,没有必要仅在绘图的确切范围内定义刻度。相反,您可以为大范围的值定义中断,这些值将涵盖您期望看到的几乎所有内容,并使用这些值为任何绘图创建漂亮的网格线。虽然可能不是最优雅的解决方案,但与每次都必须手动计算范围相比,它更容易且更具通用性。

breaks <- 10^(-10:10)
minor_breaks <- rep(1:9, 21)*(10^rep(-10:10, each=9))

d %>% 
    ggplot(aes(x, y)) +
        geom_point() +
        scale_x_log10(breaks = breaks, minor_breaks = minor_breaks) +
        scale_y_log10(breaks = breaks, minor_breaks = minor_breaks) +
        annotation_logticks() +
        coord_equal() +
        theme_bw()
Run Code Online (Sandbox Code Playgroud)

链接到情节的图像


Sam*_*agd 5

您是否正在寻找这样的缩小间距网格?

ggplot(d, aes(x=x, y=y)) + geom_point() + 
  coord_trans(y="log10", x="log10") +
  scale_y_continuous(trans = log10_trans(),
                     breaks = trans_breaks("log10", function(x) 10^x),
                     labels = trans_format("log10", math_format(10^.x))) +
  scale_x_continuous(trans = log10_trans(),
                     breaks = trans_breaks("log10", function(x) 10^x),
                     labels = trans_format("log10", math_format(10^.x)))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明