在ggplot2中更改特定刻度的颜色

时间:2016-01-17 17:42:36

标签: r ggplot2

假设我使用ggplot创建了自定义刻度:

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

如何为上面的标签上色? (只有那一个)

1 个答案:

答案 0 :(得分:8)

您需要theme axis.text.y,并为colour传递一个带有每种标签颜色的矢量。

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')))

呈现

plot with green tick label