export is an R package to easily export active R graphs and statistical output in publication quality to Microsoft Office, HTML, and Latex. More information on GitHub.
library(export)?graph2ppt?graph2doc?graph2svg?graph2png?table2ppt?table2tex?table2excel?table2doc?table2html## export of ggplot2 plotlibrary(ggplot2)qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha =I(0.7))# export to Powerpoint graph2ppt()graph2ppt(file="ggplot2_plot.pptx", aspectr=1.7)# add 2nd slide with same graph 9 inches wide and A4 aspect ratiograph2ppt(file="ggplot2_plot.pptx", width=9, aspectr=sqrt(2), append=TRUE) # add 3rd slide with same graph with fixed width & heightgraph2ppt(file="ggplot2_plot.pptx", width=6, height=5, append=TRUE)# export to Wordgraph2doc()# export to bitmap or vector formatsgraph2svg()graph2png()graph2tif()graph2jpg()## export of aov Anova outputfit=aov(yield ~ block + N * P + K, npk)x=summary(fit)# export to Powerpointtable2ppt(x=x)table2ppt(x=x,file="table_aov.pptx")table2ppt(x=x,file="table_aov.pptx",digits=4,append=TRUE)table2ppt(x=x,file="table_aov.pptx",digits=4,digitspvals=1,font="Times New Roman",pointsize=16,append=TRUE)# export to Wordtable2doc(x=x)# export to Exceltable2excel(x=x, file ="table_aov.xlsx",digits=4,digitspvals=1,sheetName ="Anova_table", add.rownames =TRUE)# export to Latextable2tex(x=x)# export to HTMLtable2html(x=x)
An example Rscript
by liuyujie0136
## Export plots to PPT# library package "export"library(export)# get system time for suitable file namet=as.character(Sys.time())t=gsub(" ","-",t)t=gsub(":","-",t)fname=paste0("Rplot-",t,".pptx")# export plotsgraph2ppt(file=fname)
ggplot(df, aes(x = Group, y = Count))+geom_boxplot(outlier.colour =NA)+coord_cartesian(ylim =c(0, 100))
From the coord_cartesian documentation:
Setting limits on the coordinate system will zoom the plot (like you're looking at it with a magnifying glass), and will not change the underlying data like setting limits on a scale (e.g. scale_y_continuous) will.
You can also use scale_y_continuous alongside coord_cartesian to modify breaks, minor_breaks and expand etc. Just don't supply it with the ylim argument!
library(ggplot2)library(cowplot)# plot something first ......# arrange the three plots in a single rowprow <-plot_grid( p1 +theme(legend.position="none"), p2 +theme(legend.position="none"), p3 +theme(legend.position="none"), align ="vh", labels =c("A", "B", "C"), nrow =1)# extract the legend from one of the plotslegend_a <-get_legend(# create some space to the left of the legend p1 +theme(legend.box.margin =margin(0, 0, 0, 12)))# add the legend to the row we made earlier. Give it one-third of the width of one plot (via rel_widths).plot_grid(prow, legend_a, rel_widths =c(3, .4))# extract a legend that is laid out horizontallylegend_b <-get_legend( p1 +guides(color =guide_legend(nrow =1))+theme(legend.position ="bottom"))# add the legend underneath the row we made earlier. Give it 10% of the height of one plot (via rel_heights).plot_grid(prow, legend_b, ncol =1, rel_heights =c(1, .1))