簡體   English   中英

在使用變量標簽作為標題和軸標題的同時自動化 ggplots

[英]automate ggplots while using variable labels as title and axis titles

我對 NSE 和繪圖函數有點混淆了。 我試圖在使用變量標簽(不是名稱)標記軸等的同時自動繪制一些圖。 假設我們有一個大型數據集,其中所有變量都已標記。 這里的小例子:

library(tidyverse)
library(sjlabelled)
library(ggplot2)
library(cowplot)
data("diamonds")
diamonds <- diamonds %>% 
  var_labels(
  cut ="nice cut",
  color = "all colours",
  clarity = "very claity all",
  depth = "test depth")

我想要的基本情節是這樣的:

p1 <- ggplot(diamonds, aes(x = cut, y = depth)) + geom_boxplot(aes(fill = cut)) +
  theme_cowplot() + 
  lab(title = "Plot of test depth ~ nice cut",   #based on label variable
                           x = "nice cut",      #based on label variable
                           y = "test depth",    #based on label variable
                         fill = "nice cut")    #based on label variable
p1

我想通過循環其他變量來自動化這個圖。 所以我想要按depth分別繪制vars中列的vars圖 以下是我想要做的。

#firstly i think i should have labels separately (wondering is there a way I can use them directly from variable label?)

my_labels <-   c(
  cut = "nice cut",
  color = "all colours",
  clarity = "very claity all",
  depth = "test depth"
)

#plot function
plot_f <- function(df, x_var, y_var, x_var_label, y_var_label) {
  ggplot(df, aes(x = {{x_var}}, y = {{y_var}})) + geom_boxplot(aes(fill = {{x_var}})) +
    theme_cowplot() + labs(title = paste("Plot of", {{x_var_label}}, "~", {{y_var_label}}),
                           x = {{x_var_label}},
                           y = {{y_var_label}},
                           fill = {{x_var_label}})
}

#variables to cycle through
vars <- c("cut", "color", "clarity")
plot_list <- vars %>% 
  pmap(~plot_f(diamonds, .x, depth, my_labels)) #need to specify y_var_label & x_var_label, is there a 
#way I can just specify my_labels here?

#Finally plot lists
grid.arrange(grobs = plot_list, ncol = 1)

其他代碼嘗試

這就是我正在考慮的方法,我想知道我是否最好嘗試像這里使用plot_list$labels那樣稍后單獨添加plot_list$labels

#Also tried a for loop which worked but the fill didnt (and also missing the variable labels)
p <- list()
for(i in vars){
  p[[i]] <- ggplot(diamonds, aes_string(x = i, y = "depth", fill = i)) + geom_boxplot() +
    #note aes_string instead of aes
    theme_cowplot()
}
grid.arrange(grobs = p, ncol = 1)

編輯

這個更簡單的版本繪圖,但繪圖沒有正確捕獲填充,顯然缺少我想要的變量標簽(粘貼等):

        #plot function
        plot_f <- function(df, x_var, y_var) {
          ggplot(df, aes(x = {{x_var}}, y = {{y_var}})) + geom_boxplot(aes(fill = {{x_var}})) +
            theme_cowplot() 
        }

        plot_f(diamonds, cut, depth )  #plots fine

#variables to cycle through
vars1 <- c("cut", "color", "clarity")
vars1
#[1] "cut"     "color"   "clarity"

#unquoted version
vars <- noquote(vars1)
vars
#[1] cut     color   clarity

    #runs
        plot_list <- vars %>% 
          map(., ~plot_f(diamonds, .x, depth))

        #plots but fill isn't correct
        grid.arrange(grobs = plot_list, ncol = 1)

任何幫助表示贊賞。

感謝@aosmith @Tung 的評論,我想出了以下解決方案:

library(cowplot)
library(gridExtra)
library(ggplot2)
library(tidyverse)

my_labels <-   c(
  cut = "nice cut",
  color = "all colours",
  clarity = "very claity all",
  depth = "test depth"
)

vars <- c("cut", "color", "clarity")

plot_f <- function(df, x_var, y_var, x_var_label, y_var_label) {
  ggplot(df, aes(x = .data[[x_var]], y = .data[[y_var]])) + 
    geom_boxplot(aes(fill = .data[[x_var]])) +
    theme_cowplot() +
    labs(title = paste("Plot of ", y_var_label, "~", x_var_label), #not .data[[]]
         x = x_var_label,
         y = y_var_label,
         fill = x_var_label)
}

#trick here is that elements of length 1 can be recycled if you wrap it in list
#https://stackoverflow.com/questions/46902461/how-to-pass-a-dataframe-and-uneven-vectors-as-parameters-in-purrr-map

plot_list <- pmap(list(df = list(diamonds), x_var = vars, y_var = list("depth"), x_var_label = my_labels[vars], 
          y_var_label = list(my_labels[!names(my_labels) %in% vars])), plot_f)

grid.arrange(grobs = plot_list, ncol = 1)

暫無
暫無

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

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