首页 > 解决方案 > 显示 3 个因素 ggplot geom

问题描述

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

这是我的数据的一个子集:

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

它产生的情节:

在此处输入图像描述

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

谢谢

标签: rggplot2

解决方案


是的,我有一个填充错字。我终于想出了如何让传说对应起来。这是我的最终答案。

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

在此处输入图像描述

ggsave("cohort2_pca.pdf")


推荐阅读