Plotting in R using ggplot2 and latex - r

I am plotting a bar graph in R and exporting as eps, the plot looks excellent. When i add it to my latex file it messes up the font any recommendations ?
Here is the original eps
Here is the image in the pdf produced in the latex, i am doing
\includegraphics[width=\columnwidth]{file.eps}

Could it be that your controlling the width of the picture which is messing up the aspect ratio? In LaTeX I usually use [scale=.X] and play around a bit until it's the correct size. That way the aspect ratio is kept the same.

Related

How to export the graphs produced in R?

As a beginner on R I manage to produce correct relational graphs thanks to R. They are about correct in the visualization space but of very bad quality when I export them in PDF or in JPEG / PNG. The image is not centered, a part of the legend is missing, the graph is very small or blurred etc.
How do you proceed for the export?
Thanks in advance!
I am looking for the right handling or settings to export the graph visualizations produced in R?
I understood that I had to set up the viewer space but via the code but have no idea how to do it...
Here an example how to write a plot to a PNG file. The plot "commands" are embedded in png(....)and dev.off(). Several options are available to configure size and resolution.
png("myfile.png", width=1600, height=1200, res=300) # good for LaTeX or Word
#png("myfile.png", width=800, height=600, res=150) # good for Powerpoint or Impress
plot(iris$Sepal.Length, iris$Petal.Length)
dev.off()
Some hints:
width and height are given in pixels
res influences nominal resolution and font size (play with it)
use at least 300 dpi (dots per inch). For centimeters, the number of pixels = 300/2.54 * width in cm
professionals use 600 or even 1200 pixels per inch, but then .docx and .pptx files will dramatically increase
1600 x 1200px is good for 13.3 x 10 cm size in the printed document
If you work with LaTeX, it is in most cases better to use PDF for the figures. Another very good idea is to use Markdown for the text. Then, figures are automatically embedded.

How to crop plots with R officer package?

When exporting plots to a .docx with the officer package, a lot of white space is created around the plots.
That way it is using up way to much space in the word document. It would be nice to crop the picture to get rid of the white space around the plot, so it looks more like this:
I don't know (1) if I have to set the dimension/size of the plot when I create the plot or (2) when I export the plot to the .docx with officer package.
Because I have a lot of plots that are getting exported (which are roughly of the same content and seize), I am looking for a good way to let them look the same.

Increasing resolutions of pdf graphics in R

If I'm creating pdf figures in R, is there an input argument analogous to res (for png) that I can use to increase the resolution from the default in the following:
pdf("Dsr_2b.pdf")
hist(Dsr,breaks=200,xlim=c(-0.03,0.03),main="Figure 2b: Dsr")
dev.off()
Nothing I saw in the documentation seemed to be geared to this.

scaling the R exported plot to match R's output plot

This may sound weird, or impossible but I can say the scale of the output plot when you compile inside R and view it without zooming or exporting works great for me. However, when I export to pdf, the plot becomes outrageously large and I never manage to scale it to what I see inside R. Am I being silly, or there is actually a difference and there is a way to get what I see inside that bottom-left-corner box of the R.
use ggsaves width and length arguments to get the desired size.
ggsave('name.pdf', width =14, height = 8)

Set Legend Spacing in ggplot2

I am trying to create a plot in R using ggplot2. The figure looks great when displayed in R, but when I write it to pdf the labels in the legend slightly overlap the different color lines they are defining. How do I add white space between entries so that this does not happen?
Without code example it is hard to say, but if you don't see this problem in the R display and you see it in the pdf, you can try to increase the pdf output size. When rendering a pdf, the font size is kept and the elements of the graph are more squeezed if the output size is smaller than the displayed one.
p<-ggplot(mpg,aes(cyl,year))+geom_point()
ggsave('yourfile.pdf',p,width=10,height=10) # default is 7 on my install
I don't know if it is possible to change the spacing, but I don't see any parameters for that in the theme() or element_text() documentation

Resources