R:ggplot在情节传奇中形状和填充美学之间的不兼容性

时间:2017-11-09 18:28:55

标签: r ggplot2

我正在尝试使用填充和形状美学来创建绘图。情节看起来很精彩,但传说并没有被填充美学所染色。你能帮我解决一下吗?

这是一个示例代码

#Example dataset
bio_rep = rep(c(1:3), 4)
category = rep(c("w", "x", "y", "z"), each = 3)
ranking = sample(c("s","a","b","c"), 12, replace = T)
score = runif(12)

df = data.frame(bio_rep, category, ranking, score)

> df
   bio_rep category ranking      score
1        1        w       b 0.12496463
2        2        w       b 0.82229942
3        3        w       b 0.20121351
4        1        x       a 0.06352934
5        2        x       s 0.57510752
6        3        x       a 0.54471793
7        1        y       a 0.87203684
8        2        y       c 0.32858945
9        3        y       a 0.06234144
10       1        z       c 0.41124401
11       2        z       s 0.62253128
12       3        z       a 0.42499771

现在的情节

require(ggplot2)

ggplot(df, aes(category, score, shape = factor(bio_rep), fill = ranking))+
  geom_point(size = 3)+
  scale_shape_manual(values = c(21,22,23))

如您所见,排名未在图例

中着色

Image link here!

你知道怎么解决吗?

非常感谢提前 MP

1 个答案:

答案 0 :(得分:3)

原因是“排名”图例使用的默认形状没有fill美学(仅color)。您可以使用override.aes更改此形状以匹配其他图例中的形状:

ggplot(df, aes(category, score, shape = factor(bio_rep), fill = ranking))+
    geom_point(size = 3)+
    scale_shape_manual(values = c(21,22,23)) +
    guides(fill = guide_legend( override.aes = list(shape=21) ))

enter image description here