Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以Mtcars为例求散点图的最佳拟合线_R - Fatal编程技术网

以Mtcars为例求散点图的最佳拟合线

以Mtcars为例求散点图的最佳拟合线,r,R,您好,如何在重量与Milage散点图上获得最佳拟合线?尽管他们有点分散。以下是我的代码: plot(x = mtcars$wt,y = mtcars$mpg, xlab = "Weight", ylab = "Milage", xlim = c(2.5,5), ylim = c(15,30), main = "Weight vs Milage") 由于您正在使用基本R的绘图,因此可以执行以下操作: plot(x = mtcars

您好,如何在重量与Milage散点图上获得最佳拟合线?尽管他们有点分散。以下是我的代码:

plot(x = mtcars$wt,y = mtcars$mpg,
     xlab = "Weight",
     ylab = "Milage",
     xlim = c(2.5,5),
     ylim = c(15,30),        
     main = "Weight vs Milage")

由于您正在使用基本R的
绘图
,因此可以执行以下操作:

plot(x = mtcars$wt,y = mtcars$mpg,
 xlab = "Weight",
 ylab = "Milage",
 xlim = c(2.5,5),
 ylim = c(15,30),        
 main = "Weight vs Milage")
abline(lm(mpg ~ wt, data = mtcars), col = "red")


说明:
lm(mpg~wt,data=mtcars)
在一个简单的线性模型中回归
mpg
on
wt
<代码>基线可以将
lm
返回对象作为输入来绘制回归线。

由于您正在使用基本R的
绘图
,因此可以执行以下操作:

plot(x = mtcars$wt,y = mtcars$mpg,
 xlab = "Weight",
 ylab = "Milage",
 xlim = c(2.5,5),
 ylim = c(15,30),        
 main = "Weight vs Milage")
abline(lm(mpg ~ wt, data = mtcars), col = "red")
library(ggplot2)

ggplot(mtcars, aes(x = wt, y = mpg)) + 
  geom_point() +
  stat_smooth(method = "lm", col = "red", se = F)


说明:
lm(mpg~wt,data=mtcars)
在一个简单的线性模型中回归
mpg
on
wt
abline
可以将
lm
返回对象作为输入来绘制回归线。

这很好。只是一个后续问题,你如何将其转化为excel?我试过写这段代码write.xlsx(Scatterplot,file=“Scatterplot.xlsx”,sheetName=“Scatterplot”,append=FALSE),但出现了一个错误,表示超出范围。@Bustergun不确定您的意思。据我所知,R绘图在Excel中不可编辑/自定义。为什么要导出到Excel?最好将所有内容都保存在R中。如果需要共享数据和结果,可以使用
write.csv
导出数据,然后在Excel中打开该文件。或者将图形另存为pdf/png,查看
?png
?pdf
。这很好。只是一个后续问题,你如何将其转化为excel?我试过写这段代码write.xlsx(Scatterplot,file=“Scatterplot.xlsx”,sheetName=“Scatterplot”,append=FALSE),但出现了一个错误,表示超出范围。@Bustergun不确定您的意思。据我所知,R绘图在Excel中不可编辑/自定义。为什么要导出到Excel?最好将所有内容都保存在R中。如果需要共享数据和结果,可以使用
write.csv
导出数据,然后在Excel中打开该文件。或者将图形另存为pdf/png,查看
?png
?pdf
library(ggplot2)

ggplot(mtcars, aes(x = wt, y = mpg)) + 
  geom_point() +
  stat_smooth(method = "lm", col = "red", se = F)