kevandrades
kevandrades

Reputation: 58

Bug in R's ggplot2 characters in VS Code

a <- data.frame(
    x = c(1, 2, 3, 4, 5),
    y = c(5, 5, 7, 3, 5))

I'm new to VS Code and I'm testing R 4.0.1 in this IDE. I used to use RStudio to do this kind of thing. This worked very well in Rstudio.

I came across a problem when I went to make graphics, the characters are weird for some reason.

install.packages('ggplot2')
library(ggplot2)
ggplot(a) +
    geom_col(aes(x = x, y = y)) +
    ggtitle('My title')

Figure obtained: enter image description here

Figure wanted:

enter image description here

How could I fix this?

Upvotes: 0

Views: 1165

Answers (1)

Douglas Cady
Douglas Cady

Reputation: 46

UPDATE (2/2021): I updated my R version from 3.6 to 4.0 and this problem was fixed. My R graphics window no longer shows weird characters! In case this doesn't work for you, see below:

@Ben Bolker suggests trying png() or CairoPNG() (with Cairo package) as well.

Original: I had the same problem happen to me and I'm using Sublime Text plus Rscript in my Linux terminal. Unfortunately, I haven't found a fix for it yet only a workaround.

Here's what I do: Use the pdf function to save the plot as a pdf. Then if you need it saved as a png or jpg, you can take a screenshot of the pdf. I know it's not very elegant, but it's worked for me so far.

a <- data.frame(
    x = c(1, 2, 3, 4, 5),
    y = c(5, 5, 7, 3, 5))

library(ggplot2)
myplot <- ggplot(a) +
    geom_col(aes(x = x, y = y)) +
    ggtitle('My title')

pdf("myplot_filename.pdf")
print(myplot)
dev.off()

Resulting plot as a png (using screenshot)

Upvotes: 2

Related Questions