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
...
See Question&Answers more detail:
os