日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

r语言读写word_R语言:在word中插入ggplot

發(fā)布時(shí)間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 r语言读写word_R语言:在word中插入ggplot 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近CRAN上新了一個(gè)叫eoffice的package,并且不時(shí)被各路大佬提起。這個(gè)包的功能剛好也符合我最近的需求,這次帶各位先來試試水。包的官方介紹:

1. Introduction?cran.r-project.org

這次主要試試在word中用該包插入ggplot。既然要試,圖就整的復(fù)雜一點(diǎn):

> rm(list = ls()) > gc()used (Mb) gc trigger (Mb) max used (Mb) Ncells 2423917 129.5 4543915 242.7 4543915 242.7 Vcells 6168695 47.1 12255594 93.6 10143094 77.4 > > pacman::p_load(data.table, magrittr, ggplot2, ggthemr, cowplot, stringr, dplyr, patchwork) > > library(eoffice) > library(gridExtra) > > ggthemr('fresh') > > test<-mtcars %>% mutate(text = rownames(mtcars)) %>% data.table() %$% + .[,c('brand', 'model','tag'):=tstrsplit(text, split = ' ')] > testsum<-test[,.(mpg = mean(mpg, na.rm = T), wt = mean(wt, na.rm = T)), by = 'brand'][1:5,] > > > p1<-ggplot(test)+ + geom_histogram(aes(x = mpg))+ + coord_flip()+ + scale_y_continuous(limits = c(0,8))+ + annotation_custom(grob = tableGrob(testsum, rows = NULL), + xmin = 25, xmax = 33, ymin = 5, ymax = 7) > p2<-ggplot(test)+ + geom_point(aes(x = disp, y = hp)) > p3<-ggplot(test)+ + geom_hex(aes(x = drat, y = wt)) > > p<-p1+(p2/p3) > p `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

出圖如下:

拼圖用的patchwork,如何在圖中插入表格請(qǐng)參考我之前寫的文章:傳送門。

然后按照官方介紹,直接用todocx把圖片插入word:

> todocx(p, filename = 'D:/R/eoffice/testword.docx', width = 6.5, height = 5, > append = F) `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. Warning message: dml_docx() is deprecated and will be removed in the next version (> 0.2.0), use devEMF::emf instead.

注意:append設(shè)置為F的話,filename文件原有的內(nèi)容會(huì)被完全覆蓋!第一次嘗試千萬注意備份文件。

提示一些警告,不過不要緊,看看filename的文件:

這么方便的工具,排版當(dāng)然不可能幫你一次排好的。要自己調(diào)長(zhǎng)寬,并且像圖中有g(shù)rob對(duì)象(就是那張表)的情況下,這些對(duì)象還要單獨(dú)調(diào)整。

這玩意兒牛逼的地方在于,圖片導(dǎo)出word的時(shí)候就是矢量圖,所有元素都可以選中并編輯,像上圖那樣。但凡投過paper的都知道期刊會(huì)要求稿件里插圖的矢量圖,這個(gè)包在這方面可以為我們省下不少功夫。

不過這也有缺點(diǎn),要是要素過多(例如大量數(shù)據(jù)的散點(diǎn)圖),一般電腦打開這個(gè)word肯定死機(jī)。就上面那個(gè)圖片,我的筆電打開word都已經(jīng)有點(diǎn)卡了...

此外,如果導(dǎo)出的時(shí)候filename的文件已在word打開,就會(huì)報(bào)錯(cuò):

> todocx(p, filename = 'D:/R/eoffice/testword.docx', width = 6.5, height = 5, + append = F) `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. Error in value[[3L]](cond) : Could not write "D:/R/eoffice/testword.docx" [zip error: `Cannot open zip file `D:/R/eoffice/testword.docx` for writing` in file `zip.c:373`] 此外: Warning messages: 1: In grob$wrapvp <- vp : 到達(dá)了流逝時(shí)間限制 2: dml_docx() is deprecated and will be removed in the next version (> 0.2.0), use devEMF::emf instead.

將append設(shè)置為T,就可以在已存在的文檔最后面插入圖片了:

> todocx(p, filename = 'D:/R/eoffice/testword_2.docx', width = 6.5, height = 5, + append = T) `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

這是一個(gè)挺大的word文檔,里頭還有各種追蹤修訂,也順利導(dǎo)入了

總的來說,eoffice無法調(diào)整導(dǎo)出圖片為柵格(例如jpeg),因此導(dǎo)出到word可能是eoffice最不適用的場(chǎng)景了。比較建議的用法還是每張圖片單獨(dú)存成一個(gè)ppt或pdf。

總結(jié)

以上是生活随笔為你收集整理的r语言读写word_R语言:在word中插入ggplot的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。