基于另一个变量在R中改变轴标签的颜色 [英] Vary colors of axis labels in R based on another variable

查看:270
本文介绍了基于另一个变量在R中改变轴标签的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用ggplot2,但在这种情况下,我使用常规的 image()函数绘制大型数据集的热图。我可以将所有标签标记为红色,但我想根据我生成的颜色定义向量为不同颜色的文本标注y轴:

  grid = structure(c(1:12),。Dim = c(4,3))
labs = c(A,B ,C)
image(1:4,1:3,grid,axes = FALSE,xlab =,ylab =)
#这个作品,但不是颜色I想要
轴(2,at = 1:长度(实验室),labels = labs,las = 2,adj = 1,cex.axis = 0.6,col.axis =red)

生成以下图片:



我想要标签A和C是黑色的,而B是红色。这是我的尝试,但它给出了一个错误的长度的错误... ...

$ $ p $ code $ axiscolors = c(black, red,black)
axis(2,at = 1:length(labs),labels = labs,las = 2,adj = 1,cex.axis = 0.6,col.axis = axiscolors)

这是我对一些真实数据的影响......





<作为备份,如果在ggplot2中这是可能的,我可能会重新考虑我的代码。还有一些其他应用程序我也会使用它。



我想出了一种在旧标签的顶部绘制一层红色符号的方法,但如果可能的话,会更喜欢使用颜色矢量的本地方法。 。
$ b $ pre $ code $ sublabs = c(B)
axis(2,at = match(sublabs,labs), labels = sublabs,las = 2,adj = 1,cex.axis = 0.6,col.axis =red)

另一种方法是使用 text(),如果我可以将标签放在绘图空间之外......


$ b (c(1,1,1),c(1,2,3),labs,col = c(黑色,红色,黑色 ))

UPDATE:请参阅下面的解决方案, code> ggplot2 ...

解决方案

c $ c> text 和 mtext ,您可以通过反复调用 axis 来达到目的。时间开销将非常小,并且它将允许所有的计算按照通常的方式进行。例如:

 #原始代码
grid =结构(c(1:12),。Dim = c(4 ,3))
labs = c(A,B,C)
image(1:4,1:3,grid,axes = FALSE,xlab =,ylab =)
axiscolors = c(black,red,black)

#new code
Map(函数(x,y,z)
轴(2,at = x,col.axis = y,labels = z,lwd = 0,las = 1),
1:3,
轴颜色,
实验室

轴(2,at = 1:3,labels = FALSE)

导致:




I usually use ggplot2, but in this case I am using the regular image() function to plot a heatmap of a large data set. I can label all the labels as red, but I want to label the y-axis with text of different colors based on a vector of color definitions that I generate:

grid = structure(c(1:12),.Dim = c(4,3))
labs = c("A","B","C")
image(1:4,1:3,grid,axes=FALSE, xlab="", ylab = "")
#This works but isn't the colors I want
axis(2,at=1:length(labs),labels=labs,las=2, adj=1,cex.axis=0.6,col.axis="red")

That generates the following image:

I would like labels A and C to be black and B to be red. This is what I tried, but it gives a "wrong length" error...

axiscolors = c("black","red","black")
axis(2,at=1:length(labs),labels=labs,las=2, adj=1, cex.axis=0.6, col.axis=axiscolors)

This is the effect I am after with some "real" data...

EDIT:

As a back-up, if this is possible in ggplot2, I might be willing to re-factor my code. There are a couple other applications I would use this for as well.

I figured out a way to plot a layer of red symbols over the top of the old labels, but would prefer a native method with the color vector, if possible...

sublabs = c("B")
axis(2,at=match(sublabs,labs),labels=sublabs,las=2, adj=1, cex.axis=0.6, col.axis="red")

Another way would be to use text() if I could put the labels outside the plot space...

text(c(1,1,1),c(1,2,3),labs,col=c("black","red","black"))

UPDATE: See below for a solution that works with ggplot2...

解决方案

If you ignore the vectorised possibilities like text and mtext, you can get there by repeatedly calling axis. The overhead timewise will be very minimal and it will allow all the axis calculations to occur as they normally do. E.g.:

# original code
grid = structure(c(1:12),.Dim = c(4,3))
labs = c("A","B","C")
image(1:4,1:3,grid,axes=FALSE, xlab="", ylab = "")
axiscolors = c("black","red","black")

# new code    
Map(function(x,y,z) 
  axis(2,at=x,col.axis=y,labels=z,lwd=0,las=1),
  1:3,
  axiscolors,
  labs
)
axis(2,at=1:3,labels=FALSE)

Resulting in:

这篇关于基于另一个变量在R中改变轴标签的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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