r - 显示 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 个因素的任何帮助。
谢谢
解决方案
是的,我有一个填充错字。我终于想出了如何让传说对应起来。这是我的最终答案。
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")
推荐阅读
- javascript - 我想知道如何从所选文件的路径中使用相同的路径下载 XML
- multithreading - 使用互斥锁和条件变量进行线程同步
- python - 为什么使用 Socket IO 而不仅仅是 Socket?
- uwp - 运行 win 10 IOT 的树莓派与 arduino 之间如何通信?
- hibernate - Hibernate 和 MariaDB 仅将时间戳存储到秒精度?
- c++ - 如何初始化 const std::vector
- vba - Excel VBA:单击两个按钮(形状)在电子表格打开时运行两个单独的 VBA 脚本
- java - 如何从其他类获取对节点的引用?
- json - 如何将 Azure JSON 模板导入 Visio Diagram?
- r - R xml2从xbrl文件中提取元素