54

I'm doing an scatter plot using ggplot. I would like to have points with a particular colour and fill (in plot, colour="blue", fill="cyan4", for ex.) but I can't find how. What I have to this point is:

ggplot(df, aes(own,method)) +
  panel.configuration +
  scale_shape_identity() +  #to use the 'plot' shape format.
  geom_point(aes(color = factor(label)), position = "jitter",size=3) +  

(In previous geom_pointI tried adding shape=21 as I would have done in plot)

  scale_colour_manual(values=c("A"="chocolate3","B"="cyan4")) +
  scale_fill_manual(values=c("A"="green", "B"="red")) + #DOES NOTHING...
  xlim(7,47) + ylim(7,47)+ ... etc.

This is what i get without "shape=21"

enter image description here

This is what I get when I add "shape=21". In both cases it ignores scale_fill enter image description here

I also tried adding fill=c("blue","red") in geom_point, but R complains: "Error: Incompatible lengths for set aesthetics: shape, size, fill".

Any suggestions about how to get it? What is wrong with scale_fill in my code?

Thank you very much!

Data (df) looks like:

21 15 A
24 16 A
24 17 A
28 14 A
24 15 A
22 15 A
20 18 A
24 18 A
34 9 B
38 12 B
41 19 B
42 13 B
36 12 B
40 17 B
41 14 B
37 12 B
40 13 B
37 15 B
35 15 B
PGreen
  • 3,239
  • 3
  • 24
  • 29

1 Answers1

112

You'll have to use shapes from 21 to 25. These are the ones that have colour and fill properties:

ggplot(df, aes(own, method)) + 
     geom_point(colour="white", shape=21, size = 4, 
     aes(fill = factor(label))) + 
     scale_fill_manual(values=c("blue", "cyan4"))

If you want different colours for colour as well, then:

ggplot(df, aes(own, method)) + 
      geom_point(aes(colour=factor(label), 
      fill = factor(label)), shape=21, size = 4) + 
      scale_fill_manual(values=c("blue", "cyan4")) + 
      scale_colour_manual(values=c("white", "black"))
Arun
  • 116,683
  • 26
  • 284
  • 387
  • 1
    Thank you Arun. Your suggestions works perfectly but it fills all dots in the same colour. Is it possible to specify a particular color for each as in `plot`? I would edit it to provide the data. – PGreen Apr 12 '13 at 07:59
  • 1
    Yes. It is possible. I'm just looking for a data to create the plot.. :) – Arun Apr 12 '13 at 08:01
  • 1
    @PGreen, try this edit. You can do the same for `colour` with `scale_colour_manual(values=...)` if you insert `colour` within `aes(.)` (edited). – Arun Apr 12 '13 at 08:04
  • 1
    The colours/fills are assigned according to the levels of your factor. – Arun Apr 12 '13 at 08:08
  • 2
    Perfect! That's it! Finally, only 2-color are meaningful, but I wanted to add border since I apply jitter and border helps to visualize crowded regions. Different colors for both colour and fill was just an exta-detail, just to respect the format of my older script in `plot`. Thanks a lot Arun, really helpful and efficient ;-) – PGreen Apr 12 '13 at 08:20
  • 1
    am i crazy or is it incredibly hard to find documentation on the behaviour of these shapes...? I just burned an hour trying to set fill... – badgley Sep 15 '15 at 16:48