knitR (.Rnw)을 사용하여 보고서를 작성하여 PDF로 컴파일했습니다. 모든 단일 질문에 대해 동일한 그림을 그리기 위해 for 루프에 플롯을 만들었습니다. 불행히도 PDF 파일에 경고 메시지가 나타나고 오류의 원인을 정확히 알지 못합니다 ...for-loop에서 ggplot을 사용한 플롯이 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로 저장하는 데 사용되며 보고서 내에 플롯을 포함하지 않기 위해 사용됩니다. – Gregor
'\ label {abc}'는'\ ref {abc}'를 허용합니다. 그러나'\ label {figure/class }'(''주위에 공백이 있음)에'\ ref {} '(공백 포함)을 사용하고 있습니다. – Werner
... [또한 \ centering' 대신에'\ begin {center}'를 사용해야 할 때?] (https://tex.stackexchange.com/q/23650/5764)'\ centering'을 사용해야합니다. . – Werner