Chapter 4 Formatting
4.1 Stacking Graphs
- Example:
g1 <- ggplot(cars, aes(x = speed)) + geom_histogram(bins = 10)
g2 <- ggplot(cars, aes(x=dist)) + geom_histogram(bins=10)
gridExtra::grid.arrange(g1,g2, g1,g2, ncol = 2)
Just change the ncol =
to match the number of columns you want in your output.
If you want two graphs side by side, use
ncol = 2
.If you want them one on top of the other, use
ncol = 1
.
4.2 Shrinking Graphs
Sometimes, plots take up a lot of space on a page. To change that, you can shrink graphs.
To do this, change your code chunk to: {r , fig.asp = .6}
You can change the .6 to any number between .1 and .9.
Make sure the graph can still be read after shrinking!!
4.3 Formatting a Table
A lot of times in R, you have loose output like MSE values or AIC values, or predictions, etc. Instead of having them just printed out with no idea of what they correspond to, it is better to put them in a table.
If you are using the table( )
command in R, this works very well, but can give output that is not formatted nicely. To fix that, use
knitr::kable( )
Inside the ( ) put your table, for instance
knitr::kable( table(cars$speed) )
4.4 Formatting the Output from a Regression Model
The default output we get from using the summary command in R is useful, but not very pretty. To format the output, you can use the following (just replace model1
with the name of your model).
- Example:
knitr::kable( summary(model1)$coefficients)