更改ggplot2中特定勾号的颜色 [英] Change color of specific tick in ggplot2

查看:323
本文介绍了更改ggplot2中特定勾号的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



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

解决方案

您需要主题 axis.text.y ,并为每个标签传递 color 一个带有颜色的向量。

  ggplot(data = data.frame())+ 
scale_y_continuous(breaks = c(ticksY $ t),limits = c(0,1) ,
labels = expression(0,0.25,'Color 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(color = c('black','black','green','black ','black','black')))

呈现




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

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

解决方案

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

这篇关于更改ggplot2中特定勾号的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆