Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knitr: edit ggplot object when processing the document

Tags:

r

ggplot2

knitr

I would like to create a theme for all ggplot objects in R markdown. Hence, is it possible to tweak the chart output, e.g.

```{r}
ggplot(cars, aes(speed, dist)) + geom_point()
```

to add a theme when processing the document, e.g. ggplot_obj + theme_bw()?

I have looked into knitr hooks (http://yihui.name/knitr/hooks), but I can't figure out how to use it in combination with ggplot objects.

Thanks!

like image 964
reinholdsson Avatar asked Dec 06 '25 23:12

reinholdsson


1 Answers

Use theme_set(theme_bw()) in a first invisible chunk.

like image 86
Yihui Xie Avatar answered Dec 09 '25 11:12

Yihui Xie