r - How do pipes work with purrr map() function and the "." (dot) symbol -


when using both pipes , map() function purrr, confused how data , variables passed along. instance, code works expect:

library(tidyverse)  cars %>%    select_if(is.numeric) %>%    map(~hist(.)) 

yet, when try similar using ggplot, behaves in strange way.

cars %>%    select_if(is.numeric) %>%    map(~ggplot(cars, aes(.)) + geom_histogram()) 

i'm guessing because "." in case passing vector aes(), expecting column name. either way, wish pass each numeric column ggplot function using pipes , map(). in advance!

cars %>%    select_if(is.numeric) %>%    map2(., names(.),         ~{ggplot(data_frame(.x), aes(.x)) +             geom_histogram() +             labs(x = .y)                    }) 

enter image description here

enter image description here

there's few steps.

  • use map2 instead of map. first argument dataframe you're passing it, , second argument vector of names of dataframe, knows map over.
  • you need explicitly enframe data, since ggplot works on dataframes , coercible objects.
  • this gives access .y pronoun name plots.

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -