I'm trying to make axis labels bold if they contain specific text. I'm trying to do this to a plot that's the output of a function that also manipulates the my data, so there's no longer a direct relationship between my input data and the order of the labels in the output. So adding theme(axis.text.y = element_text(face = ifelse(levels(df$category) == "xxx", "bold", "plain")))
doesn't work (it turns the wrong label bold).
As a concrete example, if I had just this box plot of petal length by iris species as an object, would it be possible add a layer that would turn "setosa" bold if I didn't know the order the species would appear in?
Update: a reproducible example:
library(tidyverse)
library(rlang)
test <- tribble(
~AreaName, ~Value,
"London", 1,
"New York", 5,
"Paris", 3
)
compare_areas <- function(data, area, value) {
area <- enquo(area)
value <- enquo(value)
newLevels <- data[order(data[[quo_text(value)]], decreasing = TRUE), ][[quo_text(area)]]
print(newLevels)
data[[quo_text(area)]] <- factor(data[[quo_text(area)]], levels = newLevels)
print(data)
p <- ggplot(data, aes_string(x = quo_text(area),
y = quo_text(value))) +
coord_flip() +
geom_col()
return(p)
}
compare_areas(data = test,
area = AreaName,
value = Value)
Plot output. So I'd want to make Paris bold, for example, but I don't know how the levels will be ordered.