Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing plots generated in a function using R Markdown in RStudio

I am trying to use the R-Markdown feature in R Studio where I was trying to print plots which are generated inside a function. This is a basic run down example of what I am trying to do.

**Test printing plots generated in a function**
================================================

``` {r  fig.width=8, fig.height=4, warning=FALSE, eval=TRUE, message=FALSE, tidy=TRUE, dev='png', echo=FALSE, fig.show='hold', fig.align='center'}
dat <- data.frame(x=c(1:10),y=c(11:20),z=c(21:30),name=rep(c("a","b"),each=5))
library(ggplot2)

ex <- function(data){

  plot(data[,1],data[,2])
  plot(data[,1],data[,3])
}

for (i in 1:10){
t1 <- rbind(i,ex(dat))
}
t1
```

Those testing this code, please make sure to save it as ".Rmd" file and then run the knithtml() in RStudio toolbar. This code above works absolutely fine with the kind of html output I desire. However when I replace the base plotting function by ggplot based code, I cannot get the knithtml() to produce the ggplot output of the 10 plots that I got like before. The base plot code above is now replaced by the following code

  p1 <- ggplot(data=data, aes(x=data[,1],y=data[,2]))
  p1 <- p1+geom_point()
  p1 

Am I missing something very simple here.

VJ

like image 917
Vijay Ivaturi Avatar asked Jul 11 '12 15:07

Vijay Ivaturi


People also ask

How do I display plots in R markdown?

In RStudio, when you open a new RMarkdown file, in the editor pane, there is a cogwheel button / menu where you can choose "Chunk Output Inline". That should put the plots into the document.

What output formats can be generate from R markdown?

There are two types of output formats in the rmarkdown package: documents, and presentations.

How do I view plots in RStudio?

Tools->Global options->R Mark down In that phase select "window" from that list in the "show output preview in:" then apply. Tools > Global Options > Pane Layout, "Plots" is checked. Update the RStudio.


1 Answers

There are two problems in your code:

  1. ggplot doesn't recognize the data x and y data, bacause it works inside the data environment. You should give it the column names directly.
  2. The code in yur loop doesn't make sense. You can't mix a plot with an index... (the reason it works with the base plot is through a side-effect) I've replaced it with the simple plot command.

The following will work:

**Test printing plots generated in a function**
================================================

``` {r  fig.width=8, fig.height=4, warning=FALSE, eval=TRUE, message=FALSE, tidy=TRUE, dev='png', echo=FALSE, fig.show='hold', fig.align='center'}
dat <- data.frame(x=c(1:10),y=c(11:20),z=c(21:30),name=rep(c("a","b"),each=5))
library(ggplot2)

ex <- function(data){
  p1 <- ggplot(data=data, aes(x=x,y=y))
  p1 <- p1+geom_point()
  return(p1) 
}

for (i in 1:2){
plot(ex(dat))
}

```
like image 133
nassimhddd Avatar answered Sep 19 '22 19:09

nassimhddd