R plotmath 表达式显示 ggplot 中的值范围

R plotmath expression to show range of values in ggplot

我正在努力表达 10<=VarName<120 之类的内容。为什么以下代码会失败?

starwars %>% 
  filter(between(birth_year, 10, 120)) %>% 
  ggplot(aes(x=mass, y=height)) +
  geom_point() +
  labs(title=expression(10<="Birth Year"120))

请注意,仅此一项就有效(末尾没有 120):

starwars %>% 
  filter(between(birth_year, 10, 120)) %>% 
  ggplot(aes(x=mass, y=height)) +
  geom_point() +
  labs(title=expression(10<="Birth Year"))

一个可能的解决方案是:

starwars %>% 
  filter(between(birth_year, 10, 120)) %>% 
  ggplot(aes(x=mass, y=height)) +
  geom_point() +
  labs(title=expression(paste(10<="Birth Year<",120, sep = "")))

另一个解决方案(更复杂,但更好)

starwars %>% 
  filter(between(birth_year, 10, 120)) %>% 
  ggplot(aes(x=mass, y=height)) +
  geom_point() +
  labs(title = parse(text = paste0('"10" <= ', ' ~ "Brith Year" <= ', '~ 120')))

在评论中添加了数字经常更改的内容,因此已修改为使用 bquote:

lo <- 10
hi <- 120
ggplot() + labs(title = bquote(.(lo) <= Birth ~ Year < .(hi)))