I would like to conditionally alter the color/face/etc of a continuous tick mark label using logic instead of hard coding. For example:
library(tidyverse)
library(viridis)
xx=rpois(1000,lambda = 40)
y=density(xx,n=3600,from=0)
ggplot(data.frame(x = y$x, y = y$y), aes(x, y)) +
geom_line() +
geom_segment(aes(xend = x, yend = 0, colour = y)) +
scale_color_viridis() +
labs(y='Density',x='Count',colour='Density')+
geom_vline(xintercept=40,color='red') +
scale_x_continuous(breaks=c(0,40,seq(25,100,25)),limits=c(0,100))+
theme(axis.text.x = element_text(face=c('plain','bold',rep('plain',4)),
color=ifelse(y$x==60,'red','black')))
So in my example above, hard coding is seen in the face function and that works (I can do the same thing for color). It's not a big deal here since I only have a few breaks. In future scenarios, though, I may have significantly more breaks or a less simple coloring scheme needed for my axes. Is there a more efficient solution to create this labeling based on conditional logic? My first attempt is seen in color function but that does not work . The issue is identifying the object to use in the logical statement. Maybe there is a behind the scenes variable I can call for the breaks (similar to using ..level.. for a density plot). If that's the case, bonus points if you can teach me how to find it/figure that out on my own
..level..are generally only available insideaes().theme. But if you already have specified abreaksvector, just use it to create also the face vector in advance. See also: Change color of specific tick in ggplot2breaksvector before the plot. I was then able to use theifelselogic within theme... i.e.ifelse(breaks==40,'red','black')I'm not sure it's any better than defining the color (or face) vector before the plot like the breaks vector but I thought it was interesting to see it work.