R:geom_smooth 的对数变换线性拟合

R: log transform linear fit of geom_smooth

我正在尝试使用 R 中的 ggplot2 制作一个具有对数 x 轴和正常缩放 y 轴的图形。这一切都很好,但是当我使用 model="lm" 添加平滑器时,我得到问题。
我想要的是通过我的数据 对数转换之前拟合线性模型,然后对其进行对数转换,因此 geom_smooth 拟合的线应该是弯曲的而不是直线.我找到了一种使用 coord_trans(x="log10") 执行此操作的方法,但如果我这样做,x 轴的刻度线就会全部弄乱。

到目前为止我得到的是:

require(ggplot2)
require(colorspace)
require(scales)

C<-c(221562500,22156250,2215625,221562.5,360000000,36000000,3600000,360000)
OD400<-c(1.304,0.130,0.011,0.001,2.095,0.231,0.020,0.001)
OD700<-c(0.991,0.100,0.007,0.000,1.452,0.179,0.012,0.000)
mydata<-data.frame(C,OD400,OD700)

p<-ggplot() +
  geom_point(data=mydata,aes(x=C,y=OD400,colour="red")) +
  geom_point(data=mydata,aes(x=C,y=OD700,colour="green")) +
  scale_colour_manual("Wavelength", breaks=c("red","green"), labels=c("400 nm","700 nm"),
                     values=rainbow_hcl(2,c=80,l=60)) +
  scale_x_continuous(trans = log10_trans(),
                     breaks = trans_breaks("log10", function(x) 10^x),
                     labels = trans_format("log10", math_format(10^.x))) +
  annotation_logticks(sides="tb") +
  labs(x="Concentration [conidia/mL]",y="Absorption") +
  theme_bw() +  theme(panel.grid.minor = element_blank())

我要补充的是:

p<-p + geom_smooth(data=mydata,aes(x=C,y=OD400),method="lm")
p<-p + geom_smooth(data=mydata,aes(x=C,y=OD700),method="lm")

但是对数变换之前,拟合曲线也进行对数变换。

我想我使用以下代码得到了您想要的:

########################################
# original code without transformation #
########################################

require(ggplot2)
require(colorspace)
require(scales)

C<-c(221562500,22156250,2215625,221562.5,360000000,36000000,3600000,360000)
OD400<-c(1.304,0.130,0.011,0.001,2.095,0.231,0.020,0.001)
OD700<-c(0.991,0.100,0.007,0.000,1.452,0.179,0.012,0.000)
mydata<-data.frame(C,OD400,OD700)

p<-ggplot() +
  geom_point(data=mydata,aes(x=C,y=OD400,colour="red")) +
  geom_point(data=mydata,aes(x=C,y=OD700,colour="green")) +
  scale_colour_manual("Wavelength", breaks=c("red","green"), labels=c("400 nm","700 nm"),
                 values=rainbow_hcl(2,c=80,l=60)) +
#  scale_x_continuous(trans = log10_trans(),
#                 breaks = trans_breaks("log10", function(x) 10^x),
#                 labels = trans_format("log10", math_format(10^.x))) +
#  annotation_logticks(sides="tb") +
  labs(x="Concentration [conidia/mL]",y="Absorption") +
  theme_bw() +  theme(panel.grid.minor = element_blank())


##################
# print raw plot #
##################

p

####################################
# fit lines (copied from question) #
####################################

p<-p + geom_smooth(data=mydata,aes(x=C,y=OD400),method="lm")
p<-p + geom_smooth(data=mydata,aes(x=C,y=OD700),method="lm")


#######################################
# print untransformed plot with lines #
#######################################

p

###################################################
# transform x-axis (as mentioned in the question) #
###################################################

p<-p+coord_trans(x="log10")


#####################################
# print transformed plot with lines #
#####################################

p

如您所见,代码基本上是从问题和returns以下图表中复制粘贴的:

在问题中它说了以下关于 coord_trans 的内容:

I have found a way to do this with coord_trans(x="log10"), but if I do it this way the tick marks of the x-axis are all messed up.

虽然我不认为这是我的阴谋。