2

I have got the following problem: When I plot anything with ggplot2 like this

# 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()

The resulting graph looks like this where the text is shown in weir unicode blocks:

ggplot2 graph with text issue

enter image description here These unicode blocks look like boxes with four numbers starting with two 0s like:

# Example block
----
|00|
|2C|
----

I already tried to update and reinstall the tidyverse package, I reopened R-Studio and only called the library ggplot2 in order to have no conflicting packages open, I could not find any similar issue on the internet whatsoever. 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


EDIT: For anybody who is interested in solving this issue permanently look here. I had to upgrade to R Version 4.0.3 in order to make ggplot work properly again.

tjebo
  • 21,977
  • 7
  • 58
  • 94
Tbroeth
  • 345
  • 2
  • 12
  • Yoy may try some of this for troubleshoothing further - https://stackoverflow.com/a/60759792/4124601 ; I suspect it may be related to system fonts or some highly unusual language setting – giocomai Jan 26 '21 at 09:21
  • I think this is a conda issue because system fonts cannot be accessed from inside the conda environment. See here: https://stackoverflow.com/questions/60401617/r-draws-plots-with-rectangles-instead-of-text – teunbrand Jan 26 '21 at 09:35
  • Thank you for your reply, I tried to look through that post and I know that I changed my LC_TIME once with "Sys.setlocale("LC_TIME", "C")" and now "Sys.getlocale()" shows a mix between "en_US.UTF-8" and "de_DE.UTF-8" (since I am german). Additionally the "X11.options()" shows that my type is set to "cairo", as this seems to be correct according to the author of the post. – Tbroeth Jan 26 '21 at 09:42

2 Answers2

1

This looks a lot like a font issue. Maybe the default ggplot font is not installed or damaged? Try querying installed fonts (for Linux):

system("fc-list")

Output should be a list of entries like this:

/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf: DejaVu Serif:style=Book

Then you can set the font as a default for your ggplot theme (name of the ttf file is the correct way to name it as far as I can tell):

theme_set(theme_gray(base_family = "DejaVuSerif"))

If this returns the same result, try another one.

JBGruber
  • 11,727
  • 1
  • 23
  • 45
  • Thank you very much! Your solution worked perfectly. I merely set it to "Arial" since this is the base font of R, as far as I understand. – Tbroeth Jan 26 '21 at 10:12
  • Yes, the default argument is `base_family = ""` which defaults to "Arial" on many systems. I found a few mentions that the default is `"sans"` which defaults to "Arial" as well. Anyway, your system seems to do something weird with these defaults. – JBGruber Jan 26 '21 at 10:38
  • I would be really interested in why my system deals with these defaults so weirdly. Even though they worked perfectly fine a few days ago. – Tbroeth Jan 26 '21 at 14:37
0

try to solve the problem by changing the text size and the color and theme of you're text? maybe that will work... sth like this:

ggplot(data, aes(x=xValue, y=yValue)) +
geom_line() + theme(text = element_text(size=20))
  • Thank you for your answer, but unfortunately it does not work. I still only see the blocks as text. I already though of this myself since I messed around with "theme(axis.text = element_text(color = "black", size = 12)" but I already tried different sizes and also without any changes to text, which did not result in any changes to the problem. – Tbroeth Jan 26 '21 at 08:49
  • Another strange thing is, that for example plotly plots look completely normal and do not have the same issue. So I reckon that the problem is most likely associated with ggplot2 (or tidyverse in general). – Tbroeth Jan 26 '21 at 09:02