How to disable transparency in knitr plots? - r

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. :-)

Related

Holoviews - how to set legend click_policy for decimated points

I'm trying to set click_policy to 'hide' in legend_opts for the object resulted from applying holoviews.operation.decimate on a set of hv.Points, but it doesn't seem to have the desired effect.
Now I'm using the muted_alpha option on the underlying hv.Points to hide the points, but this is suboptimal, since hovering over the invisible points still shows the tooltip, which is cluttering the output when showing multiple curves.
I've tried passing legend_opts={'click_policy': 'hide'} as opts for the Overlay container, as well as for both the underlying hv.Points and for the result of decimate (actually, I also tried passing it as an argument to decimate), but to no avail.
I even tried using a hook for the bokeh plot as suggested here to directly set plot_obj.state.legend.click_policy = 'hide', but that doesn't work either.
So, any idea on how to hide a set of decimated hv.Points completely (i.e., not showing the hover tooltips either)?
Note: I've followed the "Principles of datashading" section of holoviews' user guide on "Working with large data" and used decimate as indicated there.
Turns out that one has to set the options for hv.Overlay after calling collate on it.

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.

Modify SVG width from within R after export from plotly orca

My goal is to save out an SVG file of a plotly graph, where the width argument is '100%' and static height is removed.
As it is, plotly orca (the tool for saving plotly objects) only supports specifying static pixel widths and heights.
The widths and heights set are important throughout the chart generation for maintaining proportions (It is an intricate plot combining many traces on both cartesian and polar co-ordinates).
This means the svg file saved is fixed width.
All processing has to occur within R. Is there a way to modify a text file from R commands? So far, my search has not yielded anything useful.
I have found my solution.
I am using readLines() to load my .svg file in to R.
Modify the offending style attribute using gsub. This works for me as the change I want to make is fixed and very simple.
I use writeLines() to over-write the old with the new.

Make Octave set a figure to be the active window (come to the "front of the screen")

When Octave draws a plot, I would like it to set that to be the active window automatically, so that it becomes visible and I don't have to click back and forth between windows to see if the code and plot have finished. Is this possible? Since it would require reaching outside of Octave and controlling the OS, I'm not sure; it depends on whether or not that capability is part of Octave but I haven't found a reference for it yet.
I can always tell Octave to close the figure before opening a new one in the code, but that could prevent me from drawing multiple plots on the same axes, and it would require me to code that command in every time. It would be nice if there were a direct way just to bring the plot to be visible and take dominance over other windows.
EDIT: Somehow, although I noted that Octave would be required to control the OS to achieve this, I completely forgot to mention what that was... I'm running Windows 10 with the default window manager; I believe that would be the Desktop Window Manager.
When you plot something on a figure (whether you specify the figure you're plotting to within the plot command explicitly or simply let it plot into the currently active figure implicitly), this does not automatically raise the figure window to the forefront.
To do so, call the figure again using the figure function, along with the handle that you want to raise.
Alternatively, if you're sure that the figure you want to raise is the currently active one, you can simply use the shg command (which is effectively equivalent to figure(gcf))
E.g.
Fig1 = figure; % (or figure(1) if you want to be explicit)
Fig2 = figure; % (or figure(2) if you want to be explicit)
figure(Fig1); plot(1:10); % raise Fig1 to the forefront, and plot.
PS: Note that there was a bug affecting this behaviour until recently (coincidentally submitted by yours truly :p See https://savannah.gnu.org/bugs/?45943 ). This is fixed in the latest version of octave though (i.e. 5.1.0)

How to fix clipped output from R postscript function?

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)

Resources