0
votes

I am trying to make a PCA plot using ggplot and geom_point. I would like to illustrate 3 factors (Diet, Time, Antibiotics). I thought I could outline the points in black for one factor). However this isn't showing the third factor (Time) for the Fill color.

Here is a subset of my data:

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

Here is the plotting command :

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

And the plot it produces:

enter image description here

I thought if I had both color and fill specified then they would both show. I would like black outlines for Antibiotics, and Fill color for Time. Right now Time is not represented. Any help on how to simultaneously view the 3 factors.

Thanks

1
You can change which shapes you use in scale_shape_manual(). Something like scale_shape_manual(values = c(0, 2) )aosmith
thanks, that helped, but I"m still not seeing the Fill and color factorsuser2814482
The aesthetic is called fill, not Fill. You will also need to choose "fillable" shapes, both for the plot itself and for the legend. Fillable shapes are shapes 21-25.aosmith

1 Answers

0
votes

Yes I had a fill typo. And I finally figured out how to get the legends to correspond. Here is my final answer.

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