R包export
可以輕松的將R繪制的圖和統(tǒng)計(jì)表輸出到Microsoft Office(Word菠隆,PowerPoint和Excel),HTML和Latex中狂秘,其質(zhì)量可以直接用于發(fā)表骇径。
特點(diǎn)
可以用命令將交互式R圖或
ggplot2
,Lattice
或base R
保存到Microsoft Word碧查,Powerpoint或其他各種位圖或矢量格式运敢。完全可編輯的簡(jiǎn)報(bào)矢量格式輸出,支持手動(dòng)整理繪圖布局忠售。
將統(tǒng)計(jì)分析的輸出保存為的Excel传惠,Word,PowerPoint中稻扬,乳膠或HTML文檔的表格形式卦方。
自定義?輸出格式。
安裝
export
包可以在Windows中泰佳,Ubuntu的和蘋(píng)果機(jī)上跨平臺(tái)運(yùn)行不過(guò)有些Mac的發(fā)行版默認(rèn)情況下沒(méi)有安裝開(kāi)羅設(shè)備盼砍,需要自行安裝如果Mac的用戶(hù)已安裝XQuartz,這個(gè)問(wèn)題就解決了逝她,它可以從HTTPS:// www.xquartz.org/免費(fèi)獲得浇坐。
官方CRAN發(fā)布
install.packages("export")
該包主要包括以下幾種轉(zhuǎn)換
graph2bitmap
graph2office
graph2vector
rgl2bitmap轉(zhuǎn)換3D圖
table2office
table2spreadsheet
table2tex
graph2bitmap
:將當(dāng)前R圖保存到bmp文件中graph2png
:將當(dāng)前R圖保存到png文件中graph2tif
:將當(dāng)前R圖保存到TIF文件中graph2jpg
:將當(dāng)前R圖保存為JPEG文件
使用幫助信息如下:
graph2bitmap(x = NULL, file = "Rplot", fun = NULL, type = c("PNG","JPG", "TIF"),
aspectr = NULL, width = NULL, height = NULL, dpi = 300,scaling = 100,
font =ifelse(Sys.info()["sysname"] == "Windows", "Arial",
"Helvetica")[[1]], bg = "white", cairo = TRUE,
tiffcompression = c("lzw", "rle", "jpeg", "zip", "lzw+p", "zip+p"),
jpegquality = 99, ...)
aspectr
:期望縱橫比。如果設(shè)置為空黔宛,則使用圖形設(shè)備的縱橫比近刘。width
:所需寬度(英寸);可以與期望的縱橫比aspectr組合。height
:所需高度(英寸);可以與期望的縱橫比aspectr組合臀晃。scaling
:按一定比例縮放寬度和高度觉渴。font
:PNG和TIFF輸出中標(biāo)簽所需的字體; 的Windows系統(tǒng)默認(rèn)為宋體,其他系統(tǒng)默認(rèn)為黑體徽惋。bg
:所需的背景顏色案淋,例如“白色”或“透明”。cairo
:邏輯险绘,指定是否使用Cairographics導(dǎo)出踢京。tiffcompression
:用于TIF文件的壓縮回右。jpegquality
:JPEG壓縮的質(zhì)量。
準(zhǔn)備開(kāi)始
完安裝 export
包后漱挚,先調(diào)用該包
library(export)
用ggplot2
繪圖
library(ggplot2)library(datasets)
x=qplot(Sepal.Length, Petal.Length, data = iris,
color = Species, size = Petal.Width, alpha = I(0.7))
qplot()
的意思是快速作圖,利用它可以很方便的創(chuàng)建各種復(fù)雜的圖形渺氧,其他系統(tǒng)需要好幾行代碼才能解決的問(wèn)題旨涝,用qplot
只需要一行就能完成。
使用半透明的顏色可以有效減少圖形元素重疊的現(xiàn)象侣背,要?jiǎng)?chuàng)建半透明的顏色白华,可以使用alpha
圖形屬性,其值從0
(完全透明)到1
(完全不透明)贩耐。更多ggplot2
繪圖見(jiàn)ggplot2高效實(shí)用指南(可視化腳本弧腥,工具,套路潮太,配色) (往期教程更有很多生物信息相關(guān)的例子)管搪。
鳶尾花(iris
)是數(shù)據(jù)挖掘常用到的一個(gè)數(shù)據(jù)集,包含150個(gè)鳶尾花的信息铡买,每50個(gè)取自三個(gè)鳶尾花種之一(setosa
更鲁,versicolour
或virginica
)。每個(gè)花的特征用下面的5種屬性描述萼片長(zhǎng)度(Sepal.Length
)奇钞,萼片寬度(Sepal.Width
)澡为,花瓣長(zhǎng)度(Petal.Length
),花瓣寬度(Petal.Width
)景埃,類(lèi)(Species
)媒至。
在console里展示數(shù)據(jù)圖(長(zhǎng)寬比自己調(diào)節(jié)):
導(dǎo)出圖形對(duì)象
# 需運(yùn)行上面的ggplot2繪圖
# Create a file name
# 程序會(huì)自動(dòng)加后綴
filen <- "output_filename" # or
# filen <- paste("YOUR_DIR/ggplot")
# There are 3 ways to use graph2bitmap():
### 1. Pass the plot as an object
graph2png(x=x, file=filen, dpi=400, height = 5, aspectr=4)
graph2tif(x=x, file=filen, dpi=400, height = 5, aspectr=4)
graph2jpg(x=x, file=filen, dpi=400, height = 5, aspectr=4)
導(dǎo)出當(dāng)前繪圖窗口展示的圖
### 2. Get the plot from current screen device
# 注意這個(gè)x,是運(yùn)行命令谷徙,展示圖像
x
graph2png(file=filen, dpi=400, height = 5, aspectr=4)
graph2tif(file=filen, dpi=400, height = 5, aspectr=4)
graph2jpg(file=filen, dpi=400, height = 5, aspectr=4)
導(dǎo)出自定義函數(shù)輸出的一組圖
### 3. Pass the plot as a functio
plot.fun <- function(){
print(qplot(Sepal.Length, Petal.Length, data = iris,
color = Species, size = Petal.Width, alpha = 0.7))
}
graph2png(file=filen, fun=plot.fun, dpi=400, height = 5, aspectr=4)
graph2tif(file=filen, fun=plot.fun, dpi=400, height = 5, aspectr=4)
graph2jpg(file=filen, fun=plot.fun, dpi=400, height = 5, aspectr=4)
轉(zhuǎn)換后的圖形:
與辦公室系列的交互
大部分圖的細(xì)節(jié)修改都是用代碼完成的拒啰,不需要后續(xù)的修飾;但如果某一些修改比較特異,不具有程序的通用性特征蒂胞,或?qū)崿F(xiàn)起來(lái)比較困難图呢,就可以考慮后期修改比如用AI 文章用圖的修改和排版。熟悉PPT的骗随,也可以用PPT蛤织,這時(shí)?的圖導(dǎo)出PPT,用到就要graph2office
系列函數(shù)了鸿染。
graph2ppt
:將當(dāng)前R圖保存到Microsoft Office PowerPoint / LibreOffice Impress演示文稿中指蚜。
graph2doc
:將當(dāng)前的R圖保存到Microsoft Office Word / LibreOffice Writer文檔中。
函數(shù)參數(shù)展示和解釋
graph2office(x = NULL, file = "Rplot", fun = NULL, type = c("PPT", "DOC"),
append = FALSE, aspectr = NULL, width = NULL, height = NULL,scaling = 100,
paper = "auto", orient = ifelse(type[1] == "PPT","landscape", "auto"),
margins = c(top = 0.5, right = 0.5, bottom = 0.5, left= 0.5),
center = TRUE, offx = 1, offy = 1, upscale = FALSE, vector.graphic = TRUE, ...)
margins
:預(yù)設(shè)留白邊距向量涨椒。paper
:紙張尺寸 - “A5”至“A1”用于Powerpoint導(dǎo)出摊鸡,或“A5”至“A3”用于Word輸出;默認(rèn)“auto”自動(dòng)選擇適合您的圖形的紙張大小绽媒。如果圖太大,無(wú)法在給定的紙張大小上顯示免猾,則按比例縮小是辕。orient
:所需的紙張方向 - “自動(dòng)”,“縱向”或“橫向”; 字輸出默認(rèn)為“自動(dòng)”猎提,簡(jiǎn)報(bào)默認(rèn)為“橫向”获三。vector.graphic
:指定是否以可編輯的向量DrawingML格式輸出。默認(rèn)值為T(mén)RUE锨苏,在這種情況下疙教,編輯Powerpoint或Word中的圖形時(shí),可以先對(duì)對(duì)圖形元素進(jìn)行分組伞租。如果設(shè)置為FALSE贞谓,則將該圖以300 dpi的分辨率柵格化為PNG位圖格式。(柵(shān)格化葵诈,是PS中的一個(gè)專(zhuān)業(yè)術(shù)語(yǔ)裸弦,柵格即像素,柵格化即即矢量圖形轉(zhuǎn)化為位圖作喘。)
同樣有3種導(dǎo)出方式
# 需運(yùn)行上面的ggplot2繪圖
# Create a file name
filen <- "output_filename" # or
# filen <- paste("YOUR_DIR/ggplot")
# There are 3 ways to use graph2office():
### 1. Pass the plot as an object
# 導(dǎo)出圖形對(duì)象
graph2ppt(x=x, file=filen)
graph2doc(x=x, file=filen, aspectr=0.5)
### 2. Get the plot from current screen device
# 導(dǎo)出當(dāng)前預(yù)覽窗口呈現(xiàn)的圖
x
graph2ppt(file=filen, width=9, aspectr=2, append = TRUE)
graph2doc(file=filen, aspectr=1.7, append =TRUE)
### 3. Pass the plot as a function
# 導(dǎo)出自定義函數(shù)輸出的一系列圖
graph2ppt(fun=plot.fun, file=filen, aspectr=0.5, append = TRUE)
graph2doc(fun=plot.fun, file=filen, aspectr=0.5, append = TRUE)
導(dǎo)出到辦公室(PPT和字)中的圖形烁兰,是可編輯的:
其它導(dǎo)出到PPT的例子(設(shè)置長(zhǎng)寬比)
graph2ppt(file="ggplot2_plot.pptx", aspectr=1.7)
增加第二張同樣的圖,9英寸寬和A4長(zhǎng)寬比的幻燈片(append = T徊都,追加)
graph2ppt(file="ggplot2_plot.pptx", width=9, aspectr=sqrt(2), append=TRUE)
添加相同圖形的第三張幻燈片沪斟,寬度和高度固定
graph2ppt(file="ggplot2_plot.pptx", width=6, height=5, append=TRUE)
禁用矢量化圖像導(dǎo)出
graph2ppt(x=x, file=filen, vector.graphic=FALSE, width=9, aspectr=sqrt(2), append = TRUE)
用圖填滿(mǎn)幻燈片
graph2ppt(x=x, file=filen, margins=0, upscale=TRUE, append=TRUE)
輸出矢量圖
graph2svg
:將當(dāng)前的R圖保存為SVG格式graph2pdf
:將當(dāng)前的R圖保存為PDF格式graph2eps
:將當(dāng)前的R圖保存為EPS格式
函數(shù)參數(shù)解釋
graph2vector(x = NULL, file = "Rplot", fun = NULL, type = "SVG",aspectr = NULL,
width = NULL, height = NULL, scaling = 100,
font = ifelse(Sys.info()["sysname"] == "Windows",
"Arial","Helvetica")[[1]], bg = "white", colormodel = "rgb",
cairo = TRUE,fallback_resolution = 600, ...)
-
fallback_resolution
:dpi中的分辨率用于柵格化不支持的矢量圖形。
#需運(yùn)行上面的ggplot2繪圖
# Create a file name
filen <- "output_filename" # or
# filen <- paste("YOUR_DIR/ggplot")
# There are 3 ways to use graph2vector():
### 1. Pass the plot as an object
# 導(dǎo)出圖形對(duì)象
graph2svg(x=x, file=filen, aspectr=2, font = "Times New Roman",
height = 5, bg = "white")
graph2pdf(x=x, file=filen, aspectr=2, font = "Arial",
height = 5, bg = "transparent")
graph2eps(x=x, file=filen, aspectr=2, font = "Arial",
height = 5, bg = "transparent")
# 導(dǎo)出當(dāng)前預(yù)覽窗口呈現(xiàn)的圖
### 2. Get the plot from current screen device
x
graph2svg(file=filen, aspectr=2, font = "Arial",
height = 5, bg = "transparent")
graph2pdf(file=filen, aspectr=2, font = "Times New Roman",
height = 5, bg = "white")
graph2eps(file=filen, aspectr=2, font = "Times New Roman",
height = 5, bg = "white")
# 導(dǎo)出自定義函數(shù)輸出的一系列圖
### 3. Pass the plot as a function
graph2svg(file=filen, fun = plot.fun, aspectr=2, font = "Arial",
height = 5, bg = "transparent")
graph2pdf(file=filen, fun=plot.fun, aspectr=2, font = "Arial",
height = 5, bg = "transparent")
graph2eps(file=filen, fun=plot.fun, aspectr=2, font = "Arial",
height = 5, bg = "transparent")
轉(zhuǎn)換3D圖形
rgl2png
:將當(dāng)前的rgl 3D圖形保存為PNG格式暇矫。
rgl2bitmap(file = "Rplot", type = c("PNG"))
# Create a file name
filen <- tempfile("rgl") # or
# filen <- paste("YOUR_DIR/rgl")
# Generate a 3D plot using 'rgl'
x = y = seq(-10, 10, length = 20)
z = outer(x, y, function(x, y) x^2 + y^2)
rgl::persp3d(x, y, z, col = 'lightblue')
# Save the plot as a png
rgl2png(file = filen)
# Note that omitting 'file' will save in current directory
生成的3D圖形:
將生成的3D圖形保存為PNG格式:
輸出統(tǒng)計(jì)結(jié)果到表格 table2spreadsheet
table2excel
:導(dǎo)出統(tǒng)計(jì)輸出到Microsoft Office Excel / LibreOffice Calc電子表格中的一個(gè)表主之。table2csv
:將統(tǒng)計(jì)輸出以CSV格式導(dǎo)出到表中(“”“”,表示值分隔李根,表示小數(shù))table2csv2
:將統(tǒng)計(jì)輸出以CSV格式導(dǎo)出到表中(“;”表示值分隔槽奕,“,”表示小數(shù))
table2spreadsheet(x = NULL, file = "Rtable", type = c("XLS", "CSV",
"CSV2"), append = FALSE, sheetName = "new sheet", digits = 2,
digitspvals = 2, trim.pval = TRUE, add.rownames = FALSE, ...)
sheetName
:一個(gè)字符串房轿,給出創(chuàng)建的新工作表的名稱(chēng)(僅針對(duì)類(lèi)型==“XLS”)粤攒。它必須是惟一的(不區(qū)分大小寫(xiě)),不受文件中任何現(xiàn)有工作表名稱(chēng)的影響囱持。digits
:除具有p值的列外夯接,要顯示所有列的有效位數(shù)的數(shù)目。digitspvals
:具有p值的列要顯示的有效位數(shù)的數(shù)目纷妆。
# Create a file name
filen <- "table_aov" # or
# filen <- paste("YOUR_DIR/table_aov")
# Generate ANOVA output
fit=aov(yield ~ block + N * P + K, data = npk) # 'npk' dataset from base 'datasets'
x=summary(fit)
# Save ANOVA table as a CSV
### Option 1: pass output as object
# 輸出對(duì)象
table2csv(x=x,file=filen, digits = 1, digitspvals = 3, add.rownames=TRUE)
# 屏幕輸出導(dǎo)出到文件
### Option 2: get output from console
summary(fit)
table2csv(file=filen, digits = 2, digitspvals = 4, add.rownames=TRUE)
# Save ANOVA table as an Excel
# Without formatting of the worksheet
x
table2excel(file=filen, sheetName="aov_noformatting", digits = 1, digitspvals = 3, add.rownames=TRUE)
# 更多參數(shù)
# With formatting of the worksheet
table2excel(x=x,file=filen, sheetName="aov_formated", append = TRUE, add.rownames=TRUE, fontName="Arial", fontSize = 14, fontColour = rgb(0.15,0.3,0.75), border=c("top", "bottom"), fgFill = rgb(0.9,0.9,0.9), halign = "center", valign = "center", textDecoration="italic")
原始數(shù)據(jù)的表格:
轉(zhuǎn)換格式之后的盔几,在控制臺(tái)中的數(shù)據(jù):
文件(CSV和EXCEL)中表格數(shù)據(jù):
導(dǎo)出為字中的表,再也不用復(fù)制粘貼調(diào)格式了 table2office
table2ppt
:導(dǎo)出統(tǒng)計(jì)輸出到Microsoft Office PowerPoint / LibreOffice Impress演示文稿中的表
table2doc
:將統(tǒng)計(jì)輸出導(dǎo)出到Microsoft Office Word / LibreOffice Writer文檔中的表
table2office(x = NULL, file = "Rtable", type = c("PPT", "DOC"),
append = FALSE, digits = 2, digitspvals = 2, trim.pval = TRUE,
width = NULL, height = NULL, offx = 1, offy = 1,
font = ifelse(Sys.info()["sysname"] == "Windows", "Arial",
"Helvetica")[[1]], pointsize = 12, add.rownames = FALSE)
# Create a file name
filen <- "table_aov"
# filen <- paste("YOUR_DIR/table_aov")
# Generate ANOVA output
fit=aov(yield ~ block + N * P + K, data = npk) # 'npk' dataset from base 'datasets'
# Save ANOVA table as a PPT
### Option 1: pass output as object
x=summary(fit)
table2ppt(x=x,file=filen, digits = 1, digitspvals = 3, add.rownames =TRUE)
### Option 2: get output from console
summary(fit)
table2ppt(x=x,file=filen, width=5, font="Times New Roman", pointsize=14, digits=4, digitspvals=1, append=TRUE, add.rownames =TRUE) # append table to previous slide
# Save ANOVA table as a DOC file
table2doc(x=x,file=filen, digits = 1, digitspvals = 3, add.rownames =TRUE)
summary(fit)
table2doc(file=filen, width=3.5, font="Times New Roman", pointsize=14, digits=4, digitspvals=1, append=TRUE, add.rownames =TRUE) # append table at end of document
將表格數(shù)據(jù)導(dǎo)出到PPT和詞中:
table2tex
table2html
:導(dǎo)出統(tǒng)計(jì)輸出到HTML表掩幢。
table2tex(x = NULL, file = "Rtable", type = "TEX", digits = 2,
digitspvals = 2, trim.pval = TRUE, summary = FALSE, standAlone = TRUE,
add.rownames = FALSE, ...)
summary
:是否匯總數(shù)據(jù)文件逊拍。
standAlone
:導(dǎo)出的乳膠代碼應(yīng)該是獨(dú)立可編譯的上鞠,還是應(yīng)該粘貼到另一個(gè)文檔中。
add.rownames
:是否應(yīng)該將行名添加到表中(在第一列之前插入一列)芯丧。
# Create a file name
filen <- tempfile(pattern = "table_aov") # or
# filen <- paste("YOUR_DIR/table_aov")
# Generate ANOVA output
fit=aov(yield ~ block + N * P + K, data = npk) # 'npk' dataset from base 'datasets'
x=summary(fit)
# Export to Latex in standAlone format
table2tex(x=x,file=filen,add.rownames = TRUE)
# Export to Latex to paste in tex document
summary(fit) # get output from the console
table2tex(file=filen, standAlone = FALSE,add.rownames = TRUE)
# Export to HTML
table2html(x=x,file=filen) # or
summary(fit) # get output from the console
table2html(file=filen,add.rownames = TRUE)
導(dǎo)出到HTML或TEX中的表格數(shù)據(jù):