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
R:在ggplot2中创建通用绘图_R - Fatal编程技术网

R:在ggplot2中创建通用绘图

R:在ggplot2中创建通用绘图,r,R,尝试创建表示简单坡度分析的折线图。我使用内置plot()命令创建了绘图。如何为ggplot2重写此代码?我尝试使用ggplot2,但无法实现此数字,因此我使用plot()编写了 这是我用来创建绘图的代码 yrange = c(4,7) xrange = c(-1.5,1.5) par(bty = 'l') par(family="Times") plot(c(-1, 1), c(5.8, 6.2), type='b', lty=1, pch = 15, axes=F, xlab="", ylab

尝试创建表示简单坡度分析的折线图。我使用内置plot()命令创建了绘图。如何为ggplot2重写此代码?我尝试使用ggplot2,但无法实现此数字,因此我使用plot()编写了

这是我用来创建绘图的代码

yrange = c(4,7)
xrange = c(-1.5,1.5)
par(bty = 'l')
par(family="Times")
plot(c(-1, 1), c(5.8, 6.2), type='b', lty=1, pch = 15, axes=F, xlab="", ylab="", ylim=yrange, xlim=xrange)
par(new = T)
plot(c(-1, 1), c(5.3, 5.5), type='b', lty=2, pch = 17, axes=F, xlab="IV1", ylab="DV", ylim=yrange, xlim=xrange)
axis(1, at=c(-1,1), labels=c("Cond1", "Cond2"))
axis(2, at=c(4, 5,6,7))
legend("topright", title = "Moderator", c("High", "Low"), lty=1:2)
box()
下面是我在尝试使用ggplot2创建上图时使用的代码。我无法确定如何为版主创建图例,如何更改x轴标签,在线条末端添加方框/三角形/圆圈,或者将线条设置为具有适当出版质量的APA图形:

ggplot(df) +
  geom_segment(aes(x = -1, y = 5.8, xend = +1, yend = 6.2), linetype = 1) + 
  geom_segment(aes(x = -1, y = 5.3, xend = +1, yend = 5.5), linetype = 2) + 
  labs(x = 'IV1', y = 'DV') +
  coord_cartesian(ylim = c(4, 7)) +  
  scale_x_continuous(breaks=seq(-1, +1, 2)) +  
  theme(legend.position = "bottom")
对不起,这是我在这里的第一篇文章。我对更多地使用ggplot2感兴趣,但它有一个陡峭的学习曲线,即使对于R


@Pdubbs-我更新了您的代码,使其更接近我的情况。正如我在下面提到的,我试图绘制两个连续变量之间的2x2交互作用,我正在创建的图是使用简单的斜率回归分析可视化交互作用。下面是我运行此代码时发生的情况和输出:

> df<-data.frame(DV = c(5.8,5.3,6.2,5.5), 
+                Moderator = c(1,2,3,4), 
+                IV1 = c(6,4,3,2))
> 
> ggplot(df,aes(x=IV1,y=DV,shape=Moderator)) + geom_point() +
+   geom_segment(aes(x = 1, y = 5.8, xend = 2, yend = 6.2), linetype = 1) + 
+   geom_segment(aes(x = 1, y = 5.3, xend = 2, yend = 5.5), linetype = 2) + 
+   labs(x = 'IV1', y = 'DV') +
+   coord_cartesian(ylim = c(4, 7)) +  
+   theme(legend.position = "bottom")
Error: A continuous variable can not be mapped to shape
>df
>ggplot(df,aes(x=IV1,y=DV,形状=慢化剂))+geom_点()+
+几何图形段(aes(x=1,y=5.8,xend=2,yend=6.2,线型=1)+
+几何线段(aes(x=1,y=5.3,xend=2,yend=5.5,线型=2)+
+实验室(x='IV1',y='DV')+
+坐标笛卡尔(ylim=c(4,7))+
+主题(legend.position=“底部”)
错误:连续变量无法映射到形状

我想不出最后的几个变化。如何增加直线端点的大小?例如,使三角形变大


还有一些关于传奇的问题。首先,如何在图例周围放置一个黑匣子?不是黑色背景,只是围绕图例的简单黑框轮廓。第二,除了圆或三角形外,如何在图例中添加实线或虚线?第三,如何将图例标题文本居中?请参见通用绘图代码,作为我所指全部3个的示例

您的问题似乎主要是缺少数据点的美观和层次
geom_point
将绘制数据点,为您提供所需线条上的图例和形状。主
ggplot
命令中的
aes
告诉ggplot应该如何绘制df中的数据。我们可以得到你的图如下:

df<-data.frame(DV = c(5.8,5.3,6.2,5.5), 
               Moderator = c("High", "Low", "High", "Low"), 
               IV1 = c("Cond1","Cond1","Cond2","Cond2"))

ggplot(df,aes(x=IV1,y=DV,shape=Moderator)) + geom_point() +
  geom_segment(aes(x = 1, y = 5.8, xend = 2, yend = 6.2), linetype = 1) + 
  geom_segment(aes(x = 1, y = 5.3, xend = 2, yend = 5.5), linetype = 2) + 
  labs(x = 'IV1', y = 'DV') +
  coord_cartesian(ylim = c(4, 7)) +  
  theme(legend.position = "bottom")

df我知道这在游戏中已经非常晚了。我在寻找一个完全不同的答案,发现了这个未包含在内的问题。首先,PDUBB绘制了图表。因此,我将从中构建并回答以下问题:如何使三角形变大,如何在图例周围放置一个黑框,如何向图例添加线条,以及如何移动图例标题。在OP的要求下,我考虑了两个连续变量,并将“调节者”作为一个因素。就在这里:

df<-data.frame(DV = c(5.8,5.3,6.2,5.5),Moderator = c("1","2","3","4"),IV1 = 
c(6,4,3,2))
ggplot(df,aes(x=IV1,y=DV,shape=Moderator)) + geom_point(size=10) +
geom_segment(aes(x = 1, y = 5.8, xend = 2, yend = 6.2), linetype = 1) + 
geom_segment(aes(x = 1, y = 5.3, xend = 2, yend = 5.5), linetype = 2) + 
labs(x = 'IV1', y = 'DV') +
coord_cartesian(ylim = c(4, 7)) +  
theme(legend.position = "bottom")

这一刻可能已经过去了,我想这些答案可能会在谷歌的某个地方传播开来,但希望这能帮助一些人。

你到底尝试了什么
ggplot
命令,你到底在哪里卡住了?如果你表现出一些努力,这个问题会得到更好的回答。堆栈溢出不是代码转换服务。这里应该有一个特定的问题。请将此信息编辑到您的问题中,以便代码可以正确格式化(不要将其放在注释中)。请准确描述您需要重新创建的功能。您可以尝试ggplot(df,aes(x=IV1,y=DV,shape=as.factor(主持人)))建议阅读:,@MLavoie-shape=as.factor(主持人)没有帮助。它将每个调节器值视为不同级别的调节器。我的原始数据是两个连续变量。该图用于两个连续变量之间的2x2交互作用,使用简单的斜率来可视化交互作用。使用上述命令时,我收到错误消息:“错误:连续变量无法映射到形状”
df<-data.frame(DV = c(5.8,5.3,6.2,5.5),Moderator = c("1","2","3","4"),IV1 = 
c(6,4,3,2))
ggplot(df,aes(x=IV1,y=DV,shape=Moderator)) + geom_point(size=4) +
geom_segment(aes(x = 1, y = 5.8, xend = 2, yend = 6.2), linetype = 
1,show.legend=T) + 
geom_segment(aes(x = 1, y = 5.3, xend = 2, yend = 5.5), linetype = 
2,show.legend=T) + 
labs(x = 'IV1', y = 'DV') +
coord_cartesian(ylim = c(4, 7)) +  
theme(legend.position = "bottom")+
theme(legend.background=element_rect(fill="white",colour="black"))+
labs(shape="Awesome Sauce")+guides(shape=guide_legend(title.position="top"))