0

I would like to make a scatter plot with shape, color and fill as coding for my data. Everything looks just fine with shape and color, however the fill option doesn't seem to respond. I've checked already other similar posts to this topic, but still can't figure out how to deal with this. What I want to have in my case is Filter column filled based on two different filter sizes (0.22=hollow, 0.45=solid) and everything according to my Time coloring. Please find my dataset and code that I was using,

 Samples    Filter  Type    Time    Coord1  Coord2
 45P116 0.45    Oil Incubation  16  -0.177125327477 0.0329641384336
 22P264 0.22    Oil Incubation  64  -0.036421737819 0.41278336259
 45P113 0.45    Oil Incubation  13  -0.183501817053 -0.0333774595889
 45BL30 0.45    Control 30  -0.0169216562011    -0.160467270544
 22P231 0.22    Oil Incubation  30  -0.0742549515959    0.134945242558
 22P116 0.22    Oil Incubation  16  -0.168257444167 0.0425318235085
 22P164 0.22    Oil Incubation  64  -0.0995650111648    0.321208450137
 22BL64 0.22    Control 64  0.344181403614  0.011496893707
 45P29  0.45    Oil Incubation  9   -0.139337303592 -0.154032242023
 22P216 0.22    Oil Incubation  16  -0.182624942475 0.036656544869
 22P30  0.22    Oil Incubation  0   0.232890938751  -0.0365286035332
 45P23  0.45    Oil Incubation  3   0.335212340096  0.00229714294784
 22P364 0.22    Oil Incubation  64  -0.0265850037098    0.267838399813
 45P26  0.45    Oil Incubation  6   -0.0663530024752    -0.123614294282
 45P316 0.45    Oil Incubation  16  -0.180387066353 0.0244231332556
 22P16  0.22    Oil Incubation  6   -0.0938690796601    -0.123292570064
 22P13  0.22    Oil Incubation  3   -0.000929711149241  -0.0840936522112
 22P36  0.22    Oil Incubation  6   -0.0846096566973    -0.126315336717
 22P39  0.22    Oil Incubation  9   -0.145400678025 -0.171058987178
 45P164 0.45    Oil Incubation  64  -0.160359423757 0.439794230436
 22P19  0.22    Oil Incubation  9   -0.138575642598 -0.151960075733
 45P313 0.45    Oil Incubation  13  -0.176079013758 0.050997652543
 45BL64 0.45    Control 64  0.0288347544654 -0.1458771364
 45P330 0.45    Oil Incubation  30  -0.11228777477  0.204236728786
 45P36  0.45    Oil Incubation  6   -0.0761185616066    -0.121261486374
 45P130 0.45    Oil Incubation  30  -0.094228858564 0.100652965768
 22BL0  0.22    Control 0   0.239232948872  -0.036785772233
 22P213 0.22    Oil Incubation  13  -0.18113904214  -0.0287178094404
 22BL6  0.22    Control 6   -0.0600555194535    -0.194650371605
 45P33  0.45    Oil Incubation  3   0.340399779328  0.0171852693215
 45P30  0.45    Oil Incubation  0   0.431938929593  0.0196841881272
 45BL6  0.45    Control 6   0.357256101911  -0.0188140900434
 45BL9  0.45    Control 9   0.303457604982  -0.0443188527517
 22P316 0.22    Oil Incubation  16  -0.164460159926 -0.0129812590712
 22P313 0.22    Oil Incubation  13  -0.188614376178 0.00058901489101
 45P39  0.45    Oil Incubation  9   -0.149217759045 -0.167311625113
 22P131 0.22    Oil Incubation  30  -0.0651994575885    0.0656871734895
 22BL31 0.22    Control 30  0.335856403002  0.0724160545763
 45P19  0.45    Oil Incubation  9   -0.142421940226 -0.160880360066
 22P331 0.22    Oil Incubation  30  -0.0500301022508    0.0416246477279
 45BL0  0.45    Control 0   0.381611397751  0.0200030589204
 22P113 0.22    Oil Incubation  13  -0.192581008392 -0.00938532222554
 45P16  0.45    Oil Incubation  6   -0.085999339488 -0.121939087208
 45BL3  0.45    Control 3   0.332023830601  0.036732406277
 45P13  0.45    Oil Incubation  3   0.315664193595  0.0248256483492
 45P216 0.45    Oil Incubation  16  -0.172909653606 0.030607355003
 45P213 0.45    Oil Incubation  13  -0.18326004657  0.00570693606563
 22P29  0.22    Oil Incubation  9   -0.150029591452 -0.16569360913
 22P26  0.22    Oil Incubation  6   -0.110220935459 -0.134301793132
 45P364 0.45    Oil Incubation  64  -0.100468231806 0.248528601323
 45BL16 0.45    Control 16  0.341331109786  0.00915399605284
 45BL13 0.45    Control 13  0.155782679403  -0.17960863481
 45P230 0.45    Oil Incubation  30  -0.0452735875021    0.0316966419992

and the code:

    PCoA <- read.delim(file.choose(), header=T)
attach(PCoA)
library(ggplot2)
PCoA$Time <- as.character(PCoA$Time)
PCoA$Filter <- as.character(PCoA$Filter)
cols <- c("0" = "red","3" = "blue","6" = "brown", "9" = "hotpink", "13" = "orange","16" = "olivedrab","30" = "azure3", "64" = "purple")
ggplot(PCoA, aes(x=Coord1, y=Coord2, shape=Type, colour=Time)) + 
  scale_colour_manual(values = cols, breaks=c("0","3","6","9","13","16","30","64")) + 
  geom_point(size=7) + xlab("PC1 (39.7%)") + ylab("PC2 (20.2%)") + 
  geom_hline(aes(yintercept=0)) + geom_vline(aes(xintercept=0)) + 
  theme(legend.text = element_text(size = 16), 
        legend.title = element_text(size=18), 
        axis.title.x = element_text(size=18, vjust=-0.7), 
        axis.title.y = element_text(size=18, vjust=2)) + 
  theme(axis.text = element_text(colour = "black", size="16")) + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.background = element_blank(), 
        axis.line = element_line(colour = "white"))
tonytonov
  • 25,060
  • 16
  • 82
  • 98
  • Usually one only uses color for points. Only some specific shapes respond to both color and fill (numbers 21 till 25). – Axeman Jun 29 '15 at 14:21
  • I believe shapes that I'm using are indeed numbers in that range. Still, it's not responding to color and fill option. – Deni Galinac Ribičić Jun 29 '15 at 14:37
  • Your code doesn't seem to set shapes different from the defaults. See [this question](http://stackoverflow.com/questions/1478532/changing-shapes-used-for-scale-shape-in-ggplot2) for how to change the shapes. You'll need to add something like `scale_shape_manual(values = 21:25)` to your call. – Axeman Jun 29 '15 at 15:17
  • Thanks Axeman, I realize now I haven't specify those shapes. – Deni Galinac Ribičić Jun 30 '15 at 07:39

1 Answers1

3

You need to use the right shapes (21:25) and to specify your fill. This post has some good tips. Although at this point, your data are becoming more difficult to illustrate with a different outline and fill color. (Maybe instead code Time as your fill and use lighter hues for one filter and darker for the other filter -or- make Filter type your outline color).

Here is your graph edited with:

  • data formats coded as.character() within ggplot instead of in the original data frame.
  • legend names specified in scale_parameter_manual()
  • fill defined in aes()
  • shape and fill added as the last line
  • fill legend fixed using override.aes

ggplot(PCoA, aes(x=Coord1, y=Coord2, shape=as.character(Type), colour=as.character(Time), fill=as.character(Filter) )) + #Edit
  scale_colour_manual(name="Time", values = cols, breaks=c("0","3","6","9","13","16","30","64")) + #Edit
  geom_point(size=7) + xlab("PC1 (39.7%)") + ylab("PC2 (20.2%)") + 
  geom_hline(aes(yintercept=0)) + geom_vline(aes(xintercept=0)) + 
  theme(legend.text = element_text(size = 16), 
        legend.title = element_text(size=18), 
        axis.title.x = element_text(size=18, vjust=-0.7), 
        axis.title.y = element_text(size=18, vjust=2)) + 
  theme(axis.text = element_text(colour = "black", size="16")) + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.background = element_blank(), 
        axis.line = element_line(colour = "white")) +
  scale_shape_manual(name="Type", values=c(21,24)) + #Edit
  scale_fill_manual(name="Filter", values=c("black", "white")) + #Edit
  guides(fill = guide_legend(override.aes = list(   #Edit
                fill = c("black", "white"),         #Edit
                shape=22,                           #Edit
                colour="black") ))                  #Edit

Your original plot + edited plot: original and edited plots

Edit

OP asks how the fill can be fine-tuned so that the fill defined by Variable 1 is determined by Variable 2, i.e. if Variable 2 is A, then the points are color-filled but if Variable 2 is B then all the points are white-filled. Since a third Variable dictates the shape, this adds an additional complication.

There are a couple potential solutions:

  1. Combine variables through aes(shape=interaction(Variable2, Variable3)) and specify shapes that allow only-colors (15:20) and colors+fill (21:25).
  2. More streamlined than #1 but with less control is to specify shapes with hollow centers (0:6) instead of filled centers (21:25). Then the fill does not need to be defined.
  3. Load library(scale) and set fill alpha properties (last example here), i.e. scale_fill_manual(values = alpha(c("blue", "red"), .3)) (This solution is probably not feasible for this example.)
  4. Plot points and their aesthetics separately: i.e. geom_point(data= subset, aes())

Approach #1:

ggplot(PCoA, aes(x=Coord1, y=Coord2, 
                 shape=interaction(as.character(Type), Filter),
                 colour=as.character(Time), 
                 fill=as.character(Time) )) + #Edit
  scale_colour_manual(name="Time", values = cols, 
      breaks=c("0","3","6","9","13","16","30","64")) + #Edit
  geom_point(size=7) + xlab("PC1 (39.7%)") + ylab("PC2 (20.2%)") + 
  geom_hline(aes(yintercept=0)) + geom_vline(aes(xintercept=0)) + 
  theme(legend.text = element_text(size = 16), 
        legend.title = element_text(size=18), 
        axis.title.x = element_text(size=18, vjust=-0.7), 
        axis.title.y = element_text(size=18, vjust=2),
        legend.key = element_rect(fill=NA)) +                       #Edit
  theme(axis.text = element_text(colour = "black", size="16")) + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.background = element_blank(), 
        axis.line = element_line(colour = "white")) +
  scale_shape_manual(name="Type/Filter", values=c(21, 16, 24, 17),
                     labels=c("Control, 0.22", "Incubation, 0.22",
                              "Control, 0.45", "Incubation, 0.45") ) + #Edit
  scale_fill_manual(name="Filter", values=rep("white", length(cols)), guide=FALSE )  #Edit

plot with colored fill on treatments and blank fill on controls

Community
  • 1
  • 1
oshun
  • 2,319
  • 18
  • 32
  • Thanks oshun! this is really close to what I want. I was just wondering is it possible to pass "cols" coding only to the filled shapes? That would be exactly what I was looking for. – Deni Galinac Ribičić Jun 30 '15 at 07:36
  • I see what you're saying. Technically, both types of filters are filled (black and white), but you are asking if _Filter_ can be colored or white depending on _Type_ (or vice versa). You can achieve this several ways. I'll edit the solution to reflect one solution, and it would be great to hear if you or others in the SO community have a better solution. – oshun Jun 30 '15 at 19:30
  • This is awesome! I still need to figure out what does code do exactly, but this is really an excellent way to represent what I had in my mind! Unfortunately, I'm not that well versed in R that I would have a better solution. The most I could do was the double coding (Time & Type) :). Thanks again! – Deni Galinac Ribičić Jul 01 '15 at 06:57