An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120802/3c337894/attachment.pl>
ggplot does not show in knitr
4 messages · firdaus.janoos, R. Michael Weylandt
You need to explicitly print() the graphs to make them show -- or in recent ggplot2 versions I think you can also use plot() as an alias. Michael
On Aug 2, 2012, at 2:37 PM, "firdaus.janoos" <fjanoos at bwh.harvard.edu> wrote:
Hello,
I'm having some issues getting a ggplot figure to show up in the knitr
output, when placed in a loop.
Specifically, I have a loop inside a knitr chunk :
```{r fitting, warning=FALSE, fig.width=10, fig.height=10, fig.keep='high'}
for (t in 1:T)
{
# do a regression of tgt.vals ~ predictors and compute coeffs and
fitted values (fit.vals / fit.adj.vals)
plot( x=tgt.vals, y=fit.vals );
plot( x=tgt.vals, y = fit.adj.vals );
qplot( x = pred.names, y = coeffs);
}
```
The html output file shows the output of first 2 plot commands but not that
of the qplot command. Even if i remove the 2 plots and keep only the qplot
in the loop, it does not display in the html output.
The qplot just by itself, not in a loop, displays fine. Also, there are no
issues when I run in regular R.
Any suggestions of what is going on ?
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120802/375e0136/attachment.pl>
See R FAQ 7.22 for the gory details -- It needs to go around the qplot() statement. In interactive use, most objects are "printed" after evaluation: sometimes this doesn't happen because the returned value is marked invisible. For grid graphics objects (of which ggplot objects are a type), this "printing" is the actual display mechanism. This makes lots of sense in a interactive context because after the object is assembled, it is auto-printed back to you, and hence displayed. However, inside a for loop (and a few other cases) the auto-printing is turned off. For example 2 + 2 # will display 4 for(i in 1:10) 2 + 2 # Will not print 4 ten times To get printing behavior in a for loop, you need to print explicitly: for(i in 1:10) print(2+2) # Will print 4 ten times. AFAICT, knitr turns on the regular auto-printing, but it doesn't get the printing inside the for loop (perhaps because there's no mechanism to make it always happen, but that's just idle speculation) Hope this helps, Michael
On Thu, Aug 2, 2012 at 3:52 PM, firdaus.janoos <fjanoos at bwh.harvard.edu> wrote:
Could you elaborate on this ? Where would the print /plot statement go ? On Thu, Aug 2, 2012 at 4:38 PM, Michael Weylandt <michael.weylandt at gmail.com> wrote:
You need to explicitly print() the graphs to make them show -- or in recent ggplot2 versions I think you can also use plot() as an alias. Michael On Aug 2, 2012, at 2:37 PM, "firdaus.janoos" <fjanoos at bwh.harvard.edu> wrote:
Hello,
I'm having some issues getting a ggplot figure to show up in the knitr
output, when placed in a loop.
Specifically, I have a loop inside a knitr chunk :
```{r fitting, warning=FALSE, fig.width=10, fig.height=10,
fig.keep='high'}
for (t in 1:T)
{
# do a regression of tgt.vals ~ predictors and compute coeffs and
fitted values (fit.vals / fit.adj.vals)
plot( x=tgt.vals, y=fit.vals );
plot( x=tgt.vals, y = fit.adj.vals );
qplot( x = pred.names, y = coeffs);
}
```
The html output file shows the output of first 2 plot commands but not
that
of the qplot command. Even if i remove the 2 plots and keep only the
qplot
in the loop, it does not display in the html output.
The qplot just by itself, not in a loop, displays fine. Also, there are
no
issues when I run in regular R.
Any suggestions of what is going on ?
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.