Different colors of axis labels in R based on another variable

I usually use ggplot2, but in this case I use the regular image() function to build a heat map of a large dataset. I can mark all the labels as red, but I want to mark the y axis with text of different colors based on the vector of the 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") 

This generates the following image:

Example plot

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 that I get after some "real" data ...

actual heatmap

EDIT:

As a backup, if possible in ggplot2, I might want to change the code coefficient. There are several more applications that I would use for this.

I figured out a way to build a layer of red characters on top of the old shortcuts, but, if possible, would prefer my own method with a color vector ...

 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 labels outside the bounds of space ...

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

UPDATE: Below is a solution that works with ggplot2 ...

+6
source share
4 answers

If you ignore vectorized features like text and mtext , you can get them by typing axis again. Time spent will be minimal, and this will allow all axis calculations to be performed as usual. For instance:.

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

Result:

enter image description here

+6
source

Accepting @thelatemail's answer as the most flexible, but it also turns out to be pretty simple using text() if you add xpd = TRUE to allow out-of-frame building. Using mtext() may also work, but it does not allow labels to be rotated .

 grid = structure(c(1:20),.Dim = c(4,5)) labs = c("A","B","C","D","E") redlabs = c("B","D") colorlist = c("black","red") # one of many ways to generate the color labels axiscolor = colorlist[labs %in% redlabs +1 ] image(1:4,1:5,grid,axes=FALSE, xlab="", ylab = "") axis(2,at=1:length(labs),labels=FALSE) # This would work for sideways labels # mtext(text=labs, side=2,at=1:length(labs),col=axiscolor,adj=.5) text(labels=labs, col=axiscolor, x=rep(.45,length(labs)), y=1:length(labs), srt = 0, pos = 2, xpd = TRUE) 

Solution using text ()

UPDATE for ggplot2 . You can use theme() and element_text to set colors and other parameters. Something like that...

  p + theme(axis.text.y = element_text(color=axiscolor)) 
+3
source

You can specify a vector with the colors that you want to apply to the labels, and then use the loop to get the labels colored with the () axis. In the following example, I use a different color for each level of the scatter chart.

 DF <- data.frame(habitat=c("Hab 1","Hab 2","Hab 3","Hab 4","Hab 5"), mean=c(0.53,0.28,0.30,0.35,0.39), color=colors()[c(24,257,26,504,652)]) > DF habitat mean color 1 Hab 1 0.53 black 2 Hab 2 0.28 green3 3 Hab 3 0.30 blue 4 Hab 4 0.35 orangered1 5 Hab 5 0.39 yellow par(mar=c(7, 5, 4, 3)) dotchart(DF[,2], xlim=c(0.2,0.6), col=as.character(DF$color), pch=16, lcolor="white", xlab=colnames(DF[2])) # Plot the points for (j in 1:5){ axis(side=2, at=j, col.axis=as.character(DF$color)[j], labels=DF$habitat[j], las=1) # Add habitat as labels, each with corresponding color, on the left margin } 
+1
source

I really like thelatemail's approach, and it can only add a little refinement, since the fixed "in" positions (as in the example above with at = 1:3 ) did not work well for me. In my case, I needed to create a barplot and provide my own values ​​for the space and width parameters. In the end, I used (for example, random data in which I wanted the bars and labels for positive (non-negative, more precisely) data values ​​to be green and red otherwise. In this example, I also use letters to create labels and rotate labels using las = 2 ):

 x <- rnorm(26) color <- rep("green", length(x)) color[x < 0] <- "red" par(mar=c(6,4.1,4.1,2.1)) barplot(x, las = 2, ylim = c(min(x)-0.5, max(x)+0.5), col = color, space = 0.5, width = 2) Map(function(x,y,z) axis(1,at=x,col.axis=y,labels=z,lwd=0,las=2), seq(from = 2, by = 3, length.out = length(x)), color, letters ) axis(1,at=seq(from = 2, by = 3, length.out = length(x)),labels=FALSE) 
0
source

Source: https://habr.com/ru/post/953956/


All Articles