R 用'绘制的对数网格;ggplot2';
我正在尝试使用ggplot2创建一个具有对数间距网格的绘图,如下图所示。我得到的是等距网格,但不是对数间距网格。我知道我遗漏了一些参数,但到目前为止我似乎还没有得到。我已经看到很多关于这个话题的问题,比如,但不要解决我正在寻找的问题R 用'绘制的对数网格;ggplot2';,r,ggplot2,scatter-plot,r-grid,R,Ggplot2,Scatter Plot,R Grid,我正在尝试使用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) plot(x, y
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)
set.seed(5)
x您是否在寻找这样的递减间距网格
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)))
您可以手动定义中断
> 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
>滴答声、ooms声、break声
[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
库(ggplot2)
种子(5)
x对于使用ggplot2的对数间隔网格,Samehmagd提供的答案因其简单而引人注目,不需要手动设置,并且回答了这个问题
在我自己的数据上使用它时,我发现了一些似乎是bug的东西。至少在我看来,当要绘制的值小于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
库(ggplot2)
图书馆(比例尺)
种子(5)
x基于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()
在d%%>%ggplot(aes(x,y))中中断错误:找不到函数“%%>”,我习惯于使用tidyverse(ggplot是其中的一部分),因此我使用了%>%,magrittr中的管道操作符。不过,这个问题完全没有必要。如果您不想使用管道,可以使用ggplot(d,aes(x,y))
。
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()