4

I'm very new to R and I'm trying to build a scatter plot that codes my data according to shape, colour and fill.I want 5 different colours, 3 different shapes, and these to be either filled or not filled (in an non filled point, I would still want the shape and the colour). My data looks basically like this:

blank.test <- read.table(header=T, text="Colour Shape Fill X13C X15N
1       B     B    A   16   10
2       D     A    A   16   12
3       E     A    B   17   14
4       C     A    A   14   18
5       A     A    B   13   18
6       C     B    B   18   13
7       E     C    B   10   12
8       E     A    B   11   10
9       A     C    B   14   13
10      B     A    A   11   14
11      C     B    A   11   10
12      E     B    A   11   19
13      A     B    A   10   18
14      A     C    B   17   16
15      E     B    A   16   13
16      A     C    A   16   14")

If I do this:

ggplot(blank.test, aes(x=X13C, y=X15N,size=5)) + 
                    geom_point(aes(shape=Shape,fill=Fill,color=Colour))

I get no filled or unfilled data points

enter image description here

I did a little a little research and it looked like the problem was with the symbols themselves, which cannot take different settings for line and fill; it was recommended I used shapes pch between 21 and 25

But if I do this:

ggplot(blank.test, aes(x=X13C, y=X15N,color=(Colour), shape=(Shape),fill=(Fill),size=5)) + 
                 geom_point() + scale_shape_manual(values=c(21,22,25))`

I still don't get what I want

enter image description here

I also tried playing around with scale_fill_manual without any good result.

answer42
  • 63
  • 1
  • 7
  • we can't really use your data, you should use dput() and I don't see your 5 in your legend. – MLavoie Dec 06 '15 at 15:48
  • 1
    I think the downvote is somewhat over-zealous. In the scale of first questions (or indeed, not even just first) , this has got to be quite high ... data / code / images. – user20650 Dec 06 '15 at 15:52
  • 2
    @MLavoie, I'm very new to R and it's also my first programming language, so I don't really know what dput() is. It is not obvious when asking a question where it is, but I'll try and figure it out; and the 5 is quite clear in the first image – answer42 Dec 06 '15 at 16:04
  • @rawr Thank you! that worked quite well! – answer42 Dec 06 '15 at 16:04

2 Answers2

2

I can get the plot to work just fine, but the legend seems to absolutely insist on being black for fill. I can't figure out why. Maybe someone else has the answer to that one.

The 5 being on the legend is cause by having it inside the aes, where only elements that change with your data belong.

Here is some example code:

ggplot(blank.test, aes(x = X13C, y = X15N, color = Colour, shape = Shape, fill = Fill)) + 
  geom_point(size = 5, stroke = 3) + 
  scale_shape_manual(values=c(21,22,25)) +
  scale_color_brewer(palette = "Set2") +
  scale_fill_brewer(palette = "Set1") +
  theme_bw()

enter image description here

Axeman
  • 32,068
  • 8
  • 81
  • 94
  • Hi Thanks for your input! However this is not quite what I want, since I would like the fill to be either white/transparent or the same colour as the outline – answer42 Dec 11 '15 at 18:36
1

I don't think you can use fill for points. What I would do is create an interaction between fill and shape and use this new factor to define your shape and fill/open symbols

blank.test$inter <- with(blank.test, interaction(Shape,  Fill))

and then for your plot I would use something like that

ggplot(blank.test, aes(x=X13C, y=X15N)) + 
                    geom_point(aes(shape=inter,color=Colour)) + scale_shape_manual(name="shape", values=c(0,15,1, 16, 2, 17)) + scale_color_manual(name="colour", values=c("red","blue","yellow", "green", "purple"))
MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • Thank you! This totally worked! My legend, however looks weird because it tells me I have 6shapes: A.A, A.B, A.A and so forth. Any idea on how to manually set it up? Can't find it anywhere – answer42 Dec 11 '15 at 18:39
  • What you were asking was not possible. So that's why you needed an interaction of shape and fill. you could change the name of the legend to + scale_shape_manual(name="shape*fill"..... – MLavoie Dec 11 '15 at 19:14