[R] Plotting in LaTeX with ggplot2 in R and using tikzdevice

Ecstasia Tisiphoni ecstasia1 at gmail.com
Wed Aug 3 23:20:17 CEST 2016


Hello,
not totally sure if this is a R or a LaTeX topic...

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
https://cran.r-project.org/web/packages/tikzDevice/vignettes/tikzDevice.pdf
 ...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() 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 (no matter how big/small the legend and the axis
numbers are)...

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()


If I change now the legend text to a slightly longer text, the output
of course has a completely different plot-size.
Is there a way to maintain the plot size?

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



More information about the R-help mailing list