【问题标题】:Conditional change of axis.text font family and font size creates unwanted 'gap'axis.text 字体系列和字体大小的有条件更改会产生不需要的“间隙”
【发布时间】:2020-01-15 18:29:51
【问题描述】:

我需要根据条件更改条形图的特定 x 轴元素的系列和大小。

我可以使用以下方法成功修改面部:

library(ggplot2)
ggplot(iris, aes(Species, Petal.Length)) + 
  geom_boxplot() + 
  coord_flip() +
  theme(axis.text.y = element_text(face = ifelse(levels(iris$Species)=="setosa","bold","italic")))

建议:https://stackoverflow.com/a/20609878/8534926

但是,由于某种原因,当我尝试应用 family 和 size 时,会在轴和名称之间创建一个空白。

ggplot(iris, aes(Species, Petal.Length)) + 
  geom_boxplot() + coord_flip() + 
  theme(axis.text.y = element_text(family = ifelse(levels(iris$Species)=="setosa","sans","mono")))

或

ggplot(iris, aes(Species, Petal.Length)) + 
  geom_boxplot() + coord_flip() + 
  theme(axis.text.y = element_text(size = ifelse(levels(iris$Species)=="setosa", 10, 20)))

我尝试使用 ma​​rgin 对其进行编辑,但是在修改名称时可能会出现覆盖(例如,使用闪亮的应用程序)。

这个差距是什么?我可以删除吗?

【问题讨论】:

  • 有趣。可能以某种方式相关:stackoverflow.com/questions/53159879/….
  • 我认为这是一个错误,我有raised a Github issue。我很好奇他们的回答。我通常认为“少即是多”,您可能需要考虑一种不同的方式来突出显示数据的特定子集。例如,如果数据点得到不同的形状/灰度值/颜色(如果真的需要颜色),那么应该不需要更改一个 x 类别的字体或大小...
  • @Tjebo 感谢您的评论。我使用颜色作为突出显示,但我想使用所有可能的突出显示,我需要使用特定的字体。

标签: r ggplot2 axis-labels


【解决方案1】:

该问题在即将发布的 ggplot2 3.3.0 中得到解决,但现在会触发警告,因为这种格式化轴文本的方法不可靠,并且可能在将来的任何时候停止工作。

library(ggplot2) # v 3.3.0 or higher

# discouraged, triggers warning message
ggplot(iris, aes(Species, Petal.Length)) + 
  geom_boxplot() + coord_flip() + 
  theme(
    axis.text.y = element_text(
      size = ifelse(levels(iris$Species)=="setosa", 10, 20)
    )
  )
#> Warning: Vectorized input to `element_text()` is not officially supported.
#> Results may be unexpected or may change in future versions of ggplot2.

作为替代方案,正在开发的 ggtext 包试图通过将格式化指令编码到文本标签中来提供解决此问题的基本方法。

library(ggtext) # remotes::install_github("clauswilke/ggtext")
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(glue)
#> 
#> Attaching package: 'glue'
#> The following object is masked from 'package:dplyr':
#> 
#>     collapse

iris %>%
  mutate(
    Species = ifelse(
      Species == "setosa",
      "<span style = 'font-size:10pt'>setosa</span>",
      glue("<span style = 'font-size:20pt'>{Species}</span>")
    )
  ) %>%
  ggplot(aes(Species, Petal.Length)) + 
  geom_boxplot() + coord_flip() + 
  theme(axis.text.y = element_markdown())

由reprex package (v0.3.0) 于 2020-01-16 创建

【讨论】:

  • 谢谢。我正在尝试测试您的代码(ggtext),但出现以下错误:Error in (angle %||% 0)%%360 : non-numeric argument to binary operator
  • 请提交问题here 并确保提供可重现的示例。
【解决方案2】:

我不知道为什么会这样。很奇特。但是您可以使用margin 更改此边距。
但是,您需要指定单位,例如作为“英寸”。找到正确的值是一个反复试验的问题,这也很大程度上取决于您的最终绘图输出。
也许找到不同的方式来突出显示您的数据会更安全、更容易?

library(ggplot2)

ggplot(iris, aes(Species, Petal.Length)) + 
  geom_boxplot() + 
  theme(axis.text.y = element_text(family =  
          ifelse(levels(iris$Species)=="setosa","sans","mono"),
        margin = margin(r = -2, l = 0.8, unit = 'in'))) +
  coord_flip()

由reprex package (v0.3.0) 于 2020-01-15 创建

【讨论】:

  • 负边距正在撤消 ggplot2 中错误的影响。这个例子将在几周后停止工作,当下一个 ggplot2 发布时。
猜你喜欢
  • 2011-05-26
  • 2015-04-08
  • 1970-01-01
  • 2012-12-19
  • 1970-01-01
  • 2011-12-01
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多