为什么2nd ggplot不会使用knitr和grid? [英] Why does 2nd ggplot not appear using knitr and grid?

查看:82
本文介绍了为什么2nd ggplot不会使用knitr和grid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 knitr 创建一个包含 ggplot2 使用修改过的图的文档。 grid



在下面的示例中,为2个图,均使用包含在 ggplot2 中的钻石数据集:第一个显示切割与颜色,第二个显示切割与清晰度。相反,后面的情节重复两次。第一个图不是在目录中生成的。

  \documentclass {article} 

\ begin {document}

<<< fig.cap = c('color','clarity')>> =
library(ggplot2)
library(grid)
theme_update(plot.margin = unit(c(1.5,2,1,1),'lines'))

#使用颜色

p = ggplot(aes(cut,fill = color))+ geom_bar(position ='fill')+ annotate('text',label = as)创建绘图。 character(table(diamonds $ cut)),x = 1:5,y = Inf,vjust = -1)
gt = ggplot_gtable(ggplot_build(p))
gt $ layout $ clip [gt $


$ bq = ggplot(diamonds,aes) (表格(diamonds $ cut)),x = 1:5,y = Inf,vjust = -1)
gs = ggplot_gtable(ggplot_build(q))
gs $ layout $ clip [gs $ layout $ name =='panel' ] ='off'
grid.draw(gs)
@

\ end {document}

重要的是,如果我删除以下行:

  gt = ggplot_gtable( ggplot_build(p))
gt $ layout $ clip [gt $ layout $ name =='panel'] ='off'
grid.draw(gt)

from both 数字,那么它们都将被正确生成,但注释将被截断。



是什么导致了这个问题,更重要的是,我该如何解决它?



谢谢!

解决方案

从评论中添加代码

我添加了一个 grid.newpage )在第二个绘图之前,以允许两个呈现。



Sp代码是

  \documentclass {article} 

\ begin {document}

库(ggplot2)
库(网格)

#使用颜色$ b创建图
$ bp = ggplot(diamonds,aes(cut,fill = color))+ geom_bar(position ='fill')+ annotate('text',label = as.character(table(diamonds $ cut)), x = 1:5,y = Inf,vjust = -1)+
theme(plot.margin = unit(c(2,1,1,1),lines))### added
$ b gt = ggplot_gtable(ggplot_build(p))
gt $ layout $ clip [gt $ layout $ name =='panel'] ='off'
grid.draw(gt)
$ b $#创建清晰图
$ bq = ggplot(diamonds,aes(cut,fill = clarity))+ geom_bar(position ='fill')+ annotate('text' ,label = as.character(table(diamonds $ cut)),x = 1:5,y = Inf,vjust = -1)+
theme(plot.margin = unit(c(2,1,1 ,1),lines))### added

gs = ggplot_gtable(ggplot_build(q))
gs $ layout $ clip [gs $ layout $ name =='panel' ] ='off'
grid.newpage()##这是多余的行
grid.draw(gs)
@

\end { ment}


I am trying to create a document using knitr that includes ggplot2 plots modified using grid.

In the example below, there should be 2 plots both using the diamonds dataset included in ggplot2: the first one shows cut vs. color and the second shows cut vs. clarity. Instead, the later plot is repeated twice. The first plot is not generated in the figure directory at all.

\documentclass{article}

\begin{document}

<<fig.cap = c('color', 'clarity')>>=
library(ggplot2)
library(grid)
theme_update(plot.margin = unit(c(1.5, 2, 1, 1), 'lines'))

# Create plot with color

p = ggplot(diamonds,aes(cut,fill = color)) + geom_bar(position = 'fill') + annotate('text', label = as.character(table(diamonds$cut)), x = 1:5, y = Inf, vjust = -1)
gt = ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == 'panel'] = 'off'
grid.draw(gt)

# Create plot with clarity

q = ggplot(diamonds,aes(cut,fill = clarity)) + geom_bar(position = 'fill') + annotate('text', label = as.character(table(diamonds$cut)), x = 1:5, y = Inf, vjust = -1)
gs = ggplot_gtable(ggplot_build(q))
gs$layout$clip[gs$layout$name == 'panel'] = 'off'
grid.draw(gs)
@

\end{document}

Importantly, if I remove the following lines:

gt = ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == 'panel'] = 'off'
grid.draw(gt)

from both figures, then they will both be generated correctly but the annotations will be cut off.

What is causing this issue, and more importantly, how do I fix it?

Thanks!

解决方案

adding code from comments

I added a grid.newpage() before the second plot to allow both to render. Also had to tweak the margins to get the annotations to show.

Sp your code is

\documentclass{article}

\begin{document}

<< fig.cap = c('color', 'clarity')>>=
library(ggplot2)
library(grid)

# Create plot with color

p = ggplot(diamonds,aes(cut,fill = color)) + geom_bar(position = 'fill') + annotate('text', label = as.character(table(diamonds$cut)), x = 1:5, y = Inf, vjust = -1) + 
theme(plot.margin=unit( c(2,1,1,1), "lines") ) ### added

gt = ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == 'panel'] = 'off'
grid.draw(gt)

# Create plot with clarity

q = ggplot(diamonds,aes(cut,fill = clarity)) + geom_bar(position = 'fill') + annotate('text', label = as.character(table(diamonds$cut)), x = 1:5, y = Inf, vjust = -1) + 
theme(plot.margin=unit( c(2,1,1,1), "lines") ) ### added

gs = ggplot_gtable(ggplot_build(q))
gs$layout$clip[gs$layout$name == 'panel'] = 'off'
grid.newpage() ## This is the extra line
grid.draw(gs)
@

\end{document}

这篇关于为什么2nd ggplot不会使用knitr和grid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆