3

I am a total newbie to R and LaTeX, and trying to write my masters thesis right now... I tried to get this answered via cran.r-project.org...but I failed... :(

I am creating plots in R via ggplot2, and converting them to TeX format via tikzDevice.

Now many of my plots have a legend on the right, which differs in size (depending of course on the legend title and text).

If I now convert my Rplot using

tikz(file = "my_output_file.tex", standAlone=F,width = 6, height = 4)

it only scales the size for the whole image it creates.

What I want is: the rectangular plot itself to always be the same size for all my plots...

My Rscript with some test Data:

library(ggplot2)
library(scales)
require(grid)
library(tikzDevice)


#setting time zone
options(tz="Europe/Berlin")

tikz(file = "my_output_file.tex", standAlone=F,width = 6, height = 3)


cars['dt'] = seq(Sys.Date(),Sys.Date()-980,-20)
plot <- ggplot(cars,aes(y=speed,x=dist,color=as.integer(dt)))+
               geom_point(size=2,alpha=0.7)+
               xlab("distance")+
               ylab("speed")+
               scale_color_gradientn("dt",
                                     colours=rainbow(6)
                                     )+

#textsize
theme_bw()+
theme(legend.position="right",
      legend.key.height=unit(2,"lines"),
      legend.title=element_text(size=rel(0.8)),
      legend.text=element_text(size=rel(0.8)),
      axis.text.y=element_text(angle=90, 
                               hjust=0.5),
      axis.title=element_text(size=rel(0.8))

print(plot)

dev.off()

not_same_size

I hope somebody can help me, or lead me to the information I need...

------- EDIT: -------

I managed to set the size of the plots equal, by entering

aspect.ratio = 2/(1+sqrt(5))

to the theme() of my Rplot.

Now why is it, that LaTex still doesn't get the positions right?

not centered

Here my LaTeX code:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{figure}[h]
    \setlength{\abovecaptionskip}{-15pt} 
    \raggedright
    \input{my_output_file1.tex}
    \caption{Some long and useful Caption text}
    \label{plot:cars}
\end{figure}


\begin{figure}[h]
    \setlength{\abovecaptionskip}{-15pt} 
    \raggedright
    \input{my_output_file2.tex}
    \caption{Some even longer and more useful Caption text, just to see how it looks like}
    \label{plot:cars2}
\end{figure}
\end{document}

I already tried replacing raggedright by flush-, tried replacing the [h] to \begin{figure}[l]... The plot-positions don't even change when I set \centering.

Also tried this with several different values for op <- par(mar = rep(1, 4)) to remove possible white space around the plot... but it just doesn't change at all...

What am I missing? Why does it obviously align to the right?

  • I think this answer to a similar question may help you further. If it doesn't, please report back with details about what's not working or why it's not a solution. That'll help coming up with a better answer. – Yuri Robbers Aug 03 '16 at 21:56
  • @YuriRobbers Thank you for your answer. The problem is, that I will have ~15 different plots throughout my Masters thesis and just want the plotsize to be consistent. The approach in your link is only useful, if all plots are plotted in the same Rfile which would be my last resort, but could get messy very quickly. I meanwhile managed to use aspect.ratio = 2/(1+sqrt(5)) as mentioned here. – Trillian Astra Aug 03 '16 at 23:07
  • Oh, yes! I see your problem. Then the solution I linked to is not convenient at all. Is the aspect ratio a satisfying solution for you? If yes, then maybe you should write it down as an answer and accept it. If not, then I'll dig a little deeper for you. – Yuri Robbers Aug 04 '16 at 10:28
  • @YuriRobbers partly answered (see below) and still not satisfied (see EDIT above) ^^ – Trillian Astra Aug 04 '16 at 12:54
  • 1
    Here is another similar questiona and answer: http://stackoverflow.com/questions/36097391/align-panel-background-instead-of-plot-background-with-document-margins – Hugh Sep 03 '16 at 13:09
  • Although with only 15 plots for a thesis, it might be worth just doing it manually for each of them. – Hugh Sep 03 '16 at 13:11
  • @Hugh cool! thank you for the link! This worked perfectly (I just put the legends inside the margins of the box, and everything looks wonderful!) – Trillian Astra Sep 06 '16 at 17:13

1 Answers1

2

The plotsize can be regulated by using

aspect.ratio = 2/(1+sqrt(5)

as mentioned here (in my comment above, I copied the wrong source. sorry.)

In code:

tikz(file = "my_output_file1.tex", 
     standAlone=F,
     width = 7, 
     height = 3,
     )

cars['dt'] = seq(Sys.Date(),Sys.Date()-980,-20)

plot1 <- ggplot(cars,aes(y=speed,x=dist,color=as.integer(dt)))+
               geom_point(size=2,alpha=0.7)+
               xlab("distance")+
               ylab("speed")+
               scale_color_gradientn("dt",
                                     colours=rainbow(6)
                                     )+

#textsize

               theme_bw()+
               theme(legend.position="right",
                     legend.key.height=unit(2,"lines"),
                     legend.title=element_text(size=rel(0.8)),
                     legend.text=element_text(size=rel(0.8)),
                     axis.text.y=element_text(angle=90, 
                                              hjust=0.5),
                     axis.title=element_text(size=rel(0.8)),

                     aspect.ratio = 2/(1+sqrt(5))
                    ) 

print(plot1)     

dev.off()

About the positioning... still wondering ...