显示3个因素ggplot geom

时间:2019-06-11 15:38:44

标签: r ggplot2

我正在尝试使用ggplot和geom_point绘制PCA图。 我想说明3个因素(饮食,时间,抗生素)。 我以为可以用黑色勾勒出黑色的点)。 但这并未显示“填充”颜色的第三个因素(时间)。

这是我的数据的子集:

    > dput(dat.pcx.annot.test)
structure(list(PC1 = c(25.296379160162, 1.4703101394886, 11.4138097811008, 
1.41798772574591, 23.7253675969881, 15.5683516005535, -34.6012195481675, 
-25.7129281491955, -2.97230018393742, 4.83421092719293, -0.0274189140249825, 
23.227939504077, 15.2002258785889, -35.2243685702227, -34.2537374460037, 
-7.6380794043063), PC2 = c(27.2678813936857, -9.88577494210313, 
-6.19394322321806, -8.88953660465497, 33.6791127012231, -13.2912233546802, 
7.77877968081575, 2.7371646557436, -8.41929538502921, -11.5151849519265, 
-9.40733576034963, 32.3549860618533, -11.2170071727855, 10.0455709347794, 
3.05679707335492, -6.66218028060621), Diet = structure(c(1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L), .Label = c("RC", 
"WD"), class = "factor"), Time = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("ZT14", 
"ZT2"), class = "factor"), Antibiotics = structure(c(2L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L), .Label = c("Antibiotics ", 
"None"), class = "factor")), row.names = c(1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 18L, 19L, 20L, 21L, 22L), class = "data.frame")

这是绘图命令:

ggplot(dat.pcx.annot.test,aes(x=PC1,y=PC2,color=Diet,shape=Antibiotics,Fill=Time))+
  geom_point(size=3,alpha=0.5)+
  scale_color_manual(values = c("black","white") )

它产生的图:

enter image description here

我想如果同时指定了颜色和填充,那么它们都会显示出来。 我想给抗生素涂上黑色轮廓,为时间涂满颜色。 现在没有时间。 有关如何同时查看这三个因素的任何帮助。

谢谢

1 个答案:

答案 0 :(得分:0)

是的,我有填字错误。最后,我想出了如何使图例对应的方法。这是我的最终答案。

ggplot(dat.pcx.annot,aes(x=PC1,y=PC2,color=Diet,shape=Antibiotics,fill=Time))+
  geom_point(size=3)+
  scale_shape_manual(values = c(21, 22) )+
  scale_color_manual(values = c("black","white") )+
  scale_fill_manual(values=c("#EC9DAE","#AEDE94"))+
  xlab(PC1var)+
  ylab(PC2var)+
  guides(fill=guide_legend(override.aes=list(shape=21)))+
  guides(color=guide_legend(override.aes=list(shape=21)))
  guides(fill=guide_legend(override.aes=list(shape=21,fill=c("#EC9DAE","#AEDE94"),color=c("black","white"))))

enter image description here

ggsave(“ cohort2_pca.pdf”)