I want to create a nested_facet plot but the labels contain a less or equal sign and a greater or equal sign and spaces. The following is an example:
df <- iris
df$factor <- factor(rep(c('Values 7', 'Values 123', 'Values very-high'), length.out = nrow(df)))
df <- df %>% mutate(factor2 = case_when(factor =='Values 7' ~'Values <= 7',
factor == 'Values 123' ~ 'Values >= 123',
TRUE ~ factor))
ggplot(df, mapping = aes(x = Sepal.Length, y = Sepal.Width)) +
geom_line() +
facet_nested("x" + Species ~ "y-and z" + factor2, labeller = label_parsed)
This code does not work. If I remove all spaces (like the code below) the code works fine, but does not have the correct labels (which are provided in the previous example). I have tried unicodes as well, but they are not saved correctly as a pdf.
df <- iris
df$factor <- factor(rep(c('Values 7', 'Values 123', 'very-high'), length.out = nrow(df)))
df <- df %>% mutate(factor2 = case_when(factor =='Values 7' ~'Values <= 7',
factor == 'Values 123' ~ 'Values >= 123',
TRUE ~ factor))
ggplot(df, mapping = aes(x = Sepal.Length, y = Sepal.Width)) +
geom_line() +
facet_nested("x" + Species ~ "y-and" + factor2, labeller = label_parsed)
How can I get this to work?
You have to use ~
in your ?plotmath
strings instead of spaces:
library(ggplot2)
library(ggh4x)
library(dplyr, warn=FALSE)
df <- iris
# Add a ~ between "Values" and "very"
df$factor <- factor(rep(c("Values 7", "Values 123", "Values~very-high"), length.out = nrow(df)))
df <- df %>% mutate(factor2 = case_when(
factor == "Values 7" ~ "Values <= 7",
factor == "Values 123" ~ "Values >= 123",
TRUE ~ factor
))
ggplot(df, mapping = aes(x = Sepal.Length, y = Sepal.Width)) +
geom_line() +
# Add ~'s in "y and z"
ggh4x::facet_nested("x" + Species ~ "y~and~z" + factor2,
labeller = label_parsed
)