Text does not show properly in R plot - r

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

Related

Rmarkdown fails to render inline plots when opened within an R project on GoogleDrive

I have an R project held onto a Google Drive. I access both the R project and associated markdowns using the Google Drive desktop app. I recently updated my macOS to Monterey 12.2.1 and since then, I haven't been able to render plots inline within a markdown. I get the following error message when trying to print any plot:
Error in dev.off() :
QuartzBitmap_Output - unable to open file '/Volumes/GoogleDrive/Shared drives/.../.Rproj.user/shared/notebooks/-.../1/s/cqgw7b5dagxzw_t/_rs_chunk_plot_001.png'
This seems to be specific when opening a markdown, within an R project, onto a Google Drive. I have tried having a project on my local machine and opening the same markdown, plots display inline fine. I have tried opening the markdown outside of a project, again, can display plots inline fine. The plots also display in the viewer pane fine regardless and knitting is also not an issue.
The only clue I have is that when I go within the .Rproj.user directory, I can find a folder with the naming 'cqgw7b5dagxzw' (see path above), but it is missing the '_t'. Not sure what that could mean though.
Since updating the OS I have also reinstalled XQuartz.
I can still get to the plots, so it's not a HUGE issue, but the inline display feature is rather handy to make quick modifications to my visualisations.
Below are some session info.
Thanks!!!
PS: Plot twist! I can display plots inline with ggplotly?!?!
Workaround
I found this workaround which displays the plots in the viewer pane instead. It is definitely an improvement, but displaying plots inline allows you to get an idea of the proportions of the plot you will eventually knit.
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.2.1
Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
I found an issue someone logged on Rstudio github relates to this, linked here.
It seems you're right about the niche scenario of Rmarkdown + R Project + Google Drive + Inline plotting.
Someone in the comments suggested going to Preferences>General>Graphics and changing the back end option to Cairo instead of Quartz. Tried that on my end and seems to work when I tried a basic histogram with GGplot2.
Not sure if using Cairo makes anything else worse though.
Using MacOS Monteray (12.3.1) if that helps.

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 not displaying in ggplot2 plot using rpy2 magic

When I generate any ggplot2 plot using R magic in Jupyter Notebook, all text in the plot is rendered as little empty boxes.
My environment is running the notebook via Jupyter Hub on Ubuntu server. R magic is working great in general, with the goal of re-using some existing R code I have to output a Word Cloud via wordcloud package. However, when I generate any plot using ggplot2 any text is missing and all I see are small empty boxes.
library(ggplot2)
ggplot(mtcars, aes(x = drat, y = mpg)) +
geom_point()
see image here
The plot should show text on the axis labels but no text is displayed, just empty boxes.
Generating the same plot when running R from ssh terminal and saving it to disk (it's on a headless server) looks fine. Generating plot via pandas data frame in a different cell looks fine. So it seems related to rpy2? I'm stumped.
[EDIT]: in fact I see the same behavior using R kernel on this server, so it seems to not be specific to rpy2 / R magic.
I was able to solve this by installing all the needed packages via R install.packages() rather than conda. Next time I think I will avoid using conda for R packages altogether; this isn't the first time it has caused issues.

Unable to get plots displayed in Rstudio graphical viewer.

When I run my code all my graphs (both ggplot2 and plot) are displayed in an external "Quartz 2 [*]" graphical viewer.
I would like them to be displayed in the R studio GUI plot area to I can better save and view my files, along with the previous versions.
Is there anyway to stop this?
I recently updated my version of R, along with the X11 and xQuartz on my mac (also up to date), and I am guessing these updates are behind it.
I have seen many forums explaining how to get rstudio to export to an external viewer (e.g. "quartz()"), but not the other way.
I have looked, but these threads have not helped:
ggplot plots in scripts do not display in Rstudio
plot panel does not produce plots Rstudio
dev.off() even when hidden hide <- dev.off() is not working either.
Any help would be great,
Thanks.
If you upgrade R without upgrading RStudio, the graphics engine may not be compatible. To fix the "Quartz 2" popout, upgrade RStudio to a newer version that supports the graphics engine in the version of R you have installed.
I had the same problem, and noticed the following output to the console:
Warning message:
R graphics engine version 15 is not supported by this version of RStudio. The Plots tab will be disabled until a newer version of RStudio is installed.
Looks like I forgot to reinstall Rstudio as well.. That sorted my problem.

Resources