2016-12-20 58 views
0

上的log-log图用曲线我是博士学生在公牛海带学习异速生长缩放和我一直试图使用下面的代码添加一个回归线和预测斜率线(斜率= 1)到我的未记录数据的日志记录图,而R将“运行”代码,适当的行不显示在我的情节。我认为这是与日志结垢问题,最好我想它看起来更像是一个传统的对数曲线(以0.1规模,1,10,100,1000轴),但我一直无法弄清楚如何在R中这样做。我已经包含了我正在使用的数据,我会很感激您可以提供的任何帮助!绘图中的R

stipelengthcm - 1065.0 959.0 925.0 757.0 722.0 663.0 559.0 550.0 550.0 518.0 400.0 379.0 370.0 365.0 363.2 351.8 323.2 306.8 290.0 260.0 251.0 251.0 249.0 242.0 240.0 229.0 220.0 

bulbwidthcm -10.0 16.0 7.8 10.0 10.0 8.5 5.0 6.0 6.0 4.6 4.4 5.6 5.0 7.0 4.2 4.4 5.4 5.4 5.6 6.0 4.0 3.8 4.6 5.0 4.6 5.5 5.4 
library(lmodel2) 
regrlogkelp<-lmodel2(log(stipelengthcm)~log(bulbwidthcm), data=logkelp, nperm=99) 
regrlogkelp 

plot(stipelengthcm, bulbwidthcm, log="xy", xlab = "Stipe Length (cm)", ylab = "Bulb Width (cm)", pch = 1, col = "black") 
curve(exp(regrlogkelp$regression[3,2]) * x^(regrlogkelp$regression[3,3]), add = T) 
var < -(mean(bulbwidthcm)/exp(regrlogkelp$regression[3,2]))^(1/regrlogkelp$regression[3,3]) 
curve((x^2)*(mean(bulbwidthcm)/var^2), add =T, lty = "dashed") 

回答

1

可以在R基本图形或ggplot2做到这一点。我们试试ggplot2

比方说,你的数据在logkelp,你的回归是regrlogkelp

首先根据您的模型进行预测。

#make predicted values 
logkelp$logcurve1 <- regrlogkelp$regression[3,2] + 
log(bulbwidthcm)*regrlogkelp$regression[3,3] 

加载ggplot2并要求点数和预测线。

library(ggplot2) 

ggplot(data=logkelp) + 

#plots the points 
geom_point(aes(x=bulbwidthcm, y=stipelengthcm)) + 

#draws the prediction line; note that y here is exponentiated and matches your expression 
geom_line(aes(x=bulbwidthcm, y=exp(logcurve1))) + 

#put the x axis on the log scale and designate where tick marks will be 
scale_x_log10(breaks=seq(0,20, by=1)) + 

#put the y axis on the log scale and designate where tick marks will be 
scale_y_log10(breaks=seq(0,2000, by=100)) + 

#change from the default ggplot2 style 
theme_bw(base_size = 15) 

enter image description here

您可以更改轴标签和颜色,如果你愿意的话。如果你想做信心乐队,看看geom_ribbon()

我想为什么你的情节没有出现是因为你的curve()声明的结果远远高于你的点的绘图区域。 R正在执行它,但你将不得不改变规模才能看到它。你在混合exp()log()。这也令人困惑,因为X和Y在你的回归和图形之间交换。

这实际上是其中R是从curve()策划结果:

plot(stipelengthcm, bulbwidthcm, log="xy", 
xlab = "Stipe Length (cm)", ylab = "Bulb Width (cm)", pch = 1, col = "black", 
ylim=c(1,100000)) 

enter image description here