Change color of x label in a panel under facet_grid?

I need to change the color of U treatment only to red, but I end up having the following figure. I attempted to change the color of the first x label using axis.text.x = element_text(color = c("red", "black", "black")) but it seems that every first element in the panel will also change color to red. I only want the change of color happen in the first panel.

Here's the code for the figure:

x = ggplot(stmraw.csv, aes(x, y, fill=group)) +
      theme_classic() + labs(x = 'Labels', y = 'Index') +
      geom_boxplot() + facet_rep_grid(.~group, scales = 'free_x', ) +
      scale_fill_manual(values=color_scheme) +
            strip.background = element_blank(),
            strip.text.x = element_text(size=12),
            axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid'),
            axis.text.x = element_text(color = c("red", "black", "black")) ## Attempt to change the color of the first  x label
      ) +
      scale_y_continuous(breaks = c(0, 0.25, 0.50, 0.75, 1))

Thank you!

Check out this response to a similar question on StackOverflow, which uses the ggtext package:

I don't know what your data look like, but I imagine you could use a mutate() that defines the span color conditioned on the value of x (e.g. x == 'U'), and accomplish what the OP in that StackOverflow thread was trying to do based on a value of y.

I think that using the answer that suggests a vector of values to axis.text.x (as you've done above), will result in what you've already seen (the application of that vector across the various facets).

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.