ggplot2填充图例未显示正确的“填充”颜色

3

我对这个问题已经感到困惑很长时间了。一个简单的数据框架构建如下:

data <- data.frame(
  x = 1:5,
  y = 5:1,
  fill = c(rep("pink", 3), rep("blue", 2)),
  shape = c(rep(21, 3), rep(22, 2))
)

假设我想显示填充的图例
uniFill <- unique(data$fill)
p <- ggplot(data,
       mapping = aes(x = x,
                     y = y,
                     fill = fill)) + 
  geom_point(shape = data$shape) + 
  # show legend so that I do not call `scale_fill_identity()`
  scale_fill_manual(values = uniFill,
                    labels = uniFill,
                    breaks = uniFill)
p

这个图形还可以,但是图例不正确。

enter image description here

我猜,可能不同形状(21到25)不能合并?那么,我将数据分成两个子集,第一个子集具有形状21,第二个子集具有形状22。
data1 <- data[1:3, ]
data2 <- data[4:5, ]
# > data1$shape
# [1] 21 21 21
# > data2$shape
# [1] 22 22
ggplot(mapping = aes(x = x,
                     y = y,
                     fill = fill)) + 
  geom_point(data = data1, shape = data1$shape) + 
  geom_point(data = data2, shape = data2$shape) + 
  scale_fill_manual(values = uniFill,
                    labels = uniFill,
                    breaks = uniFill)

不幸的是,传说没有改变。然后,我将形状从向量更改为标量,如下:
ggplot(mapping = aes(x = x,
                     y = y,
                     fill = fill)) + 
  geom_point(data = data1, shape = 21) + 
  geom_point(data = data2, shape = 22) + 
  scale_fill_manual(values = uniFill,
                    labels = uniFill,
                    breaks = uniFill)

“填充颜色的传说最终是正确的...”

enter image description here

这里发生了什么?是一个错误吗?能否只添加一个具有不同形状(21到25)的单个图层?
一种可能的解决方案是可以添加组件guides(),如下所示:
p + 
  guides(fill = guide_legend(override.aes = list(fill = uniFill,
                                                 shape = 21)))

但是我更感兴趣的是为什么 p 不起作用(图例)。

2
从技术上讲,这是一个错误:请参见此处:https://github.com/tidyverse/ggplot2/issues/2322 - TarJae
谢谢!看起来修复起来很困难。 - Zane
1个回答

6

在您的第一个示例中,图例无法正常工作的主要原因是因为您没有将形状放入美学(aesthetics)中。

我还有几个建议:不要在数据框中定义颜色;而是定义一个列来使用代码更改美学。然后明确定义填充和形状值。每个比例尺都需要具有相同的名称 - 在本例中为“Legend”。

尝试进行此编辑。

data <- data.frame(
  x = 1:5,
  y = 5:1,
  fill = c(rep("p", 3), rep("b", 2))
 )

uniFill <- c("p"="pink", "b"="blue")
uniShape <- c("p" = 21, "b" = 22)

p <- ggplot(data,
            mapping = aes(x = x,
                          y = y,
                          fill = fill,
                          shape = fill)) + 
  geom_point() + 
  # show legend so that I do not call `scale_fill_identity()`
  scale_fill_manual("Legend",values = uniFill,
                    labels = uniFill)+
  scale_shape_manual("Legend",values = uniShape,
                     labels = uniFill)
p

(编辑) 如果您的填充(fill)和形状(shape)美学不匹配,我认为除了使用 guides 和两个图例之外,没有其他方法。请注意,如果您的属性列是描述性的,您无需设置标签,代码会更清洁(参见形状与填充美学)。

data <- data.frame(
  x = 1:5,
  y = 5:1,
  fill = c(rep("p", 3), rep("b", 2)),
  shape = c(rep("circles", 2), rep("squares", 3))
)

uniFill <- c("p"="pink", "b"="blue")
uniShape <- c("circles" = 21, "squares" = 22)

p <- ggplot(data,
            mapping = aes(x = x,
                          y = y,
                          fill = fill,
                          shape = shape)) + 
  geom_point() + 
  # show legend so that I do not call `scale_fill_identity()`
  scale_fill_manual("Legend fill",values = uniFill,
                    labels = uniFill)+
  scale_shape_manual("Legend shape",values = uniShape  )+
  guides(fill = guide_legend("Legend fill", override.aes = list(shape = 21)))
p

point plot with two legends


谢谢!非常酷。那么,如果形状和填充不匹配怎么办?例如,data <- data.frame( x = 1:5, y = 5:1, fill = c(rep("p", 3), rep("b", 2)), shape = c(rep("a", 2), rep("d", 3)) )。再次感谢您的时间和耐心。 - Zane

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接