R 使用ggplot2在同一绘图上绘制两个散点图和带误差条的回归线

R 使用ggplot2在同一绘图上绘制两个散点图和带误差条的回归线,r,ggplot2,R,Ggplot2,我正在尝试创建以下图表: 到目前为止,我已经: ggplot(df,aes(x=IOD,y=movement\u time,color=cursor,shape=cursor)) 但我一点运气都没有。有什么想法吗?ggplot2是我最喜欢的R软件包,下面是我解决这个问题的方法: df = data.frame(difficulty = 2 + (runif(200) * 6), ID = rep(c("point", "bubble"), each = 100)

我正在尝试创建以下图表:

到目前为止,我已经:
ggplot(df,aes(x=IOD,y=movement\u time,color=cursor,shape=cursor))

但我一点运气都没有。有什么想法吗?

ggplot2
是我最喜欢的R软件包,下面是我解决这个问题的方法:

df = data.frame(difficulty = 2 + (runif(200) * 6),
                ID = rep(c("point", "bubble"), each = 100))
df$movement = rep(c(1.2, 1.4), each = 100) * df$difficulty + (runif(200) / 5)

library(ggplot2)
theme_set(theme_bw())
ggplot(df, aes(x = difficulty, y = movement, color = ID, shape = ID)) + 
    geom_point() + 
    stat_smooth(method = 'lm')

这只是@PaulHiemstra答案中的一个小段,展示了如何在绘图区域内移动图例,添加边框,以及如何去除灰色背景。IMO
ggplot
绝对是一条出路

ggplot(df, aes(x = difficulty, y = movement, color = ID, shape = ID)) + 
  geom_point() + 
  stat_smooth(method = 'lm',se=F)+
  theme(legend.justification=c(1,0), legend.position=c(1,0),
        legend.key=element_rect(color=NA),
        legend.background=element_rect(color="black"))


注意:您获得灰色背景是因为默认情况下,
stat\u smooth(…)
绘制置信带(灰色)。设置
se=F
将关闭该功能。

您是否可以发布可复制的数据?你试过做什么?所以,你需要1。将数据读入R(请参见读取表),2。获得最佳拟合线(请参见
?lm
),3。绘制一个绘图(请参见绘图)。试一试,然后当您遇到特定问题时,运行相应帮助文件底部的示例。如果您仍然被卡住,请发布一个特定的问题。您还需要查看
?图例
,并注意绘图和图例的“pch”参数。请参阅@user3570653中的示例,谢谢,但我同意其他人的观点,即您的问题相当广泛,并且没有表明您首先对自己进行了研究(不是说您没有,但问题没有显示出来)。