假設我在下面ggplot畫一個stacked bar chart
library(ggplot2)
col_define = c('red', 'orange', 'blue', 'lightblue')
names(col_define) = c('A', 'B', 'C', 'D')
data = rbind(data.frame('grp1' = 'X', 'grp2' = c('A', 'B', 'C', 'D'), 'val' = c(1,2,3,4)), data.frame('grp1' = 'Y', 'grp2' = c('A', 'B', 'C', 'D'), 'val' = c(1,2,3,4) 2))
ggplot(data, aes(x = grp1, fill = grp2, y = val))
geom_bar(stat = 'identity', position = 'stack')
scale_fill_manual(aesthetics = "fill", values = col_define,
breaks = names(col_define))
它將所有顏色放在一個圖例中。但是在我的情況下,我基本上有兩組顏色,即一組用于A & B,第二組用于C & D
我在ggplot2 中進行了類似的討論:將圖例分成兩列,每列都有自己的標題,其中有一種方法可以使用包ggnewscale或relayer
但是看起來,這種方法只能應用于ordinary bar chart,geom_bar可以多次呼叫。
相反,geom_bar在我的情況下,不能多次呼叫,因為它是一個完整的物件
我正在尋找某種方法來使用ggnewscale或relayer包裝我stack bar chart的圖例中的顏色分組。
正如@stefan 在其中一個答案中所建議的那樣,一種可能的使用方式geom_col.
但是我發現這種方法相當嚴格,因為我不能將此方法應用于alluvial具有與以下相同資料的繪圖
library(ggalluvial)
ggplot(data,
aes(x = grp1, stratum = grp2, alluvium = grp2,
y = val,
fill = grp2))
geom_flow(aes(fill = grp2), alpha = .3)
geom_stratum(aes(color = grp2), alpha = .9)
scale_fill_manual(values = col_define, breaks = names(col_define))
有沒有更通用的方法來分組圖例中的顏色?
uj5u.com熱心網友回復:
由于提供的附加資訊要求解決不同的問題,恕我直言,第二個答案是合適的。基本上,它建立在我的第一個答案的基礎上,我用它ggnewscale來為條形圖創建分組圖例。在第二步中,我通過 提取此圖例cowplot::get_legend以將其添加到沖積地塊中patchwork。再一次,這并不優雅,但恕我直言,這是實作預期結果的最簡單方法:
注意:我嘗試使用“ggnewscale withggalluvial”,但似乎后者很特別而且有點固執。(;這就是為什么我改用另一種方法的原因。
library(ggplot2)
library(ggnewscale)
library(cowplot)
library(ggalluvial)
library(patchwork)
# Create a grouped legend
p <- ggplot(data, aes(x = grp1, group = grp2, y = val))
geom_col(aes(fill = grp2))
scale_fill_manual(values = col_define, breaks = c("A", "B"), name = "1")
new_scale_fill()
geom_col(aes(fill = grp2))
scale_fill_manual(values = col_define, breaks = c("C", "D"), name = "2")
p_legend <- cowplot::get_legend(p)
# Alluvial plot without legend
p_alluvial <- ggplot(data,
aes(x = grp1, stratum = grp2, alluvium = grp2,
y = val,
fill = grp2))
geom_flow(aes(fill = grp2), alpha = .3)
geom_stratum(aes(color = grp2), alpha = .9)
scale_fill_manual(values = col_define, breaks = names(col_define), aesthetics = c("color", "fill"))
guides(color = "none", fill = "none")
# Alluvial plot with legend via patchwork
p_alluvial p_legend plot_layout(widths = c(10, 1))
#> Warning: `spread_()` was deprecated in tidyr 1.2.0.
#> ? Please use `spread()` instead.
#> ? The deprecated feature was likely used in the ggalluvial package.
#> Please report the issue at
#> <https://github.com/corybrunson/ggalluvial/issues>.
#> Warning: The `.dots` argument of `group_by()` is deprecated as of dplyr 1.0.0.
#> ? The deprecated feature was likely used in the dplyr package.
#> Please report the issue at <https://github.com/tidyverse/dplyr/issues>.

uj5u.com熱心網友回復:
不優雅,但您當然可以簡單地在第一個geom_col之上添加第二個以獲得兩個圖例:
library(ggplot2)
library(ggnewscale)
ggplot(data, aes(x = grp1, group = grp2, y = val))
geom_col(aes(fill = grp2))
scale_fill_manual(values = col_define, breaks = c("A", "B"), name = "1")
new_scale_fill()
geom_col(aes(fill = grp2))
scale_fill_manual(values = col_define, breaks = c("C", "D"), name = "2")

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/523478.html
