2

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

ggplot2 graph with font issue

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":

ggplot2 graph without font issue

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

Tbroeth
  • 345
  • 2
  • 12
  • have you already tried updating R (version 4 is game!), all packages, and any font package for linux. I am not using linux, but there might be a problem with the fonts installed. this thread, although not R, might be of help https://stackoverflow.com/a/42936670/7941188 – tjebo Jan 26 '21 at 16:33
  • Thank you very much for your reply. I will try that as soon as I can and will update if it worked! – Tbroeth Jan 26 '21 at 17:02
  • Have you tried adding `mscorefonts` to your conda environment dependencies? – teunbrand Jan 26 '21 at 17:02
  • Thank you for all your answers. So I tried to install R (version 4) but this is not compatible with R-Studio as far as I understand. Consequently, I installed R v.4.0.3 in another conda environment and installed `conda install -c r r-irkernel` to use r in ipython notebooks. But inside the notebook I noticed that the `version` was still 3.6.1. Additionally, when using the code posted above in my question, the text issue inside inside the graph remained the same.. – Tbroeth Jan 26 '21 at 22:07
  • @teunbrand I am sorry a quick search online did not answer my question on how to add `mscorefonts` to the conda environment dependencies. Could you elaborate on how to do that exactly? – Tbroeth Jan 26 '21 at 22:09
  • 1
    Ok, I got R Version 4 to work and now the issue is resolved. Thank you very much @tjebo and also thank you teunbrand for your input. – Tbroeth Jan 26 '21 at 22:57

1 Answers1

1

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)

Tbroeth
  • 345
  • 2
  • 12
  • 1
    I just nudged you, you did the hard work! Sure about RStudio and R4.0.3? may be a linux thing- It works neatly on my mac – tjebo Jan 27 '21 at 00:25
  • 1
    No I am definitely not sure. I believe this was mainly a conda issue where I was just not able to install R-Studio through conda if R4.0.3 was installed. – Tbroeth Jan 27 '21 at 08:24