Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.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
R 向具有拟合线和预测间隔的散点图添加图例_R_Ggplot2 - Fatal编程技术网

R 向具有拟合线和预测间隔的散点图添加图例

R 向具有拟合线和预测间隔的散点图添加图例,r,ggplot2,R,Ggplot2,我有这个阴谋 ggplot(dt1, aes(x=x,y=y)) + geom_point(color="orange",shape=1, size=1.5) + stat_function(fun=function(x) 1.05+.65*sqrt(x)+.1*x, color="blue" ) + stat_function(fun=function(x) 1.05-.65*sqrt(x)+.1*x, color="blue" ) + stat_smooth(

我有这个阴谋

ggplot(dt1, aes(x=x,y=y)) +
    geom_point(color="orange",shape=1, size=1.5) +
    stat_function(fun=function(x) 1.05+.65*sqrt(x)+.1*x, color="blue" ) +
    stat_function(fun=function(x) 1.05-.65*sqrt(x)+.1*x, color="blue" ) +
    stat_smooth(method="lm",formula="y~-1+x",fullrange=TRUE,se = FALSE, color="black")
数据如下:

> str(dt1)
'data.frame':   1451 obs. of  2 variables:
 $ y: int  70 161 151 207 100 268 184 156 246 43 ...
 $ x: int  679 1818 1650 2325 992 2412 1830 1398 2207 495 ...
以下是数据示例:

dt1 <- structure(list(y = c(70L, 161L, 151L, 207L, 100L, 268L, 184L, 156L, 246L, 43L, 125L, 135L, 45L, 93L, 88L, 162L, 131L, 143L, 227L, 177L, 124L, 203L, 243L, 193L, 182L, +
223L, 259L, 215L, 170L, 288L, 67L, 141L, 272L, 160L, 66L, 123L, 226L, 383L, 149L, 354L), x = c(679L, 1818L, 1650L, 2325L, 992L, 2412L, 1830L, 1398L, 2207L, 495L, 1071L, 1200L, 723L, 1162L, 1075L, 1431L, 1169L, 1665L, 2136L, 1402L, 896L, 1951L, 2222L, 1960L, 1601L,+
1787L, 2277L, 1861L, 1432L, 2451L, 626L, 1292L, 2216L, 1499L, 766L, 906L, 2157L, 3419L, 1352L, 3346L)), .Names = c("y", "x"), row.names = c(NA, 40L), class = "data.frame")
我想要一个包含以下3个条目的图例:

一个用于geom_点,显示正确的点类型 一个用于显示黑线的stat_平滑 一个用于stat_函数,即一个图例条目,而不是两个,显示蓝线
我如何才能做到这一点?

这不是最好的方法,但如果是我,我会尝试以下方法:

par(new=TRUE)
plot(500,360,pch=1,col="orange",size=1.5)
text(500,350,"|")
text(500,340,"|",col="blue")
text(580,360,[text],pos=4)
text(580,350,[text],pos=4)
text(580,340,[text],pos=4)

祝你好运

事实证明,美学是关键

ggplot(dt1, aes(x=x, y=y)) + 
  geom_point(shape=1,size=1.5, 
             aes(colour="Points",shape='Points', linetype='Points')) +
  stat_function(fun=function(x) 1.05+.65*sqrt(x)+.1*x, 
                aes(colour="Fitted",shape="Fitted",linetype="Fitted")) +
  stat_function(fun=function(x) 1.05-.65*sqrt(x)+.1*x, 
                aes(colour="Fitted",shape="Fitted",linetype="Fitted")) +
  stat_smooth(method="lm",formula="y~-1+x",fullrange=TRUE,se = FALSE, 
              aes(colour="Smoothed",shape="Smoothed",linetype="Smoothed"))+
  scale_colour_manual('',values=c("Points"="orange",
                                  "Smoothed"="black", "Fitted"="blue"))+
  scale_shape_manual('',values=c("Points"=1,"Smoothed"=NA,"Fitted"=NA))+
  scale_linetype_manual('',values=c("Points"=0,"Smoothed"=1,"Fitted"=1))
另见:

@安德烈


@诚挚的A

亲爱的向下投票人-我可以知道你为什么向下投票吗?有没有办法防止图例中线条上的圆圈进行拟合和平滑?不确定。可能值得单独问一个问题。