ggplot2 - Heatmap Table by Row

≡放荡痞女 提交于 2019-12-23 05:18:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!