通过ggplot2为NMDS的椭圆添加特定的形状和线型

时间:2013-09-25 12:43:32

标签: r ggplot2 vegan

作为对这个非常有用的问题和答案的跟进:

Color-coding 95% confidence ellipses for centroids

我已经成功地在ggplot中为我的NMDS图重叠了置信椭圆。我现在想要指定绘图上数据点的颜色和形状,以及椭圆的颜色和线型,以区分我的四种处理。

NMDS在这里:

http://pastebin.com/99WcC6wN

Dat2在这里:

http://pastebin.com/D93wrShT

我尝试过的代码如下:

ggplot(data = NMDS, aes(MDS1, MDS2)) + geom_point(aes(color = group)) +
  geom_path(data=Dat2, aes(x=NMDS1, y=NMDS2,colour=group), size=1,     linetype=2)+theme(axis.title.y=element_text(size=rel(1.1),vjust=0.2),axis.title.x=element_t    ext(size=rel(1.1),vjust=0.2),axis.text.x=element_text(size=rel(1)),axis.text.y=element_text(size=rel(1)),text = element_text(size=13)) +scale_shape_manual(name = "Treatment", labels = c("W+N-", "W+NC", "WCN-", "WCNC"),
                  values = c("17", "19","17", "19")) +scale_colour_manual(name = "Treatment", labels = c("W+N-", "W+NC", "WCN-", "WCNC"),
                  values = c("blue", "blue","red", "red")) +scale_linetype_manual(name = "Treatment", labels = c("W+N-", "W+NC", "WCN-", "WCNC"),
                  values = c("3", "1","3", "1")) 

不幸的是,只有点和椭圆的颜色似乎有效 - 椭圆的线型和数据点的形状似乎没有变化。有没有人建议如何解决这个问题?

非常感谢!

1 个答案:

答案 0 :(得分:1)

您需要将linetypeshape映射到group,就像将color映射到group一样。实际上,您可以在第一行中执行这两次映射,因为两个数据集中变量的名称相同:

ggplot(data = NMDS, aes(MDS1, MDS2, color=group, linetype=group, shape=group))

当然,您需要删除其他地方的colorlinetype映射。您还应该将linetype中的scale_linetype_manual值指定为数字,而不是字符串。最后,你会有这样的事情:

ggplot(data = NMDS, aes(MDS1, MDS2, color=group, linetype=group, shape=group)) + 
  geom_point() +
  geom_path(data=Dat2, aes(x=NMDS1, y=NMDS2), size=1) + 
  theme(axis.title.y=element_text(size=rel(1.1),vjust=0.2),axis.title.x=element_text(size=rel(1.1),vjust=0.2),axis.text.x=element_text(size=rel(1)),axis.text.y=element_text(size=rel(1)),text = element_text(size=13)) +
  scale_shape_manual(name = "Treatment", labels = c("W+N-", "W+NC", "WCN-", "WCNC"), values = c(17, 19,17, 19)) +
  scale_colour_manual(name = "Treatment", labels = c("W+N-", "W+NC", "WCN-", "WCNC"), values = c("blue", "blue","red", "red")) +
  scale_linetype_manual(name = "Treatment", labels = c("W+N-", "W+NC", "WCN-", "WCNC"), values = c(3, 1,3, 1))

the desired plot