【问题标题】:R plotmath expression to show range of values in ggplotR plotmath 表达式以显示 ggplot 中的值范围
【发布时间】:2021-05-06 20:14:35
【问题描述】:

我正在努力表达以传达 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, 10, 120)) %>% 
  ggplot(aes(x=mass, y=height)) +
  geom_point() +
  labs(title=expression(10<="Birth Year"))

【问题讨论】:

  • 抱歉应该是expression(10&lt;="Birth Year"&lt;120)

标签: r ggplot2 dplyr plotmath


【解决方案1】:

一个可能的解决方案是:

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

【讨论】:

  • 谢谢莱昂纳多。但是,我确实注意到,对于此解决方案,引号中包含的“
  • 我改变了答案。现在它应该在任何情况下都可以工作
  • 感谢莱昂纳多,这很棒。我选择这个星球大战的东西作为一个简单的例子。在实践中,我正在制作一个图表,显示不同工资率箱中的计数(一条线代表 8
  • 没有看到数据很难给你答案。但请记住,您可以将变量传递给表达式,因此您至少可以自动化一点。如果答案对你有用,如果你愿意接受它
【解决方案2】:

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

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

【讨论】:

  • 抱歉应该是表达式(10
  • 好的。已修改。
猜你喜欢
  • 2018-05-24
  • 2021-02-20
  • 1970-01-01
  • 2016-05-09
  • 2013-03-14
  • 2016-10-12
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
相关资源
最近更新 更多