英文:
ggplot2 geom_text font size on linux
问题
I recently switched a workstation from Windows to Linux, and for interactive use, the font sizes chosen by geom_text
are dramatically different. I'm trying to find a way to set the base size for geom_text
so that the use of geom_text(aes(size=..))
can be used interchangeably between Windows and Linux with similar-enough results.
Notably, without changing the actual code that produces the plot.
英文:
I recently switched a workstation from windows to linux, and for interactive use, the font sizes as chosen by geom_text
are dramatically different. I'm trying to find a way to set the base size for geom_text
so that the use of geom_text(aes(size=..))
can be used interchangeably between windows and linux with similar-enough results.
Notably, without changing the actual code that produces the plot.
Code:
R.version[c("system","version.string")]
# _
# system x86_64, mingw32
# version.string R version 4.2.3 (2023-03-15 ucrt)
plot(mpg ~ disp, mtcars, type="n")
with(mtcars, text(disp, mpg, cyl))
library(ggplot2)
packageVersion("ggplot2")
# [1] ‘3.4.2’
ggplot(mtcars, aes(disp, mpg, label = cyl)) + geom_text()
The font size for base text
and ggplot2 geom_text
are approximately the same.
Same code on Ubuntu 22.04:
R.version[c("system","version.string")]
# _
# system x86_64, linux-gnu
# version.string R version 4.2.3 (2023-03-15)
plot(mpg ~ disp, mtcars, type="n")
with(mtcars, text(disp, mpg, cyl))
library(ggplot2)
packageVersion("ggplot2")
# [1] ‘3.4.2’
ggplot(mtcars, aes(disp, mpg, label = cyl)) + geom_text()
The text sizes are markedly different.
Since there is much legacy code that needs to run similarly (I recognize some differences will sneak through), I would like to be able to set something (perhaps in .Rprofile
) that will render similar font sizes for both geom_text
and axis labels and titles, etc.
- Something like
theme_set(theme_gray(base_size=20))
does not affectgeom_text
sizes. - This is not a symptom of "high DPI monitor", this has been tested on both high and low DPI displays (on the same linux system).
- Running
extrafont::loadfonts()
(with fully-imported fonts) before loading ggplot2 does not fix it.
https://stackoverflow.com/questions/52358461/is-it-possible-to-set-geom-text-size-via-theme
is very very similar: to be clear, if there is a way to set it to a known/common size basis, regardless of the ability to set it to arbitrary values, I'm good with that.
答案1
得分: 1
以下是翻译好的内容:
伟大的问题。我能想到的最简单解决方案有点巧妙,但有效,只需在加载ggplot2
后添加一行代码即可。它是覆盖ggplot::.pt
,这是用于将ggplot图层中的size
参数转换为字体大小的乘数。
library(ggplot2)
.pt
#> [1] 2.845276
ggplot(mtcars, aes(disp, mpg, label = cyl)) +
geom_text()
如果我们像这样覆盖.pt
:
assignInNamespace(".pt", 5, ns = "ggplot2")
然后我们可以看到字体大小适当增加。
ggplot(mtcars, aes(disp, mpg, label = cyl)) +
geom_text()
请注意,主题元素保持不变,因为它们不使用.pt
。
您需要调整该值以获得使您的两个系统匹配的适当乘数。
<sup>创建于2023-05-10,使用reprex v2.0.2</sup>
英文:
Great question. The easiest solution I can think of is a bit hacky, but effective, requiring only a single line of code to be added to a script after ggplot2
is loaded. It is to overwrite ggplot::.pt
, which is the multiplier used to convert the size
parameter in ggplot layers to font sizes.
library(ggplot2)
.pt
#> [1] 2.845276
ggplot(mtcars, aes(disp, mpg, label = cyl)) +
geom_text()
If we overwrite .pt
like this:
assignInNamespace(".pt", 5, ns = "ggplot2")
Then we can see the font size increases appropriately.
ggplot(mtcars, aes(disp, mpg, label = cyl)) +
geom_text()
Notice that the theme elements are left alone since these do not use .pt
.
You will need to tinker with the value to get the appropriate multiplier that makes your two systems match.
<sup>Created on 2023-05-10 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论