我有一個簡單的情節:
library(ggplot2)
ggplot(mtcars, aes(mpg, disp, fill = "fill"))
geom_violin(aes(linetype = "pattern"),
key_glyph = draw_key_path)

由reprex 包(v0.3.0)于 2021 年 11 月 8 日創建
如何更改圖例以將其顯示fill為正方形,但將linetype圖案顯示為一條線而不是正方形?
uj5u.com熱心網友回復:
我認為沒有辦法在本地做到這一點。這是一個 hacky 解決方案,通過結合guide_legend(override.aes = list(...)).
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.1.1
my_key <- function(data, params, size) {
if (all(is.na(data$fill))) {
draw_key_path(data, params, size)
} else {
draw_key_polygon(data, params, size)
}
}
ggplot(mtcars, aes(mpg, disp, fill = "fill"))
geom_violin(aes(linetype = "pattern"),
key_glyph = my_key)
guides(linetype = guide_legend(override.aes = list(fill = NA)))

由reprex 包(v2.0.1)于 2021 年 11 月 8 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/354171.html
上一篇:如何在組內將第一個值與每個后續值進行比較,直到滿足條件
下一篇:R:回傳所有簡單路徑的邊串列
