使用 addImage 在 ReporteRs 中并排绘图
Side-by-side plotting in ReporteRs using addImage
我正在使用 ReporteRs 包自动创建 docx 文档。为此,我需要并排放置两个外部图像。在 R 中使用它们之前手动组合它们不是一个选项吗
,因为那将是大量的组合。
library("ReporteRs")
doc <- docx()
doc <- addImage(doc,"image1.png",par.properties = parLeft(), height=1, width=1)
doc <- addImage(doc,"image2.png",par.properties = parLeft(), height=1, width=1)
writeDoc(doc, file = "example.docx")
我该怎么做才能使图并排,而不是 under/over 另一个?
这里有两种不同的方法:
# png(filename = "image1.png")
# barplot(1:10)
# dev.off()
#
# png(filename = "image2.png")
# barplot(1:10)
# dev.off()
library(ReporteRs)
library(magrittr)
方法一:带段
docx() %>%
addSection(ncol = 2, space_between = 0.1) %>%
addImage( "image1.png",par.properties = parLeft(), height=1, width=1) %>%
addColumnBreak() %>%
addImage( "image2.png",par.properties = parLeft(), height=1, width=1) %>%
addSection( ncol = 1 ) %>%
writeDoc( file = "example1.docx")
方法 2:使用 table
dat <- matrix("", nrow = 1, ncol = 2) # dummy empty table
# Flextable - one line, 2 columns
ft <- FlexTable(dat, header.columns = F, add.rownames = F)
ft[1,1] <- pot_img("image1.png", height=1, width=1) # add image1 to cell 1
ft[1,2] <- pot_img("image2.png", height=1, width=1) # add image2 to cell 2
docx() %>%
addFlexTable( ft ) %>%
writeDoc(file = "example2.docx")
我正在使用 ReporteRs 包自动创建 docx 文档。为此,我需要并排放置两个外部图像。在 R 中使用它们之前手动组合它们不是一个选项吗 ,因为那将是大量的组合。
library("ReporteRs")
doc <- docx()
doc <- addImage(doc,"image1.png",par.properties = parLeft(), height=1, width=1)
doc <- addImage(doc,"image2.png",par.properties = parLeft(), height=1, width=1)
writeDoc(doc, file = "example.docx")
我该怎么做才能使图并排,而不是 under/over 另一个?
这里有两种不同的方法:
# png(filename = "image1.png")
# barplot(1:10)
# dev.off()
#
# png(filename = "image2.png")
# barplot(1:10)
# dev.off()
library(ReporteRs)
library(magrittr)
方法一:带段
docx() %>%
addSection(ncol = 2, space_between = 0.1) %>%
addImage( "image1.png",par.properties = parLeft(), height=1, width=1) %>%
addColumnBreak() %>%
addImage( "image2.png",par.properties = parLeft(), height=1, width=1) %>%
addSection( ncol = 1 ) %>%
writeDoc( file = "example1.docx")
方法 2:使用 table
dat <- matrix("", nrow = 1, ncol = 2) # dummy empty table
# Flextable - one line, 2 columns
ft <- FlexTable(dat, header.columns = F, add.rownames = F)
ft[1,1] <- pot_img("image1.png", height=1, width=1) # add image1 to cell 1
ft[1,2] <- pot_img("image2.png", height=1, width=1) # add image2 to cell 2
docx() %>%
addFlexTable( ft ) %>%
writeDoc(file = "example2.docx")