Alignment of images in tables with markdown, rstudio and knitr
<p>I'm trying to create a standard monthly report for work in PDF format using Rstudio and I want to incorporate ggplot output with a table of figures - a new chart, one per cell on each row.  I'm new to markdown, latex, pandoc and knitr so this is a bit of minefield for me.</p>

<p>I have found out how to insert the charts using kable but the images are not aligned with the text on the same row.</p>

<p>I've put some (rstudio markdown) code using dummy data at the bottom of my question, and here are some images showing what I'm trying to do and the problem I've got</p>

<p><a href="http://i.stack.imgur.com/9FDU6.png" rel="nofollow">Example of graphic I want to incorporate into table</a></p>

<p><a href="http://i.stack.imgur.com/TAmO5.png" rel="nofollow">This is what the table looks like with the misaligned text and images</a></p>

<p>You can see that the text and the images are not aligned. If I leave the images out the tables are nice and compact, putting the images in means the tables spread out over multiple pages, even though the images themselves aren't that tall.</p>

<p>Any advice welcome - code snippets doubly so.</p>

<p>Many thanks</p>

<hr>

<pre><code>  title: "Untitled"
  output: pdf_document
  ---

  This example highlights the issue I'm having with formatting a nice table with the graphics and the vertical alignment of text.



  ```{r echo=FALSE, results='hide', warning=FALSE, message=FALSE}
    ## Load modules
    library(dplyr)
    library(tidyr)
    library(ggplot2)

    ## Create a local function to plot the z score
    varianceChart &lt;- function(df, personNumber) {
      plot &lt;- df %&gt;%
        filter(n == personNumber) %&gt;%
        ggplot() +
        aes(x=zscore, y=0) +
        geom_rect(aes(xmin=-3.32, xmax=-1.96, ymin=-1, ymax=1), fill="orange2", alpha=0.8) + 
        geom_rect(aes(xmin=1.96, xmax=3.32, ymin=-1, ymax=1), fill="olivedrab3", alpha=0.8) +
        geom_rect(aes(xmin=min(-4, zscore), xmax=-3.32, ymin=-1, ymax=1), fill="orangered3") + 
        geom_rect(aes(xmin=3.32, xmax=max(4, zscore), ymin=-1, ymax=1), fill="chartreuse4") +
        theme(axis.title = element_blank(), 
              axis.ticks = element_blank(), 
              axis.text = element_blank(),
              panel.grid.minor = element_blank(),
              panel.grid.major = element_blank()) +
        geom_vline(xintercept=0, colour="black", alpha=0.3) +
        geom_point(size=15, shape=4, fill="lightblue") ##Cross looks better than diamond

      return(plot)
    }

    ## Create dummy data
    Person1 &lt;- rnorm(1, mean=10, sd=2) 
    Person2 &lt;- rnorm(1, mean=10, sd=2)
    Person3 &lt;- rnorm(1, mean=10, sd=2)
    Person4 &lt;- rnorm(1, mean=10, sd=2) 
    Person5 &lt;- rnorm(1, mean=10, sd=2) 
    Person6 &lt;- rnorm(1, mean=6,  sd=1) 

    ## Add to data frame
    df &lt;- data.frame(Person1, Person2, Person3, Person4, Person5, Person6)

    ## Bring all samples into one column and then calculate stats
    df2 &lt;- df %&gt;% 
      gather(key=Person, value=time)

    mean &lt;- mean(df2$time)
    sd   &lt;- sqrt(var(df2$time))

    stats &lt;- df2 %&gt;%
      mutate(n = row_number()) %&gt;%
      group_by(Person) %&gt;%
      mutate(zscore = (time - mean) / sd)

    graph_directory &lt;- getwd() #'./Graphs'

    ## Now to cycle through each Person and create a graph
    for(i in seq(1, nrow(stats))) {
      print(i)
      varianceChart(stats, i)

      ggsave(sprintf("%s/%s.png", graph_directory, i), plot=last_plot(), units="mm", width=50, height=10, dpi=1200)
    }

    ## add a markup reference to this dataframe
    stats$varianceChart &lt;- sprintf('![](%s/%s.png)', graph_directory, stats$n)  

    df.table &lt;- stats[, c(1,2,5)]
    colnames(df.table) &lt;- c("Person Name", "Time taken", "Variance Chart")
  ```


  ```{r}  
    library(knitr)
    kable(df.table[, c(1,2)], caption="Rows look neat and a sensible distance apart")
    kable(df.table, caption="Rows are separated a long way apart and images and text are misaligned")

  ```
</code></pre>