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.
Related
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.
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.
I would like to change the default font for all plots in R to Times New Roman (without having to specify them per plot). Each plotting command in R has a different setup to change the font, which can make it quite exhausting. Is that possible?
Is there an Rstudio-specific way to specify the default font size? Since Rstudio has other panels that use up space, I'd like to have a smaller font size than I would have for other ways of using R.
Go to Tools | Options | Global Options. There you can set the font. As far I can tell this persists between sessions.
Also see here.
RE #Dank's note: sorry, I see now that you mentioned plots in the question.
I think this cannot be controlled globally in RStudio, but depends on the specific graphics package you are using. So using plot(...) in base R you would use:
par(ps=10) # default starts out at 12
This sets the default point size to 8 points until you use par(...) again.
If you are using ggplot, see this link.
Hope this helps.
You may use teh parameter cex , for example
cex=0.7
it decreases the font relatively to the default font.
Having added text to an R plot with mtext
e.g.
mtext("my added text",side=1)
How can I remove it?
Make another plot. (They are dirt cheap after all). mtext like most base graphic functions operates via a one-way side-effect model of plotting. There is no underlying storage facility that can be modified after the fact. ( I in the past have tried writing over with identical col="white" letters but it looks terrible.) Failing that, if one is on an OS that has a default "Save as.." that creates a pdf file, then one can use a pdf-editing program.