微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决R plotmath 表达式以显示 ggplot 中的值范围

我正在努力获得表达来传达诸如 10

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,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,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)))

screenshot

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。