0

我正在尝试制作各种散点图,其中符号形状和符号填充由两个不同的变量设置。这是我的数据示例:

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

这是我正在使用的代码。一切正常,除了没有填写我的任何符号:

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

一如既往地感谢您的帮助!

4

2 回答 2

0

如果是填充颜色给您带来麻烦,您可以尝试使用color而不是fill. 这是一个可重现的示例:

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

ggplot(iris, aes(Sepal.Width, Petal.Length, color = my.bg[factor(Species)])) +
  geom_jitter() +
  scale_color_identity()
于 2017-11-10T05:38:55.547 回答
0

您的 fill 参数是在 aes() 函数之外定义的。尝试将第一条 ggplot 行重写为

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

于 2017-11-10T07:08:49.290 回答