2016-12-02 11 views
11

我用下面的代碼在我*.Rmd文件產生以下的輸出:控制與knitr :: include_graphics功能的數字放置

```{r gb, echo=F, eval=T, results='asis', cache.rebuild=T, fig.cap='bla', out.width='0.7\\linewidth', fig.subcap=c('bla.', 'Using the \\textit{normalizeChIPToInput} function. THis method doesn not require to compute a enrichment ratio.')} 
p1 <- file.path(FIGDIR, 'correlK27K9me3.png') 
p2 <- file.path(FIGDIR, 'correlK27K9me3.png') 
knitr::include_graphics(c(p1,p2)) 
``` 

enter image description here

我想垂直堆疊這兩個地塊沒有並排顯示,而沒有單獨打電話給include_graphics(它不適用於subcaptions),也無需將它們放入單獨的大塊。這可能沒有操縱乳膠代碼?

更一般地說,是否有可能以某種方式指定包含在上述方式中的圖的佈局,如:'給我一個2×2的網格給我給include_graphics函數的4個圖像?

+0

如果沒有按@Yihui沒有答案,我懷疑這很容易/可能。一個複雜的情況是,這個建議的佈局不容易移植到'knitr'支持的所有格式中。既然你在爭奪PDF輸出,也許你可以在你的Rmd文件中使用文字'LaTeX'來做你想做的事情。 – r2evans

+0

我無法重現您的示例。你能提供你的PNG文件和YAML頭文件嗎? –

+0

您是否想過直接在文檔中放置HTML代碼來處理此問題?當然,假設你正在編譯爲HTML。 – user5359531

回答

1

相反的:

knitr::include_graphics(c(p1,p2)) 

這個怎麼樣:

cowplot::plot_grid(p1, p2, labels = "AUTO", ncol = 1, align = 'v') 

這會的{r}內工作,但我不知道它是如何工作給予你的塊配置/設置。

1

這不是問題的最純溶液,但使用gridarrange和R.

text功能一點的解決方法

工藝流程:read_images - >轉換爲格 - >讀格柵圖像 - > add_text - > final_save

```{r fig.align='center', echo=FALSE, fig.cap="Figure 1 : foo", warning=FALSE, message=FALSE} 
library(png) 
library(grid) 
library(gridExtra) 

#Loading images 
img0 <- readPNG("heatMap.png") 
img1 <- readPNG("heatMap.png") 
img2 <- readPNG("heatMap.png") 
img3 <- readPNG("heatMap.png") 

#Convert images to Grob (graphical objects) 
grob0 <- rasterGrob(img0) 
grob1 <- rasterGrob(img1) 
grob2 <- rasterGrob(img2) 
grob3 <- rasterGrob(img3) 

png(filename = "gridPlot.png", width = 1200, height = 716) 

grid.arrange(grob0, grob1, grob2, grob3, nrow = 2) 

invisible(dev.off()) 

gridplot.0 <- readPNG("gridPlot.png") 
h<-dim(gridplot.0)[1] 
w<-dim(gridplot.0)[2] 

png(filename = "gridPlotFinal.png", width = 1200, height = 716) 

#adding text to image (refer to https://stackoverflow.com/a/23816416/6779509) 

par(mar=c(0,0,0,0), xpd=NA, mgp=c(0,0,0), oma=c(0,0,0,0), ann=F) 
plot.new() 
plot.window(0:1, 0:1) 

#fill plot with image 
usr<-par("usr")  
rasterImage(gridplot.0, usr[1], usr[3], usr[2], usr[4]) 

#add text 
text("plot1", x=0.25, y=0.50) 
text("plot2", x=0.75, y=0.50) 
text("plot3", x=0.23, y=0.0) 
text("plot4", x=0.77, y=0.0) 

invisible(dev.off()) 

gridplot <- file.path("gridPlotFinal.png") 
knitr::include_graphics(gridplot) 
``` 

輸出: plot