How to fix clipped output from R postscript function? - r

The output from the postscript function is generating clipped output. See the legend on the right side of the figure below, the last legend entry should read SBW-25. I've tried changing the width argument to postscript but this has had no effect. I've also tried manually editting the bounding box in generated postscript but this also has had no effect.

Wihtout a reproducible example, it is hard to diagnose, but here are some things to try.
If you are viewing the postscript in GSView, you probably just need to increase paper size. Click Media -> A0 (or something suitably big).
If you save to a different format (PDF, PNG, whatever), do you get the same effect?
If all else fails, try manually changing the legend position with
p + opts(legend.position = whatever)

Related

Change plot window size in Julia notebook

I am using Julia notebook and making plots using basic Plots package.
A plot looks good, except its entire size.
I can change the plot size and font size of labels individually. But it becomes less readable unless I change the font size and line width for every component.
So is there a way to change the size of a plot as a whole? I also hope I can change it by default.
To answer your original question in the title, you can change the size of the Plot Window by specifying the size attribute as you already are in your code. See here: http://docs.juliaplots.org/latest/basics/ for more details.
As pointed out by Rashid, you can use the scalefontsize function to scale the font size. You can also scale the thickness by setting the thickness_scaling attribute, see here for more details: http://docs.juliaplots.org/latest/generated/attributes_plot/
To be clear, there is not currently a unified way to scale a plot in the way you are looking for it right now, it has to be done manually (though it would be great to have this unified scaling). I opened a feature request for this here: https://github.com/JuliaPlots/Plots.jl/issues/3153

R: text of legend falls outside of plot when exporting to PDF

When I add a legend to my R plot (using the legend() function), then it works in Rstudio, but when I export the plot as a PDF with a different size, then sometimes the text inside the legend is larger than the legend box itself.
What is going on here and how do I fix it?
If I export the image with a larger width, then the legend box becomes larger too and manages to contain its text, but this is silly: I want the legend box and its text to automatically adjust to whatever width I choose, no matter how small it is.
And please don't recommend ggplot2.
As #Gregor said - you should use pdf(). I am merely adding his suggestion as an answer. Your code would be something like this:
pdf("picture.pdf", width=6, height=6)
plot(...)
legend(...)
dev.off()
Where pdf() opens a new device for plotting (of course you have to set file name as well as dimensions according to your needs), and dev.off() closes the device writing everything to a file.
I am not sure what causes the issue with legend in R-studio. But on my machine I noticed that legends sometimes have issues with updating after resizing the device. Probably this is something related.

How to disable transparency in knitr plots?

I have a LaTeX/knitr document that I need to convert to PDF/A, but I'm getting errors about transparency being used (which is not allowed in PDF/A). I traced it back to the background fills of the boxplots that I'm generating in R via knitr. Regardless of whether I use "pdf", "cairo_pdf" or "tikzDevice" as the output device, the same error results.
The Tikz output includes the following:
\definecolor{fillColor}{RGB}{255,255,255}
\path[use as bounding box,fill=fillColor,fill opacity=0.00] (0,0) rectangle (505.89,325.21);
Manually removing the fill opacity=0.00 part and rerunning only pdflatex gets rid of the message for a particular graph, so I'm pretty sure that's the cause. However, I'd prefer to make the change in the source file (R code), so I don't have to manually make this fix every time the source changes and the intermediate files are regenerated.
Does anyone know the magic option to feed bxp or par in R that would translate into an opacity of 1.0, or, better still, specify no fill at all? (The fill is completely unnecessary, given that it's a white background being placed on an empty part of a page.)
(BTW, I tried bg=NA in the bxp call, and par(bg=NA) before, and neither had any effect. For that matter, using "red" also had no effect, so that doesn't seem to be the right option.)
Apparently, the bg in the graphics parameters (par=) only applies to objects within the plot (like the boxes in a boxplot). The background for the whole plot is set in knitr's dev.args chunk option, e.g., globally:
opts_chunk$set(dev='tikz', dev.args=list(bg="white"))
or at the start of a particular chunk. It appears there's no way to drop the fill option entirely (there's a "TODO" item in the tikzDevice source code), but changing it to "white" has eliminated the PDF/A validation errors, with no visible effect on the document. Once my thesis is in, maybe I can submit a patch for this. :-)

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

specifying font size in R figures

Is there a method for specifying the font size in when producing figures in R. This seems like a really basic requirement but I can't seem to find any references to somewhere that specifies the font size. I can save a figure to a pdf as follows:
setwd("C:\\")
pdf(file="Plot.pdf",family="Times")
plot(x,y);
dev.off()
Where R basically generates the figure in the pdf not in the figure window. When I look for ways of altering the font size all I see is people referring to cex=1.5 argument to scale fonts 150 percent, and cex.lab, cex.axis, etc ... Although not being an immediate issue now, I do wonder what will happen when I publish some results and the journal requires font size between 9 and 11. How do I control these in R? Any suggestions would be appreciated.
You control the font size with the ps (point size) parameter. The default is typically 12 (but can be controlled globally for a PDF file by the pointsize parameter) so if you want, let's say, fonts of the size 10 for a particular text you would use par(ps=10); text(...). Since you mentioned cex: note that cex is relative to the current pointsize and also applies to symbols whereas ps specifically applies to text. Obviously, the size will only match as long as you don't resize the resulting figure.

Resources