how do I fix plot problem with R in Jupyter notebook? [duplicate] - 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

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.

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

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)

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

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