简体   繁体   中英

r - column wise heatmap using ggplot2

I would really appreciate if anyone could guide me with the following challenge.

I am trying to build column wise heatmap. For each column, I want the lowest value to be green and highest value to be red. The current solution takes a matrix wide approach.

I saw the solution on Heat map per column with ggplot2 . As you can see, I implemented the same code but I am not getting the desired result [picture below]

在此处输入图片说明

df <- data.frame(
                     F1 = c(0.66610194649319, 0.666123551800434,
                            0.666100611954119, 0.665991102703081,
                            0.665979885730484),
            acc_of_pred = c(0.499541627510021, 0.49960260221954,
                            0.499646067768102, 0.499447308828986,
                            0.499379552967265),
   expected_mean_return = c(2.59756065316356e-07, 2.59799087404167e-07,
                            2.86466725381146e-07, 2.37977452007967e-07,
                            2.94242908573705e-07),
         win_loss_ratio = c(0.998168189343307, 0.998411671274781,
                            0.998585272507726, 0.997791676357902,
                            0.997521287688458),
           corr_pearson = c(0.00161443345430616, -0.00248811119331013,
                            -0.00203407575954095, -0.00496817102369628,
                            -0.000140531627184482),
          corr_spearman = c(0.00214838517340878, -0.000308343671725617,
                            0.00228492127281917, -0.000359577740835049,
                            0.000608090759428587),
                roc_vec = c(0.517972308828151, 0.51743161463546,
                            0.518033230192484, 0.518033294993802,
                            0.517931553535524)

)

combo <- data.frame(combo = c("baseline_120", "baseline_20",
                    "baseline_60", "baseline_288",
                    "baseline_5760"))

df.scaled <- scale(df)
df.scaled <- cbind(df.scaled,combo)

df.melt <- melt(df.scaled, id.vars = "combo")

ggplot(df.melt, aes(combo, variable)) + 
  geom_tile(aes(fill = value), colour = "white") + 
  scale_fill_gradient(low = "green", high = "red") +
  geom_text(aes(label=value)) + 
  theme_grey(base_size = 9) + 
  labs(x = "", y = "") + scale_x_discrete(expand = c(0, 0)) + 
  scale_y_discrete(expand = c(0, 0)) + 
  theme(legend.position = "none", axis.ticks = element_blank(), 
        axis.text.x = element_text(size = 9 * 0.8, 
                                   angle = 0, hjust = 0, colour = "grey50"))

You are nearly correct. The code you implemented is the same for plotting. But the person who asked the question did one step in data preparation, he added a scaling variable.

If you scale your variable before plotting it and using the scaled factor as fill argument it works (i just added the rescale in scale_fill_gradient in ggplot after calculating it):

df.melt <- melt(df.scaled, id.vars = "combo")
df.melt<- ddply(df.melt, .(combo), transform, rescale = rescale(value))

ggplot(df.melt, aes(combo, variable)) + 
  geom_tile(aes(fill = rescale), colour = "white") + 
  scale_fill_gradient( low= "green", high = "red") +
  geom_text(aes(label=round(value,4))) + 
  theme_grey(base_size = 9) + 
  labs(x = "", y = "") + scale_x_discrete(expand = c(0, 0)) + 
  scale_y_discrete(expand = c(0, 0)) + 
  theme(legend.position = "none", axis.ticks = element_blank(), 
        axis.text.x = element_text(size = 9 * 0.8, 
                                   angle = 0, hjust = 0, colour = "grey50"))

giving the plot:

解图

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