answer42
answer42

Reputation: 63

R scatter plot by shape, colour and fill

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.

Upvotes: 4

Views: 4281

Answers (2)

Axeman
Axeman

Reputation: 35392

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

Upvotes: 2

MLavoie
MLavoie

Reputation: 9886

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

Upvotes: 1

Related Questions