0

I'm trying to make a scatterplot of sorts, where the symbol shape and the symbol fill are set by two different variables. Here is an example of my data:

Sample  Experiment  Replicate  EPS  Time  Group
1 1  1  5  24  Wild-type
2 1  3  4.5  24  Wild-type
3 2  2  2  24  Wild-type
4 1  2  6  24  Variant
5 2  1  4  24  Variant
6 1  2  3  48  Wild-type
7 1  3  2.5  48  Wild-type
8 2  3  3.5  48  Wild-type
9 1  2  3.5  48  Variant
10 2  2  6.5  48  Variant
11 1  1  3  72  Wild-type
12 2  3  3.5  72  Wild-type
13 1  3  9.5  72  Variant
14 2  3  12.5  72  Variant

Here is the code I'm using. Everything works fine except there is no fill in any of my symbols:

fig.one<-read.table(file='data/Figure1.txt', header=TRUE)

fig.one$time.cat[fig.one$Time == 24] <- 2.5
fig.one$time.cat[fig.one$Time == 48] <- 6
fig.one$time.cat[fig.one$Time == 72] <- 9.5

fig.one$scat.adj[fig.one$Group=='Wild-type']<- -0.50
fig.one$scat.adj[fig.one$Group=='Variant']<- 0.50

my.pch<-c(21,24)
my.bg<-c('black','white')

ggplot(fig.one, aes(time.cat, EPS, shape=my.pch[Experiment]),fill=my.bg[factor(Group)]) +
  geom_jitter(aes(time.cat + scat.adj,EPS),
               position=position_jitter(width=0.2,height=0),
               alpha=0.6,
               size=3) +
  scale_fill_identity() +
  scale_shape_identity() +
  scale_x_continuous("Time since inoculation (hours)", breaks=c(2.5,6,9.5),labels=c( "24", "48", "72"), limits=c(1,11)) +
  ylab("EPS (grams per litre)") +
  theme(axis.text=element_text(size=12, face='bold'),
        axis.title=element_text(size=14, face='bold'),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank())

Thanks as always for help!

Ensa
  • 105
  • 1
  • 5

2 Answers2

0

If it's the fill color that's giving you trouble you might try using color instead of fill. Here's a reproducible example:

my.bg <- c('black','white', 'blue')

ggplot(iris, aes(Sepal.Width, Petal.Length, color = my.bg[factor(Species)])) +
  geom_jitter() +
  scale_color_identity()
amanda
  • 321
  • 5
  • 12
  • Thank you. I've tried this reproducible example and it works with either color or fill. My code still doesn't. Trying to work out what I've broken. – Ensa Nov 10 '17 at 05:47
  • Gotcha. What jumps out at me is that it looks like you're setting your `aes` twice which you don't need to do; you've only got once shape layer, `geom_jitter`. So you can set `aes` in either the `ggplot()` function or inside `geom_jitter()`. – amanda Nov 10 '17 at 05:51
0

Your fill argument is defined outside the aes() function. Try re-writing the first ggplot line as

ggplot(fig.one, aes(time.cat, EPS, shape=my.pch[Experiment], fill=my.bg[factor(Group)])) + ...

JeffR
  • 524
  • 3
  • 10