存在scale_y_连续的geom_abline(trans =“log10”)

问题描述 投票:0回答:1

我使用具有不同截距和斜率的

geom_abline
,这按预期工作。但是使用
scale_y_continuous(trans= "log10")
向轴添加对数刻度不仅会改变轴本身,还会改变
geom_abline
线在轴上的位置。请看这里:

df <- data.frame(x= -5:5, y= -5:5)

ggplot(data= df, mapping= aes(x= x, y= y)) +
  geom_point() +
  geom_abline(intercept= 0, slope= 0) +
  scale_y_continuous(trans= "log10")

wrong

水平线位于 y= 1 处,但我希望它位于 y= 0 处(如

intercept= 0
所定义)。如何做到这一点?


编辑

上图切断了 y 轴,因此我们几乎看不到左下角的点。因此,我想用

scale_y_continuous(trans= "log10", limits = c(0, 5))
更改轴限制,但这也不起作用,我收到“'from'必须是有限数量”错误。因此,使用
trans= "log10"
似乎适用于所有事物,甚至适用于
scale_y_continuous
本身的参数。如何防止
trans= "log10"
应用于所有内容(geom_abline、限制、...)?

r ggplot2
1个回答
0
投票

如评论中所述,0 不会出现在对数转换的 y 轴上。您可以使用伪对数,它切换到接近零的线性刻度。

df <- data.frame(x= -5:5, y= -5:5)

ggplot(data= df, mapping= aes(x= x, y= y)) +
  geom_point() +
  geom_abline(intercept= 0, slope= 0) +
  scale_y_continuous(trans= scales::pseudo_log_trans(), limits=c(0,5)

enter image description here

或者没有限制:

ggplot(data= df, mapping= aes(x= x, y= y)) +
  geom_point() +
  geom_abline(intercept= 0, slope= 0) +
  scale_y_continuous(trans= scales::pseudo_log_trans(), breaks=-5:5)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.