user191919
user191919

Reputation: 754

Change color of specific tick in ggplot2

Say I created custom ticks with ggplot using:

library(ggplot2)

ticksX <- data.frame (t = c(0,0.25,0.5,0.75,1))
ticksY <- data.frame (t = c(0,0.25,0.3868,0.5,0.75,1))

ggplot(data=data.frame()) +
scale_y_continuous(breaks=c(ticksY$t),limits=c(0,1), 
                            labels=expression(0,0.25,'Colour this one.'
                                              ,0.5,0.75,1)) +
scale_x_continuous(breaks=c(ticksX$t),limits=c(0,1),
                     labels=expression(0,0.25,0.5,0.75,1))

enter image description here

How can I colour the label above? (and only that one)

Upvotes: 7

Views: 3767

Answers (1)

alistaire
alistaire

Reputation: 43334

You need to theme axis.text.y, and pass colour a vector with a color for each label.

ggplot(data=data.frame()) +
  scale_y_continuous(breaks=c(ticksY$t),limits=c(0,1), 
                     labels=expression(0,0.25,'Colour this one.',0.5,0.75,1)) +
  scale_x_continuous(breaks=c(ticksX$t),limits=c(0,1), labels=expression(0,0.25,0.5,0.75,1)) +
  theme(axis.text.y = element_text(colour = c('black', 'black','green', 'black', 'black', 'black')))

renders

plot with green tick label

Upvotes: 13

Related Questions