扩展ggplot2中多元线性回归的极限并外推相应的交叉点

时间:2014-02-13 15:19:40

标签: r ggplot2 intersection linear-regression extrapolation

我有一些数据here in a .txt file,我使用下面的代码行绘制下面的图表,

library(scales)
library(ggplot2)
library(reshape2)    

# read data from .txt file into a data frame tdf
write.table(tdf,"multreg.txt",col.names=T,sep="\t") 

# Melt data frame tdf
mlt_tdf <- melt(tdf, id=names(tdf)[1], variable = "cols") 

# Plotting data with corresponding linear regression fits to it
plt_fit <- ggplot(mlt_tdf, aes(x=x,y= value, color=cols)) +
  geom_point(size=2) +
  geom_smooth(method = "lm", se = FALSE,linetype="dashed") +
  scale_y_log10(labels = trans_format("log10", math_format(10^.x))) +   # To display lables in scientific log exponent format without decimal notation for the raised log exponent
  annotation_logticks(sides = "rl") +
  xlim(2,4) +
  theme_bw() + 
  theme(panel.grid.minor = element_blank()) +
  theme(legend.text=element_text(size=14), legend.title=element_text(size=14))+
  theme(axis.text=element_text(size=26)) +
  theme(axis.title=element_text(size=22,face="bold")) +
  labs(x = "x", y = "y") +
  scale_color_discrete(name = "vals", labels = c("+0.1","+0.2","+0.3","+0.4","+0.5","+0.6","+0.7","+0.8","+0.9","+1")) +
  guides(colour = guide_legend(override.aes = list(size=3),nrow=2,title.position = 'top',title.hjust=0.5,legend.direction = "horizontal")) +
  theme(legend.position = 'bottom', legend.margin=unit(0.05,"cm"),legend.background = element_rect(fill ='gray94')) +
  theme(plot.margin=unit(c(0,2,0,0),"mm"))

enter image description here

我想将线性拟合的极限扩展到延伸,直到它们如图所示相交here。我试图增加x轴限制+ xlim(2,4),但似乎没有扩展线性拟合。 我想推断相应的交叉点并将其传递给数据帧。有人可以建议如何解决这个问题。

感谢您的帮助。

0 个答案:

没有答案