我正在尝试使用ggplot2创建一个具有对数间距网格的绘图,如下图所示。我得到等距网格,但不是对数间隔的网格。我知道我错过了一些我现在似乎无法获得的参数。我已经看到很多关于Pretty ticks for log normal scale using 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)
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)
答案 0 :(得分:3)
您是否正在寻找像这样减少间距网格?
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)))
答案 1 :(得分:2)
您可以手动定义中断。
> ticks <- 2:10
> ooms <- 10^(0:3)
> breaks <- as.vector(ticks %o% ooms)
> breaks
[1] 2 3 4 5 6 7 8 9 10
[10] 20 30 40 50 60 70 80 90 100
[19] 200 300 400 500 600 700 800 900 1000
[28] 2000 3000 4000 5000 6000 7000 8000 9000 10000
此片段定义了中断,隐藏了其中一些,并生成了图。
library(ggplot2)
set.seed(5)
x <- rlnorm(1000, meanlog=3.5, sdlog=1)
y <- rlnorm(1000, meanlog=4.0, sdlog=1)
d <- data.frame(x, y)
ticks <- 2:10
# define the OOMs (orders of magnitudes)
ooms <- 10^(0:3)
breaks <- as.vector(ticks %o% ooms)
# select the labels to show
show.labels <- c(T, F, F, T, F, F, F, F, T)
labels <- as.character(breaks * show.labels)
labels <- gsub("^0$", "", labels)
p <- ggplot(d, aes(x=x, y=y)) + geom_point() +
scale_x_log10(limits = c(1, NA), labels = labels, breaks = breaks) +
scale_y_log10(limits = c(1, NA), labels = labels, breaks = breaks) +
theme_bw() + theme(panel.grid.minor = element_line(color="blue", linetype="dotted"), panel.grid.major = element_line(color="blue", linetype="dotted")) +
annotation_logticks(base = 10)
p
答案 2 :(得分:2)
对于使用ggplot2的对数间隔网格,Samehmagd提供的答案因其简单性而闪耀,不需要手动设置,它可以回答这个问题。
在我自己的数据上使用它时,我发现了一些似乎是错误的东西。至少在我手中,当要绘制的值小于1时,此策略失败(因此在应用log10时生成负值)。
这是由Samehmagd提供的例子,以及我对最终的贡献:
library(ggplot2)
library(scales)
set.seed(5)
x <- rlnorm(1000, meanlog=3.5, sdlog=1)
y <- rlnorm(1000, meanlog=4.0, sdlog=1)
d <- data.frame(x, y)
# peek at d
head(d)
# x y
# 1 14.284064 12.74253
# 2 132.205740 189.53295
# 3 9.435773 35.44751
# 4 35.521664 54.97449
# 5 183.358064 61.84004
# 6 18.121372 36.24753
# Plot successfully (this figure is identical to Samehmagd's)
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)))
# Now, here is when it breaks
f <- 1/d
# peek at f
head(f)
# x y
# 1 0.070008087 0.078477335
# 2 0.007563968 0.005276127
# 3 0.105979655 0.028210728
# 4 0.028151834 0.018190255
# 5 0.005453810 0.016170753
# 6 0.055183459 0.027588083
# Get the plotting to fail just by using f instead of d, no other change.
ggplot(f, 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)))
Error in if (zero_range(range)) { : missing value where TRUE/FALSE needed
In addition: Warning message:
In trans$transform(out$range) : NaNs produced
无论这个失败是错误还是功能,都值得记录。
答案 3 :(得分:2)
为了建立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()