根据艾伦·卡梅伦的评论编辑:
library(tidyverse)
library(ggcorrplot)
library(reshape2)
iris %>%
group_by(Species) %>%
summarise(correlation = list(cor(across(where(is.numeric))))) %>%
ungroup() %>%
mutate(correlation_df = map(correlation, ~ melt(.x, varnames = c("Var1", "Var2")))) %>%
select(Species, correlation_df) %>%
unnest(correlation_df) %>%
ggplot(aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
scale_fill_gradient2(limit = c(-1, 1)) +
labs(x = NULL,
y = NULL,
fill = NULL) +
theme_bw() +
facet_wrap(~Species) +
theme(legend.key.height = unit(1, "null"),
legend.margin = margin(0, 0, 0, 0))
通过在
legend.key.height
参数中,参考线不仅垂直适合绘图,而且如果更改绘图的纵横比,还会拉伸。根据
this
tidyverse.org文章,此功能仍处于试验阶段。
(根据艾伦·卡梅伦的评论编辑)然后,您可以添加
legend.margin = margin(0, 0, 0, 0)
在主题内,传说与情节的顶部和底部垂直对齐。
输出: