
gglogger is an R package that logs the calls used tocreateggplot2 objects.
This can be useful for debugging, reproducibility, and understandingthe sequence of operations used to build a plot.
Have you ever created a plot in
ggplot2and thenforgotten how you made it? Or wanted to reproduce a plot but couldn’tremember the exact sequence of operations you used?ggloggercan help!
You can install thegglogger package from source usingdevtools:
devtools::install_github("pwwang/gglogger")# orremotes::install_github("pwwang/gglogger")To use thegglogger package, simply load it along withggplot2 and create your plots as usual. The package willautomatically log the calls used to create the plots.
| Previous | Now |
|---|---|
| |
Evaluate the code in logs to reproduce the plot:
p$logs$evaluate()You can also attach the variables in an environment forevaluation:
env<-new.env()env$mpg<- mpgenv$mpg$hwy<- mpg$hwy/2p$logs$evaluate(env)library(dplyr)library(gglogger)mtcars_radar<- mtcars%>%as_tibble(rownames ="group")%>%mutate_at(vars(-group), scales::rescale)%>%tail(4)%>%select(1:10)ggradar<-register(ggradar::ggradar)p<-ggradar(mtcars_radar,legend.position ="right")print(p$logs)# ggradar::ggradar(mtcars_radar, legend.position = "right")# p is a ggradar plot created in the previous examplecode<- p$logs$gen_code(setup ='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 plotgglogger cannot log the global settings used to create aplot, such astheme_set(). It can only log the calls useddirectly to create the plot itself. You may need to manually set theseglobal settings when reproducing a plot, or prepare them using thesetup argument ingen_code().
If your data is piped toggplot function, you need touse|> instead of%>%, otherwise thedata will be logged as..