0

I want to make plots of all variables with ggplot2 using map function from purrr. Everything is okay except x-axis label. Any hint.

library(tidyverse)

Data <- esoph[ , 1:3]
fm1Plots <- 
  Data %>%
  map(
    ~ ggplot(mapping = aes(x = .)) +
      geom_bar(aes_string(fill = .))  +
      labs(x = .)
  )

fm1Plots[[1]]

enter image description here

fm1Plots[[2]]

enter image description here

MYaseen208
  • 22,666
  • 37
  • 165
  • 309

1 Answers1

4

Iterate over column names in map. Note that aes_string is deprecated.

library(ggplot2)
Data <- esoph[ , 1:3]

fm1Plots <- purrr::map(names(Data), ~ggplot(Data) + 
                                     aes(!!sym(.x), fill = !!sym(.x)) + 
                                     geom_bar()  + labs(x = .x))

fm1Plots[[1]]

enter image description here

You can also use .data pronoun

fm1Plots <- purrr::map(names(Data), ~ggplot(Data) + 
                    aes(.data[[.x]], fill = .data[[.x]]) + 
                    geom_bar()  + labs(x = .x))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213