【问题标题】:reduce a string in r减少r中的字符串
【发布时间】:2021-11-24 07:01:44
【问题描述】:

我想将 ggplot 创建的轴的字符串大小(即yx)缩小为一个字符,如下图所示

请大家给我一些建议好吗?

【问题讨论】:

标签: string ggplot2


【解决方案1】:

一种可能的解决方案是使用expression(),例如

library(tidyverse)
library(palmerpenguins)

penguins %>%
  na.omit() %>%
  ggplot(., aes(x = bill_length_mm, y = body_mass_g)) +
  geom_point() +
  theme_classic(base_size = 16) +
  ylab(expression(P[y])) +
  xlab(expression(P[x])) +
  theme(axis.title = element_text(hjust = 1),
        axis.line = element_line(arrow = arrow(type='closed', length = unit(12,'pt'))))

reprex package (v2.0.1) 于 2021 年 11 月 24 日创建

另一个可能的解决方案是使用 unicode 符号(例如来自https://unicode-table.com/en/sets/superscript-and-subscript-letters/):

penguins %>%
  na.omit() %>%
  ggplot(., aes(x = bill_length_mm, y = body_mass_g)) +
  geom_point() +
  theme_classic(base_size = 16) +
  xlab(paste("P", "\u1D6A", sep = "")) +
  ylab(paste("P", "\u1D67", sep = "")) +
  theme(axis.title = element_text(hjust = 1),
        axis.line = element_line(arrow = arrow(type='closed', length = unit(12,'pt'))))

reprex package (v2.0.1) 于 2021 年 11 月 24 日创建

【讨论】:

  • 非常感谢。到目前为止,我使用了bquote,给出了类似的结果。
猜你喜欢
  • 2017-02-14
  • 2015-02-10
  • 2014-11-15
  • 1970-01-01
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
  • 2021-03-05
  • 2022-06-29
相关资源
最近更新 更多