在R中设置趋势线长度

在R中设置趋势线长度,r,scatter-plot,trendline,R,Scatter Plot,Trendline,我已经成功地创建了一个散点图,在一个图上有两个数据集。一组数据的X轴范围为0-40(绿色),而另一组数据的范围仅为0-15(红色) 我使用此代码分别向红色和绿色数据添加趋势线(使用par(新)) 绘图(x1,y1,col=“red”,axes=FALSE,xlab=“”,ylab=“”,ylim=range(0:1),xlim=range(0:40)) f尝试使用ggplot。以下示例使用mtcars数据: library(ggplot2) ggplot(mtcars, aes(qsec, w

我已经成功地创建了一个散点图,在一个图上有两个数据集。一组数据的X轴范围为0-40(绿色),而另一组数据的范围仅为0-15(红色)

我使用此代码分别向红色和绿色数据添加趋势线(使用par(新))

绘图(x1,y1,col=“red”,axes=FALSE,xlab=“”,ylab=“”,ylim=range(0:1),xlim=range(0:40))

f尝试使用ggplot。以下示例使用mtcars数据:

library(ggplot2)
ggplot(mtcars, aes(qsec, wt, color=factor(vs)))+geom_point()+ stat_smooth(se=F)

您可以使用
曲线
函数的
from
to
参数在基本图形中执行此操作(有关详细信息,请参阅
曲线
的帮助)。例如:

# Your function
f <- function(x1,a,b,d) {(a*x1^2) + (b*x1) + d}

# Plot the function from x=-100 to x=100
curve(f(x, a=-2, b=3, d=0), from=-100, to=100, col="red", lwd=1, lty=1)

# Same curve going from x=-100 to x=0 (and shifted down by 1000 units so it's
# easy to see)
curve(f(x, a=-2, b=3, d=-1000), from=-100, to=0, 
      add=TRUE, col="blue", lwd=2, lty=1)

谢谢你。由于缺乏经验,我无法发布图片。我想你可以只做
geom_smooth(se=FALSE)
而不是两次
stat_smooth
调用。谢谢你的建议。我之前试过,但没有成功,因为当时我使用的是color=vs,而不是color=factor(vs),这也很好。谢谢你的回复。我还没有完全掌握ggplot,我所有的其他图表都采用与上面相同的格式。
# Your function
f <- function(x1,a,b,d) {(a*x1^2) + (b*x1) + d}

# Plot the function from x=-100 to x=100
curve(f(x, a=-2, b=3, d=0), from=-100, to=100, col="red", lwd=1, lty=1)

# Same curve going from x=-100 to x=0 (and shifted down by 1000 units so it's
# easy to see)
curve(f(x, a=-2, b=3, d=-1000), from=-100, to=0, 
      add=TRUE, col="blue", lwd=2, lty=1)
curve(f(x, a=-2, b=3, d=0), from=range(df$x)[1], to=range(df$x)[2], 
       add=TRUE, col="red", lwd=1, lty=1)