ggplot2 figure size with RMarkdown
Your question is not exactly clear. I'm not sure, for example, why you are using ggsave()
to begin with. You can directly create a "ggplot" image in your file to knit
and set your figure width and height in your input file.
In a ".Rmd" file, your code might look something like:
```{r fig.width=7, fig.height=4, echo=FALSE}
library(ggplot2)
qplot(mpg, wt, data=mtcars)
```
The echo=FALSE
will make it so that the code doesn't display, but the resulting plot will. The figure width and height have been set with the relevant arguments.
If you wanted to convert your resulting markdown file to PDF, I would recommend looking at Pandoc which will allow you to do something like the following to convert your file to a PDF:
pandoc infile.md -o outfile.pdf
Alternatively, you can use R Sweave instead of R Markdown in R/RStudio. For instance, if you create a new "Rnw" file in RStudio and paste the following in, you'll have the option to directly compile a PDF instead of compile an HTML.
\documentclass{article}
\begin{document}
<<fig.width=5, fig.height=3, echo=FALSE>>=
library(ggplot2)
qplot(mpg, wt, data=mtcars)
@
\end{document}
Admin
Updated on June 15, 2020Comments
-
Admin almost 4 years
My converting the R script (.R) into an
RMarkdown
file (.rmd) in RStudio and then pressing “knit html” result in two output files (that is, .html and .md files). I am confronting 2 issues:The
html
file shows that the title of theggplot
graph gets chopped. I have changed the original width of 11 to a new width of 15:ggsave(file=outFile, width=15, height=7)
How would I resolve the issue ? And how would I convert the .md file into a PDF file ?
-
Admin over 11 yearsFirst issue: I have resolved .. using the \n in the title. My R script involves (1) creating factors for ggplot and printing the results, and (2) writing functions that produce 5 geom bars and save them in 5 .png files.
-
A5C1D2H2I1M1N2O1R2T1 over 11 years@user1849365 I'm sorry, but without some sample script (please edit your question to show your data and and the relevant contents of your Rmd file), I'm not able to help you further. Were you ultimately able to generate a PDF?