Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of specific tick in ggplot2

Tags:

r

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)

like image 323
user191919 Avatar asked Jan 17 '16 17:01

user191919


1 Answers

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

like image 136
alistaire Avatar answered Sep 18 '22 16:09

alistaire