Externalize ggplot2 code

You may have your own layout code like this one, and you do not want to type the code again and again. One way to avoid repeating the code explicitly is to use chunk references. Although knitr is based on the idea of literate programming, but we rarely need programming, and this is one case in which literate programming may be helpful.

We simplify the layout code in the original post and put it in a code chunk named my-layout:

theme (legend.text = element_text(size = 12, angle=45))+
  theme (legend.position = "bottom")

Now we can use <<>> to reference it.

library(ggplot2)
qplot(carat, price, data=diamonds, color=cut)+
  <<my-layout>>

Another test:

qplot(carat, data=diamonds, fill=cut)+
  <<my-layout>>


Try the parsermd package in your browser

Any scripts or data that you put into this service are public.

parsermd documentation built on May 20, 2021, 5:08 p.m.