【发布时间】:2017-12-28 15:45:41
【问题描述】:
我使用 knitR (.Rnw) 创建了一份报告,将其编译为 PDF。因为我想为每个问题绘制相同的图,所以我在 for 循环中创建了一个图。不幸的是,我在 PDF 中收到警告,并没有真正了解导致错误的原因...
这是我认为问题开始的部分:循环在 R 中运行良好,但不能编译为 PDF(请参阅下面的整个代码)。我尝试了各种标签、打印功能和其他东西,但找不到解决方案。
<<echo=FALSE, warning=T, message=F>>=
for(i in 1:3){
cat(paste("\\subsection{",titel[i],"}\n", sep=""))
cat(paste("Figure \\ref{class",i,"} \n", sep=""))
cat(paste("\\begin{figure}[H] \n", sep=""))
cat(paste("\\begin{center} \n", sep=""))
cat(paste("\\includegraphics[width=1\\textwidth,",
"height=.47\\textheight,keepaspectratio]{class",i,".pdf}\\caption{",titel[i],"}\n", sep=""))
cat(paste("\\label{class",i,"}" \n, sep=""))
cat(paste("\\end{center} \n",sep=""))
cat(paste("\\end{figure} \n",sep=""))
p <- ggplot(data[!is.na(data$F17),], aes_string(x=Fragen[i], y="..prop..", group = "1", fill="F17"))+
geom_bar()+
facet_grid(F17~.)+
geom_text(aes(label = scales::percent(..prop..),
y= ..prop.. ), stat= "count", vjust = -.5, size=3) +
ylab("Prozent")+
xlab(titel[i])+
scale_fill_manual(name="Individuals", values=colorScheme)#+
#theme_mine
pdfnam<-paste("class",i,".pdf",sep="") #produce a plot for each class
pdf(file=pdfnam,width=12, height = 4)
#gridExtra::grid.arrange(p, q)
print(p)
dev.off()
}
@
这里是复制的全部代码:
\documentclass{article}
\usepackage{amsmath,amssymb,amstext}
\usepackage{graphicx}
\usepackage{geometry}
\geometry{top=15mm, left=25mm, right=25mm, bottom=25mm,headsep=10mm,footskip=10mm}
\usepackage{xcolor}
\usepackage{float}
\usepackage[T1]{fontenc} % Umlaute
\usepackage[utf8]{inputenc}
\inputencoding{latin1}
\begin{document}
\parindent 0pt
\title{title}
\maketitle
<<echo=FALSE, warning=FALSE, message=FALSE>>=
library(ggplot2)
library(reshape)
library(knitr)
library(doBy)
library(dplyr)
opts_chunk$set(fig.path='figure/graphic-', fig.align='center', fig.show='hold',fig.pos='!ht',
echo=FALSE,warning = FALSE)
@
<<echo=FALSE, warning=FALSE, message=F>>=
# data and other useful stuff
data <- data.frame(F1 = c("A", "A", "B", "C"), # answers to question 1, ...
F2 = c("A", "B", "B", "C"),
F3 = c("A", "B", "C", "C"),
F17 = c("K", "L", "L", "M")) # K, L and M are a certain individual. L answered twice.
# colour scheme:
GH="#0085CA"; H="#DA291C"; BV="#44697D"
colorScheme <- c(BV, H, GH)
# individual theme for plots:
theme_mine = theme(plot.background = element_rect(fill = "white"),
panel.background = element_rect(fill = "white", colour = "grey50"),
text=element_text(size=10, family="Trebuchet MS"))
# a vector with the variable names from "data" (F1, F2, F3).
Fragen <- c(paste0('F',seq(1:3), sep=""))
# question title for labeling the plots:
titel <- c("Q1", "Q2", "Q3", "Q17")
@
\begin{figure}[h]
\begin{center}
<<echo=FALSE, fig.width=9.6, fig.height=6, warning=FALSE>>=
p <- ggplot(data, aes(x=F17))+
geom_bar(fill = colorScheme)+
xlab(titel[4])+
#geom_text(aes(label = scales::percent(..prop..),
# y= ..prop.. ), stat= "count", vjust = -.5, size=3) +
ylab("Absolut")+
theme_bw()
#theme_mine # does not work properly yet.
p
@
\caption{figa}
\label{figa}
\end{center}
\end{figure}
\section{individual plots}
<<echo=FALSE, warning=T, message=F>>=
# here is where the problem starts: the loop runs fine within R but does not compile to an PDF.
for(i in 1:3){
cat(paste("\\subsection{",titel[i],"}\n", sep=""))
cat(paste("Figure \\ref{class",i,"} \n", sep=""))
cat(paste("\\begin{figure}[H] \n", sep=""))
cat(paste("\\begin{center} \n", sep=""))
cat(paste("\\includegraphics[width=1\\textwidth,",
"height=.47\\textheight,keepaspectratio]{class",i,".pdf}\\caption{",titel[i],"}\n", sep=""))
cat(paste("\\label{class",i,"}" \n, sep=""))
cat(paste("\\end{center} \n",sep=""))
cat(paste("\\end{figure} \n",sep=""))
p <- ggplot(data[!is.na(data$F17),], aes_string(x=Fragen[i], y="..prop..", group = "1", fill="F17"))+
geom_bar()+
facet_grid(F17~.)+
geom_text(aes(label = scales::percent(..prop..),
y= ..prop.. ), stat= "count", vjust = -.5, size=3) +
ylab("Prozent")+
xlab(titel[i])+
scale_fill_manual(name="Individuals", values=colorScheme)#+
#theme_mine
pdfnam<-paste("class",i,".pdf",sep="") #produce a plot for each class
pdf(file=pdfnam,width=12, height = 4)
#gridExtra::grid.arrange(p, q)
print(p)
dev.off()
}
@
\end{document}
提前非常感谢!
【问题讨论】:
-
删除
pdf()行。它用于将绘图保存为独立的 PDF,而不是用于在报告中包含绘图。 -
\label{abc}将允许您使用\ref{abc}。但是,您正在创建\label{figure/class <i> }(注意<i>周围有一个空格)但使用\ref{ <i> }(同样,有空格)。 -
...另见When should we use
\begin{center}instead of\centering? 你应该使用\centering。 -
感谢您的建议。不幸的是,我仍然不确定如何解决如何在 PDF 中包含绘图的主要错误。
-
您根据 Werner 的建议进行了一些更改。但是您是否尝试删除
pdf(....)和dev.off()行?