使用大于等于符号时facet_wrap解析错误

问题描述 投票:0回答:1

我正在尝试更改使用

facet_wrap()
绘制的两个图的标题,其中一个具有大于或等于符号 (
>=
)。我可以获取图表来解析标题,但是一旦我将 >= 符号放在括号中,标题就无法解析。当 >= 符号位于括号中时,如何获取要解析的标题?

library(tidyverse)

mtcars %>% 
  mutate(size = ifelse(hp >= 100, 'greater', 'lesser'),
         size = factor(size, levels = c("greater", "lesser"),
                       labels = c("Horsepower >= 100", "Horsepower < 100"))) %>% 
  ggplot(aes(x = mpg, y = wt)) +
  geom_point() +
  theme_bw() +
  facet_wrap(~size, labeller = label_parsed)

将 >= 放在括号中时无法解析。


mtcars %>% 
  mutate(size = ifelse(hp >= 100, 'greater', 'lesser'),
         size = factor(size, levels = c("greater", "lesser"),
                       labels = c("Horsepower (>= 100)", "Horsepower (< 100)"))) %>% 
  ggplot(aes(x = mpg, y = wt)) +
  geom_point() +
  theme_bw() +
  facet_wrap(~size, labeller = label_parsed)
#> Error in parse(text = as.character(values)): <text>:1:13: unexpected '>='
#> 1: Horsepower (>=
#>                 ^

创建于 2024 年 11 月 18 日,使用 reprex v2.1.1

r ggplot2
1个回答
0
投票

下面我们修改了

labels=...

mtcars %>% 
  mutate(size = ifelse(hp >= 100, 'greater', 'lesser'),
         size = factor(size, levels = c("greater", "lesser"),
                       labels = c("Horsepower * ' (>=' * 100 * ')'", 
                                  "Horsepower * ' (<' * 100 * ')'"))) %>% 
  ggplot(aes(x = mpg, y = wt)) +
  geom_point() +
  theme_bw() +
  facet_wrap(~size, labeller = label_parsed)
© www.soinside.com 2019 - 2024. All rights reserved.