我有一個 ggplot,我想用基于 ID 的顏色填充點并基于組設定形狀,使用形狀 21 和 22,它們具有輪廓顏色和“填充”顏色。圖中的點是正確的,但右側的圖例或指南將所有點顯示為黑色。
這是一個代碼示例:
library(ggplot2)
data <- list()
data[['ID']] <- c("1","1","1","1","2","2","2","2","3","3","3","3")
data[['Group']] <- rep(c("A","B"),6)
data[['x']] <- seq(1:12)
data[['y']] <- log(data[['x']])
df <- data.frame(data)
df
# I want to use point shapes with outlines (shapes 21,22, and 23) assigned
# to different 'Group' (A or B), outline is set at black.
# Colors are manual filled as red, blue and green based on 'ID' ("1", "2", or "3")
# The problem is the ggplot guide for Color is all black.
# The guide/legend doesn't reflect the point fill color based on ID.
ggplot(df, aes(x=x, y=y, fill=ID))
geom_point(aes(shape=Group, fill=ID), size=6)
scale_fill_manual(values = c("1"="red", "2"="blue", "3"="green"))
scale_shape_manual(values=c("A"=21,"B"=22))
`
我的期望是圖例將反映點顏色。Ggplot 形狀 21 和 21 具有填充屬性(可以填充)。這意味著 scale_fill_manual 是正確的選擇。我在 ggplot 級別嘗試了填充和顏色的組合,geom_point 呼叫(在 aes 中),但我無法找到可行的組合。似乎圖例從形狀的輪廓中獲取顏色,在這種情況下為黑色(默認)。

uj5u.com熱心網友回復:
guides(fill = guide_legend(override.aes = list(shape=21)))
問題是填充圖例不“知道”您正在為點使用非默認形狀,因此我們可以覆寫默認值以使其使用形狀 21。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/531114.html
標籤:rggplot2
上一篇:我想使用geom_abline在一個圖中將多條線一起繪制,但每條線都有唯一的x限制
下一篇:無法生成具有對數刻度的分組箱線圖
