簡體   English   中英

ggplot2 填充圖例未顯示正確的“填充”顏色

[英]ggplot2 fill legend does not display the correct “fill” color

我對這個問題困惑了很長時間。 一個簡單的數據框構造如下

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

圖形正常,但是圖例不正確

在此處輸入圖片說明

我想,也許不同的形狀(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)

填充顏​​色的傳說終於正確了......

在此處輸入圖片說明

那么這里會發生什么呢? 這是一個錯誤嗎? 是否可以只添加單層但具有不同的形狀(21 到 25)?

一種可能的解決方案是可以添加組件guides() ,如

p + 
  guides(fill = guide_legend(override.aes = list(fill = uniFill,
                                                 shape = 21)))

但我更感興趣的是為什么p不起作用(圖例)

你的圖例在你的第一個例子中不起作用的主要原因是你沒有把你的形狀放在美學中。

我還有其他一些建議:不要在數據框中定義顏色; 而是定義一列以使用代碼更改美感。 然后明確定義填充和形狀值。 每個音階都需要具有相同的名稱 - 在本例中為“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

(編輯)如果您的填充和形狀美學不匹配,除了使用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

帶有兩個圖例的點圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM