Created
May 29, 2020 21:24
-
-
Save vasantm/608e55ab30f062a7828f25cf40864bac to your computer and use it in GitHub Desktop.
How to embed script name or a hash of it on a plot to allow to track plots and the scripts thar produced it in R, based on this question : https://twitter.com/tangming2005/status/1266404192744243202?s=20
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# code source for plotting | |
# https://www.datanovia.com/en/blog/ggplot-title-subtitle-and-caption/ | |
# get source filename SO quest: http://stackoverflow.com/q/1815606/946850 | |
# get source fielname answer: https://stackoverflow.com/a/20282846 | |
# if you do use a hash, you will have to save these to a csv or write | |
# a DB, perhaps an sqlite file that has the entry | |
# for the source file and corresponding hash to keep track of the | |
# hashes or just use filename and proj_dir, | |
# and display that if you want to keep it simple, | |
# open and reproducible | |
# use font colour white to not distract from plot | |
# Then you can select the caption - Note this only works for PDFs | |
library(ggplot2) | |
library(rprojroot) | |
library(git2r) | |
Proj_dir_name <-NULL | |
if(git2r::in_repository()){Proj_dir_name <-git2r::repository()$path} | |
if(is.null(Proj_dir_name)){ | |
Proj_dir_name <-dirname(thisfile()) | |
} | |
hash_label <-digest::digest(thisfile(), algo = "murmur32") #char fixed length of 8 | |
p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + | |
geom_boxplot() | |
p <- p + labs(title = "Effect of Vitamin C on Tooth Growth", | |
subtitle = "Plot of length by dose", | |
caption = paste0("Proj_dir: ",Proj_dir_name,"\n", | |
"File: ",thisfile(),"\n", | |
"FileHash: ", hash_label)) | |
p + theme(plot.caption = element_text(size =5,colour = "white")) | |
ggsave("plot-withcaption.pdf") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment