问题
I am trying to make a heatmap table, which is fairly easy, but I am wanting the gradient color to be confined within a single row, not over the entire data.frame. The result should be that each row should have the row max and min both present. Basically all examples of heatmaps I can find online create the gradient color scale from all values in the table.
Here is an example dataframe:
df <- data.frame('var' = paste0('var',1:3),
'group1' = c(1,500,3),
'group2' = c(2,300,1),
'group3' = c(3,100,2),
'group4' = c(4,50,4),
'group5' = c(5,10,5))
And here is the result of what I am looking for:
回答1:
What about scaling your data by row (divide it by the average of the row). Example with ggplot2:
df <- melt(df)
df <- data.table(df)
df[,value_rescaled:=value/mean(value),by=.(var)]
ggplot(data = df, aes(x = variable, y = var)) +
geom_tile(aes(fill = value_rescaled))+
scale_fill_gradient2(low = "#4682B4", mid = "#FFFFFF", high = "#FF0000", midpoint = 1, space = "Lab",
na.value = "grey50", guide = "colourbar")+
geom_text(aes(label=value))+
theme(legend.position="none")
来源:https://stackoverflow.com/questions/49947405/ggplot2-heatmap-table-by-row