R:在ggplot2

时间:2017-04-07 20:49:45

标签: r

尝试创建表示简单斜率分析的折线图。我使用builtin 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

我无法弄清楚的最后几项改变。如何增加线端点的大小?例如,将三角形变大?

传奇的其他几个问题。首先,如何在传奇周围放置黑匣子?不是黑色背景,只是传说周围的简单黑框轮廓。第二,除了圆形或三角形之外,如何在图例中添加实线或虚线?三,如何将传奇标题文本居中?请参阅通用绘图代码作为我所有3个这样的例子。

2 个答案:

答案 0 :(得分:0)

您的问题似乎主要是因为您的数据点缺乏审美和层次。 geom_point将绘制数据点,为您提供所需线条上的图例和形状。主aes命令中的ggplot告诉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")

答案 1 :(得分:0)

我知道这在比赛中已经很晚了。我正在寻找一个完全不同的答案,并找到了这个无关紧要的问题。首先,Pdubbs制作了图表。所以,我只是要从中构建并回答以下内容:如何使三角形更大,如何在图例周围放置黑框,如何将线条添加到图例中,以及如何移动图例标题。在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")

注意:要更改形状的大小,您只需在size=X调用中使用geom_point()即可。这是第一个出色的答案。

添加您使用的黑匣子+theme(legend.background=element_rect(fill="white",colour="black")) 要将线条添加到图例,您应该只能添加 ,show.legend=Tgeom_segment行。例如:

geom_segment(aes(x = 1, y = 5.8, xend = 2, yend = 6.2), linetype = 1,show.legend=T)我估计你需要这两行。

要在图例中移动标题,您可以使用: guides(shape=guide_legend(title.position="top"))

最终运行可能如下所示:

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"))

这一刻可能已经过去了,我认为这些答案可能会在某个地方传播到谷歌领域,但希望这对某人有所帮助。