How can I set up a code block which uses R code to output a cairo
graphics file?  In below example, code block for simple R graph uses
header argument to input filename.  Is it possible to create a similar
set up when cairo is used?

When exported to pdf, my graphs look similar with and without cairo.
Are other inputs needed to use all capabilities of cairo?

Thanks,
Naresh

** Simple Plot
#+header: :file figures/simpleplot.png
#+begin_src R :exports results :results graphics file
  set.seed(123)
  mydf <- data.frame(x1 = rnorm(5, mean = 0, sd = 1),
                     y1 = rnorm(5, mean = 0, sd = 1),
                     x2 = rnorm(5, mean = 4, sd = 2),
                     y2 = rnorm(5, mean = 4, sd = 2))

  with(mydf,
       plot(x1, y1, xlim = c(-2, 8), ylim = c(-2, 8), pch = 20,
            cex = 2, col = rgb(0.9, 0, 0, alpha = 0.7), xlab = "", ylab = ""))
  with(mydf,
       points(x2, y2, pch = 20, cex = 2, col = rgb(0, 0.9, 0, alpha = 0.7)))
  with(mydf,
       arrows(x1, y1, x2, y2, length = 0.2, angle = 20, code = 2,
              col = rgb(0.7, 0.7, 0.7)))
#+end_src

** Using Cairo
How to provide filename as header?  How to use :type cairo?
#+begin_src R :exports results :results output
  library(Cairo)

  set.seed(123)
  mydf <- data.frame(x1 = rnorm(5, mean = 0, sd = 1),
                     y1 = rnorm(5, mean = 0, sd = 1),
                     x2 = rnorm(5, mean = 4, sd = 2),
                     y2 = rnorm(5, mean = 4, sd = 2))

  CairoPNG("figures/cairoplot.png")
  with(mydf,
       plot(x1, y1, xlim = c(-2, 8), ylim = c(-2, 8), pch = 20,
            cex = 2, col = rgb(0.9, 0, 0, alpha = 0.7), xlab = "", ylab = ""))
  with(mydf,
       points(x2, y2, pch = 20, cex = 2, col = rgb(0, 0.9, 0, alpha = 0.7)))
  with(mydf,
       arrows(x1, y1, x2, y2, length = 0.2, angle = 20, code = 2,
              col = rgb(0.7, 0.7, 0.7)))
  dev.off()
#+end_src

#+RESULTS:

[[file:figures/cairoplot.png]]




Reply via email to