R:geom_smooth的对数变换线性拟合 [英] R: log transform linear fit of geom_smooth
问题描述
我试图用R中的ggplot2制作一个具有对数x轴和正常比例y轴的图形.这一切都很好,但是当我用model ="lm"添加一个更平滑的图形时,我遇到了问题.
我想通过对数变换对数据进行线性拟合,然后对其进行对数变换,因此geom_smooth拟合的线应该是弯曲的,而不是直线.我已经找到了一种使用 coord_trans(x ="log10")
的方法,但是如果我这样做,则x轴的刻度都被弄乱了.
I am trying to make a graph with logarithmic x-axis and normal scaled y-axis with ggplot2 in R. This all works fine, but when I add a smoother with model="lm", I get problems.
What I would like is to fit a linear model through my data before the log transformation and then have it log transformed, thus the line fitted by geom_smooth should be curved instead of straight. 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.
到目前为止,我的情况是:
What I have so far is this:
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")
但在之前,对数转换和拟合曲线也将进行对数转换.
But before the log transformation and have the curves fitted be log transformed as well.
推荐答案
我认为我可以使用以下代码完全得到您想要的东西:
I think I get exactly what you want using the following code:
########################################
# 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
如您所见,代码本质上是从问题中复制粘贴的,并返回以下图表:
As you can see the code is essentially copy-pasted from the question and returns the following plots:
在问题中,有关 coord_trans
的内容如下:
In the question it says the following regarding coord_trans
:
我找到了一种使用coord_trans(x ="log10")进行此操作的方法,但是如果我这样做,则x轴的刻度线都被弄乱了.
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.
我看不到这是我的情节.
I don't see this is my plots though.
这篇关于R:geom_smooth的对数变换线性拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!