gglogger
is an R package that logs the calls used to
create ggplot2
objects.
This can be useful for debugging, reproducibility, and understanding the sequence of operations used to build a plot.
Have you ever created a plot in
ggplot2
and then forgotten how you made it? Or wanted to reproduce a plot but couldn’t remember the exact sequence of operations you used?gglogger
can help!
You can install the gglogger
package from source using
devtools
:
::install_github("pwwang/gglogger")
devtools
# or
::install_github("pwwang/gglogger") remotes
To use the gglogger
package, simply load it along with
ggplot2
and create your plots as usual. The package will
automatically log the calls used to create the plots.
Previous | Now |
---|---|
|
|
Evaluate the code in logs to reproduce the plot:
$logs$evaluate() p
You can also attach the variables in an environment for evaluation:
<- new.env()
env $mpg <- mpg
env$mpg$hwy <- mpg$hwy / 2
env$logs$evaluate(env) p
library(dplyr)
library(gglogger)
<- mtcars %>%
mtcars_radar as_tibble(rownames = "group") %>%
mutate_at(vars(-group), scales::rescale) %>%
tail(4) %>%
select(1:10)
<- register(ggradar::ggradar)
ggradar
<- ggradar(mtcars_radar, legend.position = "right")
p print(p$logs)
# ggradar::ggradar(mtcars_radar, legend.position = "right")
# p is a ggradar plot created in the previous example
<- p$logs$gen_code(setup = '
code library(dplyr)
library(ggradar)
mtcars_radar <- mtcars %>%
as_tibble(rownames = "group") %>%
mutate_at(vars(-group), scales::rescale) %>%
tail(4) %>%
select(1:10)
')
cat(code)
## library(dplyr)
## library(ggradar)
##
## mtcars_radar <- mtcars %>%
## as_tibble(rownames = "group") %>%
## mutate_at(vars(-group), scales::rescale) %>%
## tail(4) %>%
## select(1:10)
##
##
## ggradar::ggradar(mtcars_radar, legend.position = "right")
# eval(parse(text = code)) # to reproduce the plot
gglogger
cannot log the global settings used to create a
plot, such as theme_set()
. It can only log the calls used
directly to create the plot itself. You may need to manually set these
global settings when reproducing a plot, or prepare them using the
setup
argument in gen_code()
.
If your data is piped to ggplot
function, you need to
use |>
instead of %>%
, otherwise the
data will be logged as .
.