Control alignment of two side-by-side plots in knitr
To center two plots you can add fig.align='center'
to your chunk options. If it produces one plot above the other add also fig.show='hold'
. The result should be two centered graphs.
So your final chunk option should look something like:
{r,echo=FALSE, out.width='.49\\linewidth', fig.width=3, fig.height=3,fig.show='hold',fig.align='center'}
Controlling distance between two knitr side-by-side plots
You can use layout
to add an adjustable space between the two plots. Create a three-plot layout and make the middle plot blank. Adjust the widths
argument to apportion the relative amount of space among the three plots.
In the example below, I also had to adjust the plot margin settings (par(mar=c(4,2,3,0))
) to avoid a "figure margins too large" error and I changed fig.width
to 4 to get a better aspect ratio for the plots. You may need to play with the figure margins and the figure parameters in the chunk to get the plot dimensions you want.
```{r,echo=FALSE,out.width='.49\\linewidth', fig.width=4, fig.height=3, fig.align='center'}
par(mar=c(4,2,3,0))
layout(matrix(c(1,2,3),nrow=1), widths=c(0.45,0.1,0.45))
barplot(1:4)
plot.new()
barplot(4:7)
```
If you happen to want to use grid graphics, you can use an analogous approach:
```{r,echo=FALSE,out.width='.49\\linewidth', fig.width=3, fig.height=3, fig.align='center'}
library(ggplot2)
library(gridExtra)
library(grid)
p1=ggplot(mtcars, aes(wt, mpg)) + geom_point()
grid.arrange(p1, nullGrob(), p1, widths=c(0.45,0.1,0.45))
```
Can't replicate MWE for side-by-side plots in knitr / R Markdown (HTML output)
I figured out the problem: I had fig.align='center'
enabled. Apparently this conflicts somehow with the ability to place two plots on the same line.
Two images side by side in rmarkdown using knitr?
Include the argument fig.show='hold'
.
```{r, echo=FALSE, out.width="33%", fig.align = "center", fig.show='hold', fig.cap="Examples of the FPTP bias: In Prairies (left) and Greater Toronto area (right). Source: www.fairvote.ca"}
knitr::include_graphics(
c("https://www.fairvote.ca/wp-content/uploads/2019/12/Western-alienation-website-FPTP-page.png",
"https://www.fairvote.ca/wp-content/uploads/2019/12/Toronto-Peel-halton-2019-results-website-FPTP.jpg")
)
```
Here is what it looks like now:
Align multiple tables side by side
Just put two data frames in a list, e.g.
t1 <- head(mtcars)[1:3]
t2 <- head(mtcars)[4:6]
knitr::kable(list(t1, t2))
Note this requires knitr >= 1.13.
Produce two plots from same chunk / statement in knitr
I thought there was not a solution, and was about to say no to @baptiste, but got a hack in my mind soon. Below is an R Markdown example:
```{r test, dev='png', fig.ext=c('png', 'large.png'), fig.height=c(4, 10), fig.width=c(4, 10)}
library(ggplot2)
qplot(speed, dist, data=cars)
```
See the [original plot](figure/test.png) and
a [larger version](figure/test.large.png).
The reason I thought the vectorized version of dev
would not work was: for dev=c('png', 'png')
, the second png file will overwrite the first one because the figure filename is the same. Then I realized fig.ext
was also vectorized, and a file extension like large.png
does not really destroy the file extension png
; this is why it is a hack.
Anyway, by vectorized versions of dev
, fig.ext
, fig.height
, and fig.width
, you can save the same plot to multiple versions. If you use a deterministic pattern for the figure file extensions, I think you can also cook up some JavaScript code to automatically attach fancy boxes onto images.
How to align table and plot in rmarkdown html_document
A great solution to this issue is provided here by @ErrantBard: https://stackoverflow.com/a/40650190/645206. Please visit and upvote it!
I am copying the solution in my answer to show how it works with your example, and to provide an image of the solution.
To better understand how these div
tags work, learn more about the bootstrap library. Here is one good link: https://getbootstrap.com/docs/4.1/layout/grid/
---
title: "Foo"
output: html_document
---
```{r setup, include=FALSE}
library(ggplot2)
library(knitr)
library(kableExtra)
```
# Table next to plot
<div class = "row">
<div class = "col-md-6">
```{r echo=FALSE}
kable(head(iris)) %>%
kable_styling(bootstrap_options = "striped", full_width = FALSE, position="left")
```
</div>
<div class = "col-md-6">
```{r echo=FALSE}
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()
```
</div>
</div>
Related Topics
R Partial Reshape Data from Long to Wide
Using If Else Conditions on Vectors
Scale_Color_Manual Colors Won't Change
How to Manually Set Colors in a Bar Chart
Writing R Function with If Enviornment
Correct Positioning of Multiple Significance Labels on Dodged Groups in Ggplot
Parse String with Additional Characters in Format to Date
R Aggregate Data in One Column Based on 2 Other Columns
Weird As.Posixct Behavior Depending on Daylight Savings Time
Merge Two Dataframes If Timestamp of X Is Within Time Interval of Y
How to Produce Time Series for Each Row of a Data Frame with an Unnamed First Column
Separate Columns with Constant Numbers and Condense Them to One Row in R Data.Frame
R: How to Get the Last Element from Each Group
R: Expand and Fill Data Frame by Date in Series
Lookup Values Corresponding to the Closest Date