Home > Enterprise >  R Markdown - Description next to plot in PDF output
R Markdown - Description next to plot in PDF output

Time:11-09

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:

Ideal Output

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).

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("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)

Current Output

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. Any ideas for how this can be achieved?

CodePudding user response:

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: enter image description here

  • Related