Movatterモバイル変換


[0]ホーム

URL:


ggloggergglogger website

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 inggplot2 and thenforgotten how you made it? Or wanted to reproduce a plot but couldn’tremember the exact sequence of operations you used?gglogger can help!

Installation

You can install thegglogger package from source usingdevtools:

devtools::install_github("pwwang/gglogger")# orremotes::install_github("pwwang/gglogger")

Usage

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.

PreviousNow
library(ggplot2)p<-ggplot(mpg,aes(x = displ,y = hwy))+geom_point()
library(ggplot2)# Just add gglogger after ggplot2library(gglogger)p<-ggplot(mpg,aes(x = displ,y = hwy))+geom_point()# Print the logsprint(p$logs)## ggplot2::ggplot(mpg, aes(x = displ, y = hwy)) +##   geom_point()

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)

Registering afunction from a ggplot2 extension

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")

Generating code toreproduce a plot

# 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 plot

Limitations

gglogger 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..


[8]ページ先頭

©2009-2025 Movatter.jp