Created
July 3, 2018 12:29
-
-
Save stefanedwards/dc7e37e5574a7b539f70246bf1f294e8 to your computer and use it in GitHub Desktop.
Copy sparklines and inline figures from R to clipboard for pasting in Word
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
library(ggplot2) | |
library(grid) | |
library(gridExtra) | |
theme_mini <- theme_minimal(9) | |
win.metafile("", width=1, height=0.4, pointsize=9) ## copy to clipboard ! :D | |
print(p) | |
dev.off() | |
# Paste into e.g. Word! | |
## Another example for inline figures (e.g. sparklines) | |
(p <- ggplot(mtcars, aes(mpg)) + geom_histogram(fill='white', colour='black') + theme_mini) | |
ggp <- ggplot_build(p + theme_inline) | |
xs <- ggp$layout$panel_params[[1]]$x.labels | |
win.metafile("", width=1, height=0.4, pointsize=9) ## copy to clipboard ! :D | |
tg1 <- ggplot2:::element_render(theme_mini, 'axis.text', xs[1], x=1, hjust=1) | |
tg2 <- ggplot2:::element_render(theme_mini, 'axis.text', xs[length(xs)], x=0, hjust=0) | |
gt <- ggplot_gtable(ggp) | |
i <- which(gt$layout$name == 'panel') | |
grid.arrange(tg1, gt$grobs[[i]], tg2, | |
widths=unit.c(grobWidth(tg1)*1.1, unit(1, 'null'), grobWidth(tg2)*1.1), | |
nrow=1, newpage = FALSE) | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment