简体   繁体   中英

Different colors for every column in heat map using geom_tile()

I am trying to get a heat map where every column has a different color.

I have a heatmap like this:

# install.packages("reshape")
library(reshape)
library(ggplot2)

# Data
set.seed(8)
m <- matrix(round(rnorm(200), 2), 5, 5)
colnames(m) <- paste("Row", 1:5)
rownames(m) <- paste("col", 1:5)

# long format
df <- melt(m)
colnames(df) <- c("x", "y", "value")

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile()

在此处输入图像描述

I would like to get for each columun col1,col2,col3,col4, and col5 a different color.

For example: For col1 blue, col2 2 green, violet for col3, yellow for col4 and orange in col5.

I need to catch these ideas because I am doing the next plot with the next dataset:

 dput(bdd)
structure(list(var = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L), .Label = c("var_1", "var_2", 
"var_3", "var_4", "var_5", "var_6", "var_7", "var_8", "var_9", 
"var_10", "var_11", "var_12", "var_13", "var_14"), class = "factor"), 
    value = c(4.93, 2.85, 2.075, 1.91, 1.73, 1.34, 0.615, 0.145, 
    0.14, 0.11, 0.09, 0.06, 0.06, 0.015, 4.13, 1.65, 1.985, 0.51, 
    5.805, 0.84, 1.28, 0.03, 0.235, 0.145, 0.145, 0.205, 0.03, 
    0.2, 1.135, 2.175, 2.735, 1.69, 0.86, 0.715, 1.905, 0.17, 
    0.86, 0.055, 0.03, 0.075, 0.14, 0.005, 3.55, 4.225, 5.985, 
    0.185, 1.17, 0.91, 0.49, 1.34, 0.485, 0.1, 0.145, 1.145, 
    0.53, 0.11, 12.06, 1.995, 2.205, 0.48, 1.875, 2.03, 0.335, 
    0.26, 1.25, 0.225, 0.245, 0.52, 0.075, 0.04), country = structure(c(1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 
    2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
    3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 
    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5), .Label = character(0)), 
    country1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L), .Label = c("C1", "C2", "C3", "C4", "C5"), class = "factor")), row.names = c(NA, 
-70L), class = c("tbl_df", "tbl", "data.frame"))



ggplot(data=bdd,aes(x=country1,y=var,fill=value))+
  geom_tile(aes(alpha=value,fill=country),color="white")+
  geom_text(aes(label = sprintf("%0.3f", round(value, digits = 3))))+
  scale_fill_gradient(low="white", high="blue")+
  scale_alpha(range = c(0, 1))+
  theme_classic()+theme(axis.title.x=element_blank(), axis.text.x=element_text(angle=0,hjust=0.5,vjust=0.5), legend.position = "none")+ 
  labs( fill="% ",y = "y ")

在此处输入图像描述

But what I need is every column with a different color as in the first example.

Best.

ggplot(data=bdd,aes(x=country1,y=var,fill=country1))+
  geom_tile(aes(alpha=value),color="white")+
  geom_text(aes(label = sprintf("%0.3f", round(value, digits = 3))))+
  scale_alpha(range = c(0, 1))+
  theme_classic()+theme(axis.title.x=element_blank(), axis.text.x=element_text(angle=0,hjust=0.5,vjust=0.5), legend.position = "none")+ 
  labs( fill="% ",y = "y ")

To specify the colors for each column to be different than the default spectrum, you could use one of the discrete fill options like scale_fill_discrete , scale_fill_manual , or a custom palette like ggthemes::scale_fill_tableau(palette = "Nuriel Stone")

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM