为什么不使用knitr和grid显示第二个ggplot?

为什么不使用knitr和grid显示第二个ggplot?,r,ggplot2,knitr,r-grid,R,Ggplot2,Knitr,R Grid,我正在尝试使用knitr创建一个文档,其中包括使用grid修改的ggplot2绘图 在下面的示例中,使用ggplot2中包含的diamonds数据集,应该有两个绘图:第一个显示切割与颜色,第二个显示切割与清晰度。相反,后面的情节重复了两次。第一个图根本不在图目录中生成 \documentclass{article} \begin{document} <<fig.cap = c('color', 'clarity')>>= library(ggplot2) librar

我正在尝试使用
knitr
创建一个文档,其中包括使用
grid
修改的
ggplot2
绘图

在下面的示例中,使用
ggplot2
中包含的
diamonds
数据集,应该有两个绘图:第一个显示切割与颜色,第二个显示切割与清晰度。相反,后面的情节重复了两次。第一个图根本不在
目录中生成

\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}
两个图形中,将正确生成两个图形,但注释将被切断

是什么导致了这个问题,更重要的是,我如何修复它


谢谢

从注释中添加代码

我在第二个绘图之前添加了一个
grid.newpage()
,以便两者都可以渲染。还必须调整边距以显示注释

Sp您的代码是

\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}
\documentclass{article}
\开始{document}
>=
图书馆(GG2)
图书馆(网格)
#使用颜色创建绘图
p=ggplot(菱形,aes(切割,填充=颜色))+geom_条(位置='fill')+注释('text',标签=as.character(表格(菱形$cut)),x=1:5,y=Inf,vjust=-1)+
增加了主题(plot.margin=unit(c(2,1,1,1),“线”)##
gt=ggplot\U gtable(ggplot\U构建(p))
gt$layout$clip[gt$layout$name=='panel']=='off'
网格绘制(gt)
#创建清晰的绘图
q=ggplot(菱形,aes(切割,填充=清晰度))+geom_条(位置='fill')+注释('text',标签=as.character(表格(菱形$cut)),x=1:5,y=Inf,vjust=-1+
增加了主题(plot.margin=unit(c(2,1,1,1),“线”)##
gs=ggplot_gtable(ggplot_build(q))
gs$layout$clip[gs$layout$name=='panel']=='off'
grid.newpage()##这是额外的一行
网格绘制(gs)
@
\结束{document}

您可能需要一个
grid.newpage()
在第一次绘图后添加
grid.newpage()
。你想把它放在那里吗?在
grid.draw(gt)
之后?如果只使用ggplots(即省略所有网格内容),是否会得到两个绘图?是的。两个不同的情节就像我期望看到的。嗯,好吧。。。啊,我用的是斯瓦夫而不是刀子
\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}