Ecstasia Tisiphoni
2016-Aug-03 21:20 UTC
[R] Plotting in LaTeX with ggplot2 in R and using tikzdevice
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...
Jeff Newmiller
2016-Aug-04 00:57 UTC
[R] Plotting in LaTeX with ggplot2 in R and using tikzdevice
I would think knitr package would be useful in this endeavor. And possibly RStudio.... If that doesn't do it, someone here may have a better hint, but solving this kind of question can require studying both the input (R code) and output (tikz/LaTeX code). While the R code belongs here, details of the rest get OT pretty quick. -- Sent from my phone. Please excuse my brevity. On August 3, 2016 2:20:17 PM PDT, Ecstasia Tisiphoni <ecstasia1 at gmail.com> wrote:>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... > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
Paul Murrell
2016-Aug-04 01:48 UTC
[R] [FORGED] Plotting in LaTeX with ggplot2 in R and using tikzdevice
Hi You might need an approach that converts the ggplot object to a gtable and then either combine the gtables as here ... http://stackoverflow.com/questions/16255579/how-can-i-make-consistent-width-plots-in-ggplot-with-legends ... or explicitly control the width of the plot within the gtable layout as here ... http://stackoverflow.com/questions/30571198/how-achieve-identical-facet-sizes-and-scales-in-several-multi-facet-ggplot2-grah/30571289#30571289 Hope that helps Paul On 04/08/16 09:20, Ecstasia Tisiphoni wrote:> 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... > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Dr Paul Murrell Department of Statistics The University of Auckland Private Bag 92019 Auckland New Zealand 64 9 3737599 x85392 paul at stat.auckland.ac.nz http://www.stat.auckland.ac.nz/~paul/
Ulrik Stervbo
2016-Aug-04 04:51 UTC
[R] [FORGED] Plotting in LaTeX with ggplot2 in R and using tikzdevice
I saved my plots as pdf and used pdflatex. It's a few years ago and now you can even use the r-package cowplot to create panels with subfigures. That means more work with r, less manual work. I believe kable from the knitr package can export tables for latex too. Hope this helps. Ulrik Paul Murrell <paul at stat.auckland.ac.nz> schrieb am Do., 4. Aug. 2016 03:50:> Hi > > You might need an approach that converts the ggplot object to a gtable > and then either combine the gtables as here ... > > > http://stackoverflow.com/questions/16255579/how-can-i-make-consistent-width-plots-in-ggplot-with-legends > > ... or explicitly control the width of the plot within the gtable layout > as here ... > > > http://stackoverflow.com/questions/30571198/how-achieve-identical-facet-sizes-and-scales-in-several-multi-facet-ggplot2-grah/30571289#30571289 > > Hope that helps > > Paul > > On 04/08/16 09:20, Ecstasia Tisiphoni wrote: > > 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... > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > > -- > Dr Paul Murrell > Department of Statistics > The University of Auckland > Private Bag 92019 > Auckland > New Zealand > 64 9 3737599 x85392 > paul at stat.auckland.ac.nz > http://www.stat.auckland.ac.nz/~paul/ > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Richard M. Heiberger
2016-Aug-05 04:19 UTC
[R] Plotting in LaTeX with ggplot2 in R and using tikzdevice
I suggest the microplot package that I placed on CRAN several weeks ago. Description: Prepare lists of R graphics files to be used as microplots (sparklines) in tables in either LaTeX or HTML files. For LaTeX use the Hmisc::latex() function or xtable::xtable() with Sweave, knitr, rmarkdown, or Emacs org-mode to construct latex tabular environments which include the graphs. For HTML files use either Emacs org-mode or the htmlTable::htmlTable() function to construct an HTML file containing tables which include the graphs. Examples are shown with lattice graphics, base graphics, and ggplot2 graphics. Examples for LaTeX include Sweave (both LaTeX-style and Noweb-style), knitr, emacs org-mode, and rmarkdown input files and their pdf output files. Examples for HTML include org-mode and Rmd input files and their webarchive HTML output files. In addition, the as.orgtable function can display a data.frame in an org-mode document. For your task, the idea would be to construct a multi-panel plot using ggplot and then capture each panel into a separate pdf file. The files would then all be scaled identically. Capture the legend material into a separate pdf file. Use the as.includegraphics function on each pdf filename and place the resulting strings into a LaTeX table with the Hmisc::latex function. Please see the vignette included with the microplot package for details. vignette("rmhPoster", package="microplot") The vignette is my poster session from the useR conference last month at Stanford. The complete R code for all examples in the vignette is in file paste0(system.file(package="microplot"), "/doc/rmhPoster.R") The vignette and the examples use lattice. A simple working example using ggplot is included in the ?microplot help file. Simple working examples are also shown for base graphics and for all the output options listed in the Description. Rich On Wed, Aug 3, 2016 at 5:20 PM, Ecstasia Tisiphoni <ecstasia1 at gmail.com> wrote:> 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... > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.