I am trying to understand how to use walk
to silently (without printing to the console) return ggplot2
plots in a pipeline.
library(tidyverse)
# EX1: This works, but prints [[1]], [[2]], ..., [[10]] to the console
10 %>%
rerun(x = rnorm(5), y = rnorm(5)) %>%
map(~ data.frame(.x)) %>%
map(~ ggplot(., aes(x, y)) + geom_point())
# EX2: This does not plot nor print anything to the console
10 %>%
rerun(x = rnorm(5), y = rnorm(5)) %>%
map(~ data.frame(.x)) %>%
walk(~ ggplot(., aes(x, y)) + geom_point())
# EX3: This errors: Error in obj_desc(x) : object 'x' not found
10 %>%
rerun(x = rnorm(5), y = rnorm(5)) %>%
map(~ data.frame(.x)) %>%
pwalk(~ ggplot(.x, aes(.x$x, .x$y)) + geom_point())
# EX4: This works with base plotting
10 %>%
rerun(x = rnorm(5), y = rnorm(5)) %>%
map(~ data.frame(.x)) %>%
walk(~ plot(.x$x, .x$y))
I was expecting example #2 to work, but I must be missing or not understanding something. I want the plots from #1 without the console output.
The base R functions such as par() and layout() will not work with ggplot2 because it uses a different graphics system and this system does not recognize base R functionality for plotting. However, there are multiple ways you can combine plots from ggplot2 . One way is using the cowplot package.
%>% is a pipe operator reexported from the magrittr package. Start by reading the vignette. Adding things to a ggplot changes the object that gets created. The print method of ggplot draws an appropriate plot depending upon the contents of the variable.
The function qplot() [in ggplot2] is very similar to the basic plot() function from the R base package. It can be used to create and combine easily different types of plots. However, it remains less flexible than the function ggplot(). This chapter provides a brief introduction to qplot(), which stands for quick plot.
ggplot2 [library(ggplot2)] ) is a plotting library for R developed by Hadley Wickham, based on Leland Wilkinson's landmark book The Grammar of Graphics ["gg" stands for Grammar of Graphics]. Some documentation can be found on the ggplot website .
I'm not sure why it works with base R plot
in your 4th example honestly. But for ggplot
, you need to explicitly tell walk
that you want it to print. Or as the comments suggest, walk
will return plots (I misspoke in my first comment on that) but not print them. So you could use walk
to save the plots, then write a second statement to print them. Or do it in one walk
call.
Two things here: I'm using function notation inside walk
, instead of purrr
's abbreviated ~
notation, just to make it clearer what's going on. I also changed the 10 to 4, just so I'm not flooding everyone's screens with tons of plots.
library(tidyverse)
4 %>%
rerun(x = rnorm(5), y = rnorm(5)) %>%
map(~ data.frame(.x)) %>%
walk(function(df) {
p <- ggplot(df, aes(x = x, y = y)) + geom_point()
print(p)
})
Created on 2018-05-09 by the reprex package (v0.2.0).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With