Rfanatic
Rfanatic

Reputation: 2290

Customize ggplot2 axis labels with specific colours

I would like to customize the axis labels such that the line color of the plot and the x value have the same color. For example, I would like to color time steps 08:00, 15:00, and 18:30 with "#FF9999" and 07:00,15:30,19:00,21:00 with "0000FF".

enter image description here

Sample data:

df<-structure(list(time = c("04:00", "04:15", "04:30", "04:45", "05:00", 
"05:15", "05:30", "05:45", "06:00", "06:15", "06:30", "06:45", 
"07:00", "07:15", "07:30", "07:45", "08:00", "08:15", "08:30", 
"08:45", "09:00", "09:15", "09:30", "09:45", "10:00", "10:15", 
"10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00", 
"12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", 
"14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", 
"15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", 
"17:30", "17:45", "18:00", "18:15", "18:30", "18:45", "19:00", 
"19:15", "19:30", "19:45", "20:00", "20:15", "20:30", "20:45", 
"21:00", "21:15", "21:30", "21:45", "22:00", "22:15", "22:30", 
"22:45", "23:00", "23:15", "23:30", "23:45", "00:00", "00:15", 
"00:30", "00:45", "01:00", "01:15", "01:30", "01:45", "02:00", 
"02:15", "02:30", "02:45", "03:00", "03:15", "03:30", "03:45"
), index = 1:96, variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "value", class = "factor"), 
    value = c(0, 0.00120336943441637, 0.00401123144805455, 0.00481347773766546, 
    0.0116325711993582, 0.0104292017649418, 0.0236662655435219, 
    0.0284797432811873, 0.0709987966305656, 0.0770156438026474, 
    0.0882470918572002, 0.0918572001604493, 0.201363818692339, 
    0.219013237063779, 0.281989570798235, 0.298435619735259, 
    0.503409546730846, 0.555956678700361, 0.67428800641797, 0.707180104292018, 
    0.8896911351785, 0.922583233052547, 0.935018050541516, 0.936221419975933, 
    0.963096670677898, 0.974729241877256, 0.985960689931809, 
    0.984757320497393, 0.984757320497393, 0.994785399117529, 
    1, 0.997994384275973, 0.919775371038909, 0.902527075812274, 
    0.860409145607702, 0.854793421580425, 0.756117127958283, 
    0.768953068592058, 0.837545126353791, 0.847172081829122, 
    0.944243882872042, 0.963497793822704, 0.965503409546731, 
    0.967107902125953, 0.957079823505816, 0.969915764139591, 
    0.9558764540714, 0.953469715202567, 0.890894504612916, 0.882872041716807, 
    0.8235058162856, 0.805054151624549, 0.661452065784196, 0.631768953068592, 
    0.549538708383474, 0.51544324107501, 0.300842358604091, 0.277978339350181, 
    0.235058162855997, 0.214199759326113, 0.143602085840353, 
    0.139590854392298, 0.129161652627357, 0.127557160048135, 
    0.0946650621740874, 0.0934616927396711, 0.0886482150020056, 
    0.0834336141195347, 0.0709987966305656, 0.0722021660649819, 
    0.0705976734857601, 0.0701965503409547, 0.0605695948656237, 
    0.0605695948656237, 0.0573606097071801, 0.0561572402727637, 
    0.0389089450461292, 0.036101083032491, 0.0344965904532692, 
    0.0348977135980746, 0.0260730044123546, 0.0280786201363819, 
    0.0260730044123546, 0.0260730044123546, 0.0212595266746891, 
    0.0208584035298837, 0.0164460489370237, 0.0156438026474128, 
    0.00922583233052547, 0.00882470918572002, 0.00842358604091456, 
    0.00882470918572002, 0.00240673886883273, 0.00200561572402728, 
    0.00200561572402728, 0.00200561572402728)), class = "data.frame", row.names = c(NA, 
-96L))

Upvotes: 1

Views: 82

Answers (1)

Oliver
Oliver

Reputation: 8602

Adding to my own and Jon's comment you could add something like

f <- function(x){
  d <- rep('black', NROW(x))
  one <- which(x %in% c("08:00", "15:00", "18:30"))
  two <- which(x %in% c("07:00", "15:30", "19:00", "21:00"))
  d[one] <- '#FF9999'
  d[two] <- '#0000FF'
  d
}
colour <- f(df$time)

plot + 
  theme(axis.text.x = element_text(color = colour,
                                   angle = 90))

Note that you will get a warning

Warning message: Vectorized input to element_text() is not officially supported. Results may be unexpected or may change in future versions of ggplot2.

But it does work. It is however not aware of the tick labels themselves. By this I mean it will colour based on position in colour compared to the position on the axis. So You'll need to match the position somehow (I might have "misaligned" the colours in my example)

Upvotes: 2

Related Questions