Ggplot2 heatmap, color scheme with conditions

I need to change the colors of the ggplot scale in R. My table:

tt<-data.frame(C1=c(0.4,.5,.5, 0, .8,.8),C2=c(.5,.6,.7, 0, .7,.8), C3=c(.8,.7,.9, 0, .8,.7), 
  C4=c(rep(0,6)), C5=c(0.4,.6,.6, 0, .8,.8),C6=c(0.8,.7,.5, 0, .8,.8), C7=c(0.8,.6,.4, 0, .8,.8))

row.names(tt)<-paste("F", 1:6, sep='')
tt<-as.matrix(tt)

      

Then I change the shape:

library(ggplot2)
library(reshape2)
tt_melt <- melt(tt)
tt_melt
colnames(tt_melt)<-c('fila', 'columna', 'performance')

      

And my graph represents the call center and the metric associated with each position:

ggplot(data=tt_melt,
       aes(x=columna, y=fila, fill=performance)) + geom_tile() + 
  geom_text(aes(label=performance), color='white') 
+ theme_minimal(base_size = 12, base_family = "")+
labs(title = 'Performance por posicion en el call')+
  scale_colour_manual(values = c("red","yellow", "green"))
scale_fill_gradient(low = "yellow",  high = "darkgreen")

      

enter image description here

But he does not accept the scale. I need to get 0s in white (because that means there is no one in that space) and the rest are from red to green. Thus, it is not a truly continuous scale (but should be continuous from red to green). How can I get ggplot to take the scale? Also, how can I create a scale that has a condition over the values? Finally, if there is a better way to do this, I'd love to hear about it. Many thanks.

+3


source to share


2 answers


I hope the code formatting is a SO paste artifact. I prefer the more structured ggplot builds.

You can have a lot more control if you create discrete breaks:

library(ggplot2)
library(reshape2) 

tt <- data.frame(C1=c(0.4,.5,.5, 0, .8,.8),
                 C2=c(.5,.6,.7, 0, .7,.8), 
                 C3=c(.8,.7,.9, 0, .8,.7), 
                 C4=c(rep(0,6)), 
                 C5=c(0.4,.6,.6, 0, .8,.8),
                 C6=c(0.8,.7,.5, 0, .8,.8), 
                 C7=c(0.8,.6,.4, 0, .8,.8))

row.names(tt) <- paste("F", 1:6, sep='')
tt <- as.matrix(tt)

tt_melt <- melt(tt) 
colnames(tt_melt) <- c('fila', 'columna', 'performance')

tt_melt$cut <- cut(tt_melt$performance, 
                   breaks=c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0),
                   labels=as.character(c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9)),
                   include.lowest=TRUE)

perf_cols <- c("white", colorRampPalette(c("red", "green"))( 9 ))
perf_text_cols <- c("black", rep("white", 9))

gg <- ggplot(data=tt_melt, aes(x=columna, y=fila, fill=cut))
gg <- gg + geom_tile()
gg <- gg + geom_text(aes(label=performance, color=cut)) 
gg <- gg + labs(title = 'Performance por posicion en el call')
gg <- gg + coord_equal()
gg <- gg + scale_colour_manual(values = perf_text_cols)
gg <- gg + scale_fill_manual(values=perf_cols)
gg <- gg + theme_minimal(base_size = 12, base_family = "")
gg <- gg + theme(legend.position="none")
gg

      

enter image description here



You have to pounce on the Color Brewer and change the ramp. This method also allows you to ensure that the text appears on white tiles. It also coord_equal

saves you the hassle of playing with height / width to get even blocks. You can also add a border to snippets by adding the color

(non aes

) parameter to geom_tile

.

To preserve the 0

white values (per your comment), use the color not aes

as you originally did:

gg <- ggplot(data=tt_melt, aes(x=columna, y=fila, fill=cut))
gg <- gg + geom_tile()
gg <- gg + geom_text(aes(label=performance), color="white") 
gg <- gg + labs(title = 'Performance por posicion en el call')
gg <- gg + coord_equal()
gg <- gg + scale_fill_manual(values=perf_cols)
gg <- gg + theme_minimal(base_size = 12, base_family = "")
gg <- gg + theme(legend.position="none")
gg

      

enter image description here

+4


source


Try:

ggplot(data=tt_melt, aes(x=columna, y=fila, fill=performance))+
    geom_tile()+ 
    geom_text(aes(label=performance), color='white') +
    theme_minimal(base_size = 12, base_family = "")+
    labs(title = 'Performance por posicion en el call')+
    scale_colour_manual(values = c("red","yellow", "green"))+
    scale_fill_gradient(low = "yellow",  high = "darkgreen")

      



enter image description here

There were only errors in your code. There must be a "+" sign at the end of the line if you want to continue the next line in the ggplot code.

+1


source







All Articles