Exporting ggplot with extrafont using ggsave - r

To have my plot match the font used for my writing, I added the font via the extrafont package. Thus, I performed the following procedure:
library(extrafont)
font_import()
loadfonts(device = "win")
This allowed me to change the font of my plot via a change of the theme. So far, so good. Now I want to export the plot using the ggsave() function. Unfortunately, the plot is exported without the new font, looking like this:
Output without font
However, I want it to look like this:
Wanted look
Now, I read up and found some people with similar issues. However, none of the proposed solutions worked for me. I tried to use the jpeg() and png() functions, however, the resulting quality is extremely bad; even when changing size and dpi manually, it does not seem to have an effect.
Also, exporting as a pdf is not an option for me.

Related

Julia Plots-GR, export to PDF, retain text editability

I am trying to save some figures I made in Julia (Plots package, GR backend) to PDFs and I love the aesthetics + being able to save as vector graphics. However, I may need to invariably tweak things in Illustrator. When in Matlab, the plots exported as PDF are by default editable, e.g. it is an easy few clicks to replace all fonts. Something similar can also be done for matplotlib.
So my question: how can I export to PDF with editable text (this means something like Illustrator will treat text as text instead of paths) using the default backend?
Googling around, it seems the same matplotlib hack above might work in Julia via Plots.pyrcparams, but I am afraid to change backends and then have to fix a dozen plots because things broke.

How do I stop a ggplot graph legend from getting cut off on the right when exporting-saving when using showtext?

Just started R a couple weeks ago. Getting familiar with ggplot. I successfully made the graph I wanted to including all legends, axes titles, etc. I'm able to export/save this image fine. However, the assignment also required me to change the font used for the title, axes, legend text, etc. on the graph. To import the font I wanted to use, I used showtext as I saw this recommended when I Googled. When I use this, it does change the font to what I want. However, although I can see the entire graphs and its two legends fine in the Plot viewer, when I go to export it, the legend text is now cut off on the right. This appears to be the case as soon as I load the showtext package and use showtext_auto(), even if I just try to then recreate the graph using R's default font, not even with the new font. I've spent tons of time playing around and can't seem to resolve this. Please help. Also would be preferable if there is a more permanent way to get fonts into R rather than me having to load showtext and whatever font I want each and every time. Thank you very much.

Is there a way to set RStudio console output to exactly mimic PDF output

What I am doing is producing a document using rmarkdown and kniting to a PDF. This seems generally a great thing and produces professional looking output. However, it is really frustrating to try and tweak the look of graphics. The preview looks fine in Rstudio, and then when the PDF is generated there is some issue with labels not fitting, etc.
Even though my data is relatively small, and I am only producing a dozen graphs, each knit of the document takes 5 minutes or so. That means a couple of tweaks of a graph takes 15 minutes!
Is there a way to set the default size of the RStudio plot window so this doesn't happen?
Any other tricks to speed up this process?
Thanks,
David
Running dev.new() will open a plot window that is to your specified sizes.
E.g. dev.new(width=5, height=4)
(from related question)
Else you could set the figure sizes for the chunk.
{r, fig.width=5, fig.height=4}
(reference)

creating multiple file types while plotting

I would like to produce a series of plots in both high-resolution and low-resolution versions, or stated differently using two different file types (.png and .eps). I'd like to know the best/least repetetive way to do this. I am using the gplot function in sna, and the plot has a custom legend outside the plot area. I wrote a function something like this:
library(sna)
plotfun <- function(net){
png("test.png",width=800)
p <- gplot(net)
par(xpd=T)
legend(max(p[,1])+1,max(p[,2]),legend=letters[1:10],title="custom legend")
dev.off()
seteps()
postscript(test.eps)
#repeat all the plotting commands, which are much longer in real life
dev.off()
}
#try it with some random data
plotfun(rgraph(10))
This is perfectly functional but seems inefficient and clumsy. The more general version of this question is: if for any reason I want to create a plot (including extra layers like my custom legend), store it as an object, and then plot it later, is there a way to do this? Incidentally, this question didn't seem sna specific to me at first, but in trying to reproduce the problem using a similar function with plot, I couldn't get the legend to appear correctly, so this solution to the outside-the-plot-area legend doesn't seem general.
I would recommend generate graphs only in Postscript/PDF from R and then generate bitmaps (e.g. PNG) from the Postscript/PDF using e.g. ImageMagick with -density parameter (http://www.imagemagick.org/script/command-line-options.php#density) set appropriately to get desired resolution. For example
convert -density 100 -quality 100 picture.pdf picture.png
assuming picture.pdf is 7in-by-7in (R defaults) will give you a 700x700 png picture.
With this approach you will not have to worry that the picture comes out formatted differently depending which R device (pdf() vs png()) is used.

Change ploting font globally or on a per-session basis

I have a 2k lines script that generates several complex plots (plots in plots, custom plot functions etc).
I would like to change the default font to Times New Roman or Arial. Is it possible to do it globally or on a per session basis? I can't go through my script and change every plot() or text() etc. Thanks
You could change the settings at the beginning of your session using par(family="the-font-you-want"). If not overwritten afterwards this will affect all the plots in your session.
par(family="HersheyGothicEnglish")
plot(1:10, main="Stylish Font Family")
The number fonts that come along in R are quite limited though (see family parameter in ?par). You can extend the number of fonts you can apply (to use e.g. Arial) using the extrafont package.
Here you will find a good explanation of how to do that.

Resources