ggplot2问题具有默认字体大小(base_family =“") [英] ggplot2 issue with default font size (base_family = "")

查看:128
本文介绍了ggplot2问题具有默认字体大小(base_family =“")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以在

我可以通过将主题base_family设置为"Arial"来手动解决该问题:

 #个库库(ggplot2)#创建数据set.seed(42)x值<-1:10yValue<-cumsum(rnorm(10))数据<-data.frame(xValue,yValue)# 阴谋ggplot(数据,aes(x = xValue,y = yValue))+geom_line()+theme_classic(base_family ="Arial") 

这是已解决问题的图像,仅当base_family设置为特定字体(例如"Arial")时才有效:

问题是,为什么我的系统与默认字体有些冲突,如何将默认字体恢复为正常?因为现在我必须在用ggplot2进行的每个绘图中都调用 base_family ="Arial" 参数.我应该提到的是,例如,我没有字体问题无论如何.除了如何更改特定主题类型的默认设置的链接以外,我没有发现任何类似的问题,但是我想将设置重置为正常设置,因此 base_family =" 可以再次使用.希望您能为我提供帮助,如果您需要我的更多信息,请不要犹豫.

R版本:3.6.1(2019-07-05)

平台:linux mint x86_64

conda环境

解决方案

感谢@tjebo,我可以通过将R版本升级到4.0.3来解决此问题.并据我所知切换到了ipython笔记本,因为据我所知R-Studio不支持4.0.3版(可能只是由conda引起的问题,而不是由R-Studio引起的).

我与conda合作,因此创建了一个新环境

 #创建并激活conda环境conda创建--name r4康达激活r4 

然后我安装了R Verison 4:

 #安装R版本4conda安装-c conda-forge r-baseconda安装-c conda-forge/label/gcc7 r-base 

最后,我安装了 irkernel 以便在IPython笔记本中使用R:

 #为IPython Notebook安装R内核conda install -c r r-irkernelIRkernel :: installspec()#打开jupyter笔记本ipython笔记本 

在ipythone笔记本中,我现在可以选择"R"作为内核,我以前遇到的代码无法按预期工作.再次归功于@tjebo.

重新安装R会导致进一步的问题,其中由于缺少lib文件而无法正确安装软件包.但这是题外话,所以我可能会在其他地方打开另一个讨论.(仅供参考,此链接由@fredaas 此处)发布.)

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)

这篇关于ggplot2问题具有默认字体大小(base_family =“")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆