Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 figure size with RMarkdown

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 the ggplot 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 ?

like image 240
user1849365 Avatar asked Nov 24 '12 11:11

user1849365


1 Answers

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}
like image 126
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 18 '22 18:10

A5C1D2H2I1M1N2O1R2T1