将乳胶添加到knitr多个图形中

时间:2012-10-17 13:49:37

标签: r latex ggplot2 sweave knitr

您好我有一些代码生成多个图形,下面是knitr模拟的。生成的图形网格非常小,我想在knitr includegraphic代码中添加\adjustbox{scale=1.25}{\includegraphic{**}},但明显的尝试cat(\ includegrahics)却没有用。有没有一种简单的方法可以在生成的includegraphic之前和之后添加latex命令?

\documentclass{article}
\usepackage{adjustbox}
\begin{document}
\SweaveOpts{echo=FALSE, warning=FALSE,message=FALSE,error=FALSE}

<<results='hide'>>=
require(gdata)
require(xtable)
source('~/Dropbox/Programming/R Resources/defs.R')
require(grid)
require(ggplot2)
require(reshape2)
@

<<results='hide'>>=
flows=structure(list(Fund.Domicile = c("A", "A", "A", "A", "A", "B", 
"B", "B", "B", "B", "C", "C", "C", "C", "C", "D", "D", "D", "D", 
"D"), Fund.Investment.Type = c("W", "W", "W", "W", "W", "X", 
"X", "X", "X", "X", "Y", "Y", "Y", "Y", "Y", "Z", "Z", "Z", "Z", 
"Z"), `2002` = c(-0.529898540845281, 0.772054875993179, -20.0331761409875, 
18.1013558280681, 5.24105768030327, -0.103737945556193, -21.1814709681659, 
-17.5440978426109, -15.5829977118454, -20.2260315293908, 40.1011996671901, 
31.7827753364134, 10.3285781074177, -0.439416156872641, 10.0561291484258, 
28.6005484483324, -0.78786653287941, -13.3541087171504, -23.2187998341653, 
-31.0039436462572), `2003` = c(14.7492468042958, -0.0275963287375104, 
-17.7708887712113, -21.6767106833672, 40.1833625101162, 11.123046653849, 
-7.75903264645413, -32.8999860636484, 2.08949747258285, -20.5043265068236, 
-1.36220948683688, -43.666119989502, 49.8895881388731, -6.77098667217137, 
-1.14679926989166, 1.95491694361459, -19.2996405333482, -27.7844292459854, 
-31.2344881507997, -19.5546235912165), `2004` = c(-23.7147297386606, 
-16.3947580155831, -26.2668449875697, 7.44336777160022, 10.5589478002836, 
-8.75005070668023, -1.86641790420448, 16.1977001332142, -22.1050631845402, 
-39.3516451994645, -49.0769487782368, -3.35924808913687, -24.4704066120435, 
-37.2442867971211, 0.683587234461294, -18.2706752984679, 41.5870386956072, 
-13.0489657114237, 15.9378239971014, 7.67697910507204), `2005` = c(-4.55959797613669, 
5.88565456256252, -1.69072856589651, 10.5241870675827, -14.6862822775804, 
17.5177606053963, -20.8386082504429, -19.4972967013319, -10.511857263532, 
-41.2074075438929, 24.5654545251262, 8.55786318066587, 5.44738422527871, 
0.634623938079709, 8.12292371061514, 23.4632529622168, 24.4070063548887, 
-11.0915520114019, 12.135207142526, 20.4497798625889), `2006` = c(9.75226025251804, 
-4.29865061171953, -2.09724392048597, 44.6666722837138, 30.661418933677, 
31.4006593587462, 7.81193825433631, 6.67455195559533, -21.4061513950816, 
-10.9347694700455, 21.679512353103, 3.00620296764385, -28.8652577925736, 
-2.40500496458537, -34.2999700917406, 4.16424846748192, 12.8570962420172, 
14.2386144440232, 24.8251023510994, -8.43426647739114), `2007` = c(5.63097110819688, 
24.5618516506043, -12.1851875872125, 1.47169500868284, -2.06314306074233, 
49.3156076715943, 22.1496090994165, -5.66968824427377, 19.0180156274722, 
9.60066029597923, -17.3853484334746, -20.3244695486896, 19.2245352517759, 
-27.0714138354969, -10.2816030397085, -32.7428648340247, -6.91534782810891, 
-22.5738223368588, -4.90194082412061, 4.5930516250516), `2008` = c(17.6132975354028, 
13.2825751589442, 10.9526114709831, 54.5347419688899, 10.2168636887397, 
22.7675434698984, 21.2966040483597, -5.48726279410465, -5.74273553195292, 
-15.9585829721537, -13.7289712451739, 7.30295584751816, -34.1484269522794, 
1.99774293039047, 20.1873723311578, -5.64271685865591, -15.2492762599638, 
14.8885666471535, -0.667785495902541, 25.3487149747655), `2009` = c(12.6895026843362, 
8.79135905052476, -31.1013615485344, 53.2336276071583, 0.850127605357284, 
-25.4230346228574, 0.685229220802603, 2.17685117184943, 22.9033401966985, 
4.99033888685287, -6.73959582268601, 1.64262497691941, -7.97937067923302, 
2.08671946596528, -1.49959272604534, -56.053302546395, -26.9877635637831, 
10.70792214362, 20.767800895652, 5.56759212862765), `2010` = c(9.12519719158834, 
56.4458682158794, 10.0924010459392, -26.1685287160829, 0.383729796176608, 
8.46399630560169, 13.9961555521805, 2.08243592536891, 9.30857371730836, 
-5.68931159712071, -1.90329476168925, 4.63803550621013, -9.6622478328383, 
38.2174244916787, -34.31334333628, -31.8687230108344, 22.2571919461198, 
18.0507770996133, -29.9436511641762, -5.15424637861403), `2011` = c(0.293475666658379, 
-37.6498026750657, 12.2168994671778, 35.4799718156936, -19.3929280696092, 
9.32638067918487, -29.0199397818446, -37.9432273617349, 2.94421967337048, 
-24.7995737867905, 27.0860382001494, 34.7688415979825, -6.63377573201861, 
14.2519054923501, 13.4697104157147, -7.33188207017677, 5.13471816288466, 
-5.04112561270486, -13.8230485866696, 44.0005209380284), `2012` = c(-1.46548181648837, 
11.469059790123, 21.0839248832524, 3.00780109149884, -32.9185824569684, 
22.120980240766, -3.46899628596499, 5.52026948842546, -10.1622646453992, 
-8.09625643479651, 1.20491472685827, 35.7200647592221, -25.2214669973592, 
27.114933346277, 4.11392575746633, -16.1856657167321, 22.0260776979833, 
-38.0714290042188, 23.4976820652487, 47.7740524641717)), .Names = c("Fund.Domicile", 
"Fund.Investment.Type", "2002", "2003", "2004", "2005", "2006", 
"2007", "2008", "2009", "2010", "2011", "2012"), class = "data.frame", row.names = c(NA, 
-20L))
@

<<results='hide'>>=
countries=unique(flows[['Fund.Domicile']])
ff=melt(flows,id.vars=c(1:2))
ff[['sign']]<-ifelse(ff[['value']] >= 0, 'positive', 'negative')
@

<<results='asis',fig.height=11,fig.width=9>>=
for(c in countries){
  ctry <- subset(ff,subset=(Fund.Domicile == c))
  grid.newpage()
  pushViewport(viewport(layout=grid.layout(4,3)))
  n=matrix(1:12,ncol=3,byrow=T)
  strats=unique(ctry[['Fund.Investment.Type']])
  k=length(strats)
  for(i in 1:12){
    if(i<=k){
      tmp2=subset(ctry,subset=(Fund.Investment.Type == strats[i]))
      xco=which(n == i,arr.ind=T)[1]
      yco=which(n == i,arr.ind=T)[2]
      xx=ggplot(tmp2,aes(variable,value,fill=sign)) +
        geom_bar(stat='identity') +
        xlab("Year") +
        ylab(bquote("US$ Millions")) +
        ggtitle(paste(c,'\n',strats[i])) +
        guides(fill=F) +
        theme(axis.title.x=element_text(size=6,colour='dark blue'),
              axis.title.y=element_text(size=6,colour='dark blue'),
              axis.text.x=element_text(size=4,angle=90),
              axis.text.y=element_text(size=4),
              plot.title=element_text(size=7,face='bold',colour='dark blue')) +
        scale_fill_manual(values=c('positive' = 'dark blue','negative' = 'red'))
          cat('\\adjustbox{scale=1.2}{',
  print(xx,vp=viewport(layout.pos.row=xco,layout.pos.col=yco)),"}")
    }
  }
}
@


\end{document}

tex输出添加在

之下
\documentclass{article}\usepackage{graphicx, color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\definecolor{fgcolor}{rgb}{0.2, 0.2, 0.2}
\newcommand{\hlnumber}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlfunctioncall}[1]{\textcolor[rgb]{0.501960784313725,0,0.329411764705882}{\textbf{#1}}}%
\newcommand{\hlstring}[1]{\textcolor[rgb]{0.6,0.6,1}{#1}}%
\newcommand{\hlkeyword}[1]{\textcolor[rgb]{0,0,0}{\textbf{#1}}}%
\newcommand{\hlargument}[1]{\textcolor[rgb]{0.690196078431373,0.250980392156863,0.0196078431372549}{#1}}%
\newcommand{\hlcomment}[1]{\textcolor[rgb]{0.180392156862745,0.6,0.341176470588235}{#1}}%
\newcommand{\hlroxygencomment}[1]{\textcolor[rgb]{0.43921568627451,0.47843137254902,0.701960784313725}{#1}}%
\newcommand{\hlformalargs}[1]{\textcolor[rgb]{0.690196078431373,0.250980392156863,0.0196078431372549}{#1}}%
\newcommand{\hleqformalargs}[1]{\textcolor[rgb]{0.690196078431373,0.250980392156863,0.0196078431372549}{#1}}%
\newcommand{\hlassignement}[1]{\textcolor[rgb]{0,0,0}{\textbf{#1}}}%
\newcommand{\hlpackage}[1]{\textcolor[rgb]{0.588235294117647,0.709803921568627,0.145098039215686}{#1}}%
\newcommand{\hlslot}[1]{\textit{#1}}%
\newcommand{\hlsymbol}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlprompt}[1]{\textcolor[rgb]{0.2,0.2,0.2}{#1}}%

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}
\usepackage{adjustbox}
\begin{document}


\adjustbox{scale=1.2}{ 
\includegraphics[width=\maxwidth]{figure/unnamed-chunk-4} 


\end{document}

1 个答案:

答案 0 :(得分:4)

我在knitr talk at useR! 2012中强调了这一点:

Do not cat includegraphics

knitr中,您可以使用块选项out.extra,例如<<out.extra='scale=1.25'>>=,以避免邪恶的cat()。见http://yihui.name/knitr/options

我在测试您的案例时发现了knitr中的错误,并在development version中修复了该错误。如果您使用的是CRAN版本,则必须opt_chunk$set(out.width = NULL),否则您可以撰写<<out.extra='scale=1.25', out.width=NULL>>=