Question

I'm trying to generate a histogram with knitr and the tikzDevice for a beamer presentation.

I have already generated the histogram using the console in R-studio so it appears the data is formatted correctly but compiling the document (using R-studio) with the dev=tikz option (or 'dev=pdf', for that matter) just produces a blank slide with header and no errors (as far as I can tell).

The data looks like this:

   Dag Okt   Ar Bes
1    M   K 2009 522
2    M  Vm 2009  89
3    T   A 2009   0
4    T  Sg 2009 252
5    T  SS 2009   0
6    O  Vo 2009   0
7    O  St 2009 238
8    O   B 2009 107
9   To  Vt 2009 249
10  To  Tu 2009 121
11   F  Sk 2009 415
12   F  Tt 2009   0
13   M   K 2010 558
14   M  Vm 2010 283
15   T   A 2010  36
16   T  Sg 2010 211
17   T  SS 2010   0
18   O  Vo 2010   0
19   O  St 2010 381
20   O   B 2010 164
21  To  Vt 2010 260
22  To  Tu 2010  99
23   F  Sk 2010 397
24   F  Tt 2010  19
25   M   K 2011 571
26   M  Vm 2011 302
27   T   A 2011 273
28   T  Sg 2011 183
29   T  SS 2011  84
30   O  Vo 2011   0
31   O  St 2011 368
32   O   B 2011  53
33  To  Vt 2011 298
34  To  Tu 2011  92
35   F  Sk 2011 346
36   F  Tt 2011  30
37   M   K 2012 479
38   M  Vm 2012 382
39   T   A 2012 146
40   T  Sg 2012 181
41   T  SS 2012  57
42   O  Vo 2012  83
43   O  St 2012 379
44   O   B 2012  54
45  To  Vt 2012 507
46  To  Tu 2012 173
47   F  Sk 2012 238
48   F  Tt 2012  55

The code is:

\documentclass{beamer}
\usepackage[norsk]{babel}
\usepackage[utf8]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc}
\usepackage{pgf}
\usepackage{tikz}

\mode<presentation>
{
  \usetheme{Frankfurt}
  % or ...

  \setbeamercovered{transparent}
  % or whatever (possibly just delete it)
}



\title[]{Title}

\subtitle{} % (optional)


\title{Title}
\author{JB}
\date{\today}
\if pdf
\pdfinfo {
  /Author (JB)
  /Title (Title)
  /Subject ()
  /Keywords ()
  /CreationDate (D:20130531)
}
\fi
\begin{document}

\maketitle

\section{Intro}
\begin{frame}
\frametitle{Test}
\begin{itemize}
\item Test
\end{itemize}
\end{frame}


\section{Data}

% Hidden R code chunk --- reading in the data
<<setup, echo=FALSE,message=FALSE>>=
require(ggplot2)
require(tikzDevice)
histdata<-read.csv("data.csv")
histdata$Ar<-factor(histdata$Ar)
@

\begin{frame}
<<histogram,echo=FALSE,dev=tikz,cache=FALSE>>=
ggplot(histdata,aes(x=Dag,y=Bes,fill=Ar))+geom_bar(position="dodge",stat="identity")+ scale_fill_grey(start=0.8, end=0)
@
\end{frame}
\end{document}
Was it helpful?

Solution

Well, I appear to have solved my own question. Not really sure as to the cause of problem but I will explain how I went about finding out wasn't working.

As usual in these situations I had to start by pairing down the code to the bare essentials, which was in this case left only the beamer template without the language packages.

I then added single code chunk with only a simple label name and the device <<nrone,dev='tikz'>>= making sure I put the single quotes around the 'tikz'.

I then gradually added the elements into the code chunk, beginning with importing the data.

When I imported the data I removed the rows with 2010-2012 in the histdata$Ar column to simplify the plot and then gradually added in the missing data when I was sure it compiled.

While adding in the data I also added the extra settings to the ggplot line to group the data correctly, make the colour scheme black and white, and tilt the labels on the x-axis.

The plot was a bit big so I added in the out.width setting to make it fit and put the cache=FALSE to makesure it was refreshing the picture each time I compiled, as well as echo=FALSE,message=FALSE to remove the extraneous text.

I then divided the code chunk in to two with the data import in one code chunk and the plotting of the data into the second as before.

Having gone through this process, I realised I didn't need the require(tikzDevice), which I had in before.

The corrected code is as follows:

\documentclass{beamer}
\usepackage[norsk]{babel}
\usepackage[utf8]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc}

\mode<presentation>
{
  \usetheme{Frankfurt}
  % or ...

  \setbeamercovered{transparent}
  % or whatever (possibly just delete it)
}



\title[]{Title}

\subtitle{} % (optional)


\title{Title}
\author{JB}
\date{\today}
\if pdf
\pdfinfo {
  /Author (JB)
  /Title (Title)
  /Subject ()
  /Keywords ()
  /CreationDate (D:20130531)
}
\fi
\begin{document}

\maketitle


% Hidden R code chunk --- reading in the data
<<setup, echo=FALSE,message=FALSE>>=
require(ggplot2)
histdata<-read.csv("data.csv")
histdata$Ar<-factor(histdata$Ar)
@

\begin{frame}
<<histogram,echo=FALSE,message=FALSE,dev='tikz',cache=FALSE,out.width='.8\\textwidth'>>=
ggplot(histdata,aes(x=Okt,y=Bes,fill=Ar)) + geom_bar(position="dodge",stat='identity') +  scale_fill_grey(start=0.8, end=0) + theme(axis.text.x = element_text(angle=30, hjust=1, vjust=1))
@
\end{frame}
\end{document}

I hope this helps someone in a similar situation :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top