R: log transform linear fit of geom_smooth

南楼画角 提交于 2021-02-04 19:42:32

问题


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())

What I would like to add is:

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.


回答1:


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:

In the question it says the following regarding 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.

I don't see this is my plots though.



来源:https://stackoverflow.com/questions/29275288/r-log-transform-linear-fit-of-geom-smooth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!