這是我的資料和相應的ggplot
library(ggplot2)
library(ggnewscale)
library(cowplot)
library(ggalluvial)
library(patchwork)
data =
structure(list(grp1 = c("X", "X", "X", "X", "Y", "Y", "Y", "Y"
), grp2 = c("A", "B", "C", "D", "A", "B", "C", "D"), val = c(1,
2, 3, 4, 3, 4, 5, 6)), row.names = c(NA, -8L), class = "data.frame")
col_define = c('red', 'orange', 'blue', 'lightblue')
names(col_define) = c('A', 'B', 'C', 'D')
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")
#guides(fill=guide_legend(order = 1, ncol = 2))
theme(legend.position="top", legend.direction = 'vertical')
p_legend <- cowplot::get_legend(p)
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")
p_legend
theme(legend.position="top")
plot_layout(widths = c(50, 1))
我想將最終繪圖的圖例放在繪圖視窗內的頂部居中位置(即淺灰色區域)。我嘗試了不同的輸入,例如theme(legend.position="top"),但這似乎不起作用。
有沒有辦法將圖例位置固定在繪圖視窗中頂部居中位置的上方繪圖?
筆記:
這個情節來自我之前的帖子的公認答案

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/523468.html
標籤:rggplot2牛图
上一篇:為什么在抖動點時在ggplot2中對兩組中的整數進行不同的處理?
下一篇:按每個子組計算比例(dplyr)
