簡體   English   中英

如何為 ggplot2 的每一列添加 y 軸 label?

[英]How can I add a y-axis label for each column of the ggplot2?

例如我想在第二列 y 軸上添加一個 log10(Sepal.Width) label

data(iris)
iris$Sepal.Width[iris$Species=="versicolor"] <- log10(iris$Sepal.Width[iris$Species=="versicolor"])
p <- iris %>% 
  ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
  theme_classic()+
  geom_point()
p+  facet_wrap(~Species,scales = "free")

向每個面板(或我知道的唯一一個)添加標題的一種選擇是創建單獨的圖並使用例如patchwork將它們粘合在一起。 為此,我按分面變量split數據,使用自定義繪圖 function 最后使用Map循環拆分數據和軸標題向量:

library(ggplot2)
library(patchwork)

iris2 <- iris
iris2$Sepal.Width[iris2$Species == "versicolor"] <- log10(iris2$Sepal.Width[iris2$Species == "versicolor"])

plot_fun <- function(.data, y) {
  ggplot(.data, aes(x = Sepal.Length, y = Sepal.Width)) +
    theme_classic() +
    geom_point() +
    facet_wrap(~Species, scales = "free") +
    labs(y = y)
}

y_title <- c("Sepal.Width", "log10(Sepal.Width)", "Sepal.Width")
iris_split <- split(iris2, iris2$Species)

Map(plot_fun, iris_split, y_title) |> 
  wrap_plots()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM