在PCA图中使用scale_fill_manual向geom_mark_ellipse添加颜色的问题
我最近发布了有关如何在scale_fill_manual中使用向量的文章,并得到了一些使用setNames的好建议。到目前为止,这工作得很好,但现在我的 PCA 图出现了一些我无法弄清楚的小故障。
首先,我为样本名称和颜色创建了向量,如下所示:
DGEGroup1Col <- "steelblue3"
DGEGroup2Col <- "grey56"
DGEGroup1 <- "E1E2"
DGEGroup2 <- "pCDNA"
然后我想在 PCA 图中调用它,如下所示:
PCAP1 <- ggplot(ScoresPC1PC2)+
geom_point(mapping=aes(x=PC1, y=PC2, fill = Groups), size = 4.5, color = "black", shape = 21)+
scale_fill_manual(values =setNames(c(DGEGroup1Col, DGEGroup2Col), nm = c(DGEGroup1, DGEGroup2)))+
ggforce::geom_mark_ellipse(mapping=aes(x=PC1,y=PC2, fill= Groups, color= Groups),alpha=0.2,
position = position_jitter(width=0.3,height=0.3))+
scale_colour_manual(values =setNames(c(DGEGroup1Col, DGEGroup2Col), nm = c(DGEGroup1, DGEGroup2)))+
geom_hline(yintercept=0, linetype="dashed", color = "black", size = 0.8)+
geom_vline(xintercept=0, linetype="dashed", color= "black", size = 0.8)+
xlab(paste("PC1 ", "(",ExpPC1P2[1],"%)", sep=""))+
ylab(paste("PC2 ", "(",ExpPC1P2[2],"%)", sep=""))+
theme_minimal()+
theme(axis.title.y = element_text(size = 18, family = "sans"),
legend.position = "bottom",
legend.text = element_text(size = 18, family = "sans"),
axis.text.x = element_text(colour ="black", size = 16, family = "sans"),
axis.text.y = element_text(colour ="black", size = 16, family = "sans"),
axis.title.x = element_text(colour = "black", size = 18, family = "sans"),
legend.title = element_blank(),
panel.background = element_blank(),
axis.line = element_blank(),
plot.title = element_text(size = 10, hjust = 0.5),
legend.text.align = 0,
legend.key = element_rect(fill = "white", colour = "white"))
,我希望两个椭圆都填充各自的颜色,并且我认为代码对此是正确的。我不明白为什么它不起作用,为什么有时它适用于灰色样本。
这是 ScoresPC1PC2 的 dput() 输出
structure(list(PC1 = c(-35.2409313607162, -23.0341545349512,
-19.840742842832, -36.5827751478223, -28.6911983398547, -40.2380866994142,
39.2026834777075, 37.4165448661976, 47.2975300548786, 59.7111305268072
), PC2 = c(46.8503978923974, -13.5039005407852, -5.8433732590231,
25.7422241399349, -21.2022920781914, -42.5376795322046, 36.9581226051217,
-25.3346801555976, 47.3421448172082, -48.4709638888607), Samps = c("E1E2 1",
"E1E2 2", "E1E2 3", "E1E2 4", "E1E2 5", "E1E2 6", "pCDNA 1",
"pCDNA 2", "pCDNA 3", "pCDNA 4"), Groups = c("E1E2", "E1E2",
"E1E2", "E1E2", "E1E2", "E1E2", "pCDNA", "pCDNA", "pCDNA", "pCDNA"
)), row.names = c("E1E2 1", "E1E2 2", "E1E2 3", "E1E2 4", "E1E2 5",
"E1E2 6", "pCDNA 1", "pCDNA 2", "pCDNA 3", "pCDNA 4"), class = "data.frame")
提前感谢您的帮助!
I recently posted on how to use vectors in scale_fill_manual and got some great advice to use setNames. This has worked fine so far however now I'm having some glitches with a PCA plot that I cannot figure out.
First I have created vectors for sample names and colours as follows:
DGEGroup1Col <- "steelblue3"
DGEGroup2Col <- "grey56"
DGEGroup1 <- "E1E2"
DGEGroup2 <- "pCDNA"
Then I want to call this in a PCA plot as follows:
PCAP1 <- ggplot(ScoresPC1PC2)+
geom_point(mapping=aes(x=PC1, y=PC2, fill = Groups), size = 4.5, color = "black", shape = 21)+
scale_fill_manual(values =setNames(c(DGEGroup1Col, DGEGroup2Col), nm = c(DGEGroup1, DGEGroup2)))+
ggforce::geom_mark_ellipse(mapping=aes(x=PC1,y=PC2, fill= Groups, color= Groups),alpha=0.2,
position = position_jitter(width=0.3,height=0.3))+
scale_colour_manual(values =setNames(c(DGEGroup1Col, DGEGroup2Col), nm = c(DGEGroup1, DGEGroup2)))+
geom_hline(yintercept=0, linetype="dashed", color = "black", size = 0.8)+
geom_vline(xintercept=0, linetype="dashed", color= "black", size = 0.8)+
xlab(paste("PC1 ", "(",ExpPC1P2[1],"%)", sep=""))+
ylab(paste("PC2 ", "(",ExpPC1P2[2],"%)", sep=""))+
theme_minimal()+
theme(axis.title.y = element_text(size = 18, family = "sans"),
legend.position = "bottom",
legend.text = element_text(size = 18, family = "sans"),
axis.text.x = element_text(colour ="black", size = 16, family = "sans"),
axis.text.y = element_text(colour ="black", size = 16, family = "sans"),
axis.title.x = element_text(colour = "black", size = 18, family = "sans"),
legend.title = element_blank(),
panel.background = element_blank(),
axis.line = element_blank(),
plot.title = element_text(size = 10, hjust = 0.5),
legend.text.align = 0,
legend.key = element_rect(fill = "white", colour = "white"))
This creates the following graph:
Sometimes when I run the same code I get this graph:
Ideally, I want both elipses to be filled with the respective colours and I thought that the code is correct for that. I can't understand why it doesn't and why sometimes it works for the grey sample.
Here is the dput() output of ScoresPC1PC2
structure(list(PC1 = c(-35.2409313607162, -23.0341545349512,
-19.840742842832, -36.5827751478223, -28.6911983398547, -40.2380866994142,
39.2026834777075, 37.4165448661976, 47.2975300548786, 59.7111305268072
), PC2 = c(46.8503978923974, -13.5039005407852, -5.8433732590231,
25.7422241399349, -21.2022920781914, -42.5376795322046, 36.9581226051217,
-25.3346801555976, 47.3421448172082, -48.4709638888607), Samps = c("E1E2 1",
"E1E2 2", "E1E2 3", "E1E2 4", "E1E2 5", "E1E2 6", "pCDNA 1",
"pCDNA 2", "pCDNA 3", "pCDNA 4"), Groups = c("E1E2", "E1E2",
"E1E2", "E1E2", "E1E2", "E1E2", "pCDNA", "pCDNA", "pCDNA", "pCDNA"
)), row.names = c("E1E2 1", "E1E2 2", "E1E2 3", "E1E2 4", "E1E2 5",
"E1E2 6", "pCDNA 1", "pCDNA 2", "pCDNA 3", "pCDNA 4"), class = "data.frame")
Thanks in advance for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论