R markdown creates interactive reports from R code. This post provides a few tips I use on a daily basis to improve the appearance of output documents. In any case, an unavoidable resource is the Rstudio documentation.
R markdown allows to easily format your text. You can add links, write in bold or italic. This is very well explained in the Rstudio cheatsheet.
Here is the code I used to make this paragraph:
R markdown allows to easily format your text. You can add [links](www.r-graph-gallery.com), write in **bold** or *italic*. This is very well explained in the [Rstudio cheatsheet](https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf).Add an horizontal line by adding 3 stars:
***Header of level 1, 2, 3 are set using #, ## and ###. You can auto number your chapters using this option in the header:
---
title: "Your title"
output:
html_document:
number_sections: TRUE
---
# Title
## A subtitle
## Another subtitle
# Another titleI really like to add spaces in my document to give it a more uncluttered look. This is done using the <br> command. This .rmd code:
A first sentence
<br><br><br><br>
A seconde sentencewill give this htmloutput:
A first sentence
A seconde sentence
To center an image, use this code:
<center>

</center>If you’re struggling with to much white space around your image, try to play with the fig.asp argument. Here I used fig.asp=0.50.
library(png)
library(grid)
img <- readPNG("kan.png")
grid.raster(img)Lovely Kangaroo from the Mornington Peninsula.
Note: thanks to Anna Quaglieri for this suggestion.
I find it pleasant to have a bit of space before starting a new chapter. You can use a <br> before each header. A more convenient way is to add some margin in your CSS. Create a style.css file:
h1, .h1, h2, .h2, h3, .h3 {
margin-top: 84px;
}A rmd document that takes into account this .css file:
---
title: "A document with a CSS included"
output:
html_document:
css: style.css
---
A title will follow, but with a lot of space before it
# Title 1
content of part 1
# Title 2
content of part 2The document you are reading uses this css. See the separation between chapters.
Specify the caption of your figure in the chunk header. Example:
{r, fig.align="center", fig.width=6, fig.height=6, fig.cap="Figure: Here is a really important caption."}library(tidyverse)
mpg %>%
ggplot( aes(x=reorder(class, hwy), y=hwy, fill=class)) +
geom_boxplot() +
xlab("class") +
theme(legend.position="none")Figure: Here is a really important caption.
Change the black default caption using CSS. Adding this code in your style.css file.
<style>
p.caption {
font-size: 0.9em;
font-style: italic;
color: grey;
margin-right: 10%;
margin-left: 10%;
text-align: justify;
}
</style>