ggplot2 issue with default font size (base_family = "") - r

As you can see in this post someone found the solution to my problem where the text in my ggplot2 graphs were replaced by unicode-blocks. This was caused by an error in the default font settings of ggplot2 (base_family = ""). Therefore, the workaround was to manually set the base_family argument to "Arial".
Here you can see an example code:
# Libraries
library(ggplot2)
# create data
xValue <- 1:10
yValue <- cumsum(rnorm(10))
data <- data.frame(xValue,yValue)
# Plot
ggplot(data, aes(x=xValue, y=yValue)) +
geom_line() +
theme_classic()
The resulting graph looks like this where the text is shown in weird unicode blocks (sorry I do not know what these are called exactly):
I can manually solve the issue by setting the theme base_family to "Arial":
# Libraries
library(ggplot2)
# create data
set.seed(42)
xValue <- 1:10
yValue <- cumsum(rnorm(10))
data <- data.frame(xValue,yValue)
# Plot
ggplot(data, aes(x=xValue, y=yValue)) +
geom_line() +
theme_classic(base_family = "Arial")
This is the image of the resolved issue, which only works if the base_family is set to a specific font like "Arial":
The question is why does my system somehow conflict with the default font and how can I set the default font back to normal? Because now I have to call the base_family = "Arial" Argument in every plot I make with ggplot2. I should mention, that I have no font issues with e.g. plotly whatsoever. I have not found any similar problems except a link on how to change the default setting for a specific theme type but I would like to reset the settings back to normal so base_family = "" works again. I hope you can help me out and please do not hesitate if you need further information from me.
R version: 3.6.1 (2019-07-05)
platform: linux mint x86_64
conda environment

Thanks to #tjebo I was able to resolve the issue by upgrading my R version to 4.0.3. and switched to ipython notebook since as far as I understood R-Studio does not support version 4.0.3 (EDIT: probably only an issue caused by conda and not by R-Studio).
I work with conda and therefore I created a new environment
# Create and activate conda environment
conda create --name r4
conda activate r4
Then I installed the R Verison 4:
# install R version 4
conda install -c conda-forge r-base
conda install -c conda-forge/label/gcc7 r-base
Finally, I installed irkernel in order work with R in IPython notebooks:
# Install R kernel for IPython notebook
conda install -c r r-irkernel
IRkernel::installspec()
# open jupyter notebook
ipython notebook
Inside the ipythone notebook I could now choose "R" as a kernel and the code I previously had issues with works as expected. Again credits to #tjebo.
EDIT: Reinstalling R lead to further issues where packages were not beeing installed correctly due to missing lib files. But this is offtopic so I will probably open another discussion somewhere else. (FYI Link to the solution was postet by #fredaas here)

Related

how do I fix plot problem with R in Jupyter notebook? [duplicate]

My R was installed in a conda environment on Ubuntu 20.04.1 VM.
I was searching for this problem when i found this thread. I too am using RStudio from Anaconda, on Debian 10.0 (buster).
Try
par(family = "Arial")
or whichever font name before calling plot(). It worked for me.
Update > I still had problems with ggplot2 plots not displaying labels, like above. I tried the workaround of using showtext() instead of regular text rendering.
install.packages("showtext")
After it was installed,
library(showtext)
to load the library. Then simply call
showtext_auto()
followed by the ggplot(.....) + ... + etc. function and text was rendered alright within the plot. I did not load specific fonts using font_add(). Maybe because previously 'Arial' was loaded with par()

GhostScript was not found with extrafont

I'm using an M1, Big Sur Macbook. I need to embed the fonts of a number of pdfs that include plots from ggplot2.
However, when I run the embed_fonts() function, it returns the following error message: GhostScript was not found
With Homebrew, I installed Ghostscript. I have also reinstalled extrafont and extrafontdb, restarted RStudio, and then run font_import() and loadfonts() again. None of this solves the error message.
Have you experienced this problem? I wonder if it's because of the change to the M1's Apple Silicon?
I have also switched from using bash to zsh in my terminal. Could that have affected this?
A reprex:
library(ggplot2)
library(extrafont)
(plot <- ggplot(cars, aes(x = speed, y = dist)) +
geom_point())
ggsave("test_plot.pdf", plot)
embed_fonts(file = "test_plot.pdf", outfile = "test_plot_embedded.pdf")
I found the following two answers that seem related, but I'm unsure how to implement them:
How to fix "Unable to find GhostScript executable to run checks on size reduction" error upon package check in R?
R does not recognize GhostScript to embed eps plots
My issue was solved when I installed Ghostscript directly https://pages.uoregon.edu/koch/ (Ghostscript 9.54.0)
It appears there was, at least on my end, an issue using homebrew install ghostscript. When I ran that in Terminal, everything appeared to be fine. No error messages.

Text does not show properly in R plot

My R was installed in a conda environment on Ubuntu 20.04.1 VM.
I was searching for this problem when i found this thread. I too am using RStudio from Anaconda, on Debian 10.0 (buster).
Try
par(family = "Arial")
or whichever font name before calling plot(). It worked for me.
Update > I still had problems with ggplot2 plots not displaying labels, like above. I tried the workaround of using showtext() instead of regular text rendering.
install.packages("showtext")
After it was installed,
library(showtext)
to load the library. Then simply call
showtext_auto()
followed by the ggplot(.....) + ... + etc. function and text was rendered alright within the plot. I did not load specific fonts using font_add(). Maybe because previously 'Arial' was loaded with par()

Error: ScalesList was built with an incompatible version of ggproto

I'm doing a presentation in slidfy, using the deckjs framework.
Everything was ok, but suddenly this chunk of code:
ggplot(cars, aes(x = speed, y = dist)) + geom_point(color = 'red') + stat_smooth(method = "lm", formula = y ~ x, size = 0.5, se = F)
stopped working and shows this error instead:
## Error: ScalesList was built with an incompatible version of ggproto.
## Please reinstall the package that provides this extension.
The code works perfectly when executed from source or console... But it doesn't work anymore from the R markdown. The function that fails is the stat_smooth(). The rest visualizes ok if executed without the smooth.
Here and here a similar error is reported, and the solution offered is to reinstall ggplot and the packages from github, but I'm not sure which packages should I install, and, besides, the code only fails from Rmarkdown, and not when executed from console or source.
Thanks
I had a similar problem in RMarkdown after updating ggplot2. I was loading a workspace with plots created with the older version of ggplot2. The solution was to recreate that workspace with the updated version, and now RMarkdown works.
This is very strange; now things are back to normal.
I did what #baptiste suggested, and the R source code just worked ok.
After that, I knitted the Rmarkdown again and the error had disappeared!
in Feb 2017 the same error was resolved by re-installing all the packages (RStudio - Tools - Check for package updates)
I had similar problem with geom_tufteboxplot . I first updated ggplot2 & ggthemes package, then detached all ggplot related packages and reloaded them. It worked for me.

rgl does not find font family

I face a strange issue with R and package rgl on my Desktop Ubuntu 14.04. While on my laptop with Ubuntu 12.04
title3d(xlab="Mean Market 1", pos=c(1,0,0), col="black",adj=c(0.8,0.8),
family="Times", cex=1.1)
works perfectly, I get a warning message
font family "Times" not found, using "bitmap"
on my desktop pc. Plotting works, but titles are pretty small. If I check
names(X11Fonts())
# [1] "serif" "sans" "mono" "Times" "Helvetica"
# [6] "CyrTimes" "CyrHelvetica" "Arial" "Mincho"`
but none works. I installed freetype2 package, reinstalled rgl package but the warning is still there. I tried sans font etc., as well.
Carsten Oppitz,
I experienced the same issues that you mention above in my computer with Ubuntu 14.04. After searching for fixing them for a long time and nothing worked, the instructions below, copied from http://www.smnd.sk/kotanyi/index.php?page=rgl, did work perfectly:
a) Terminal, check that you have GNU version >= 3.80:
make -v
b) Download FreeType from: https://sourceforge.net/projects/freetype/files/ e.g. latest version.
c) Extract the downloaded file, enter in the directory where it is saved and type in:
./configure
make
sudo make install
d) Still need to rebuild rgl in R: download its source code from the link appearing in the first link above; without extracting the file, type in:
R CMD INSTALL name_of_archive.tar.gz
e) Enjoy any text size (that R supports) in plot3d objects.
You can only really count on "serif", "sans", "mono" and "symbol", unless you've used rglFonts to install additional Freetype font files. So "Times" should be "serif".

Resources