鑒于此資料:
mtcars %>%
gather(-mpg, -hp, key = "var", value = "value") %>%
mtcars %>%
gather(-mpg, -hp, key = "var", value = "value") %>%
ggplot(aes(x = value, y = mpg, color = hp))
geom_point()
facet_wrap(~ var, scales = "free")
theme_bw()
如您所見,hp 有一個圖例(隱藏了低值)。我想要每個面板的圖例。
uj5u.com熱心網友回復:
正如 stefan 在他的評論中已經提到的那樣,我們可以使用patchwork包:
library(ggplot2)
library(dplyr)
library(patchwork)
mtcars_list <- mtcars %>%
gather(-mpg, -hp, key = "var", value = "value") %>%
group_split(var)
gg_list = lapply(mtcars_list, function(x) {
ggplot(x, aes(x = value, y = mpg, color = hp))
geom_point()
facet_wrap(~ var, scales = "free")
theme_bw()
})
# patchwork
wrap_plots(gg_list)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/420809.html
標籤:
