简体   繁体   English

R Markdown - PDF 输出中绘图旁边的描述

[英]R Markdown - Description next to plot in PDF output

I want to align several plots on the left hand side of the page with their description on the right hand side.我想将页面左侧的几个图与右侧的描述对齐。 My ideal output is something like this, with up to 5 plots on a page:我理想的输出是这样的,页面上最多有 5 个图:

理想输出

The plots are dynamic and based on user input in a shiny app.这些图是动态的,基于用户在闪亮应用程序中的输入。 Here's an example of how I generate the plots (currently using fsmb for the radarchart function but considering using ggplot2 instead).这是我如何生成绘图的示例(目前使用 fsmb 作为雷达图函数,但考虑使用 ggplot2)。

library(fmsb)
library(knitr)

opts_chunk$set(echo=FALSE)

df <- data.frame(
  row.names=c("Max", "Min", "Data"),
  rbind(
    rep(5, 6),
    rep(1, 6),
    sample(1:5, 6, replace=TRUE)
  )
)

radarchart(
  df,
  vlabels=c(1,2,3,4,5,6), 
  caxislabels=c(1,2,3,4,5)
)

The chart is fine, but where I'm struggling is with the alignment.图表很好,但我挣扎的地方是对齐方式。 I have tried to save the chart as a png and add it to a table but it's not working at all as I want it to.我试图将图表另存为 png 并将其添加到表格中,但它根本无法正常工作。

png("plot1.png", width=400, height=400)
radarchart(
  df,
  vlabels=c(1,2,3,4,5,6), 
  caxislabels=c(1,2,3,4,5)
)
dev.off()

dat <- data.frame(
  Figure=c("1"),
  Description=c("Short description"))
dat$Figure <- c(sprintf("![](%s){width=200px}", "plot1.png"))
kable(dat)

电流输出

I would like to be able to arrange the figures without having to save a png first but this is the closest I've managed to get to the desired output so far.我希望能够在不必先保存 png 的情况下排列数字,但这是迄今为止我设法获得所需输出的最接近的结果。 Any ideas for how this can be achieved?关于如何实现这一目标的任何想法?

May be I can help you.也许我能帮你。 I use data from your example and do this:我使用您示例中的数据并执行以下操作:

op <- par(mar=c(1, 2, 2, 1),mfrow=c(2, 2))
radarchart(
    df,
    vlabels=c(1,2,3,4,5,6), 
    caxislabels=c(1,2,3,4,5)
)
plot(NA, xlim = c(-5, 5), ylim=c(-5,5), axes = F, ann = F)
text(x = 0, y = 0, "good plot\nanother line\ngood plot", cex = 2)
radarchart(
    df,
    vlabels=c(1,2,3,4,5,6), 
    caxislabels=c(1,2,3,4,5)
)
plot(NA, xlim = c(-5, 5), ylim=c(-5,5), axes = F, ann = F)
text(x = 0, y = 0, "your plot description\nmay be on 2 lines", cex = 2)
par(op)

Result:结果: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM