Heat map on a post with ggplot2

I am using this R script:

tableau <- read.table(
  text = 
    "Net    B   C   D   E.(e)   F.(f)
a   1.88    0.15    0.60    10.00   90.00
b   2.05    0.23    0.51    55.00   80.00
c   2.09    0.29    0.40    58.00   88.00
d   2.07    0.52    0.36    80.00   84.00
e   2.13    0.30    0.27    7.00    90.00",
  header = TRUE)

library(plyr)
library(reshape)
library(ggplot2)
library(scales)
tableau.m <- melt(tableau)
tableau.m <- ddply(tableau.m, .(variable), transform, rescale = rescale(value))

(p <- ggplot(tableau.m, aes(variable, Net)) + 
    geom_tile(aes(fill = rescale), colour = "white") + 
    scale_fill_gradient(low = "white", high = "steelblue"))

base_size <- 9
p + theme_grey(base_size = base_size) + 
  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 = base_size * 0.8, angle = 0, 
                                   hjust = 0, colour = "grey50"))

tableau.s <- ddply(tableau.m, .(variable), transform, rescale = scale(value))

last_plot() %+% tableau.s

And I get this plot:

enter image description here

If darker blue means higher values ​​and white means lower values.

How, if possible, I can change this code so that:

  • are the values ​​from the table displayed in each corresponding cell of the matrix graph?
  • the range of the heatmap is not calculated on the entire matrix, but rather for each column. So for each category: B, C, D, E (e) and F (f) does white mean a lower value for this column and a darker blue means a higher column value?

Thank!

+4
source share
2 answers

To add valueas a text label for each cell, you can use geom_text:

p <- ggplot(tableau.m, aes(variable, Net)) + 
      geom_tile(aes(fill = rescale), colour = "white") + 
      scale_fill_gradient(low = "white", high = "steelblue") +
      geom_text(aes(label=value))

# Add the theme formatting
base_size <- 9
p + theme_grey(base_size = base_size) + 
  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 = base_size * 0.8, 
                                   angle = 0, hjust = 0, colour = "grey50"))

. rescale , , variable. rescale fill, . tableau.s ... last.plot....

. , , - . (, "" , , "90", ):

enter image description here

+8

tidyr dplyr ggvis :

library(dplyr)
library(ggvis)
library(tidyr)

tableau %>% 
  gather(variable, value, -Net) %>%
  group_by(variable) %>%
  mutate(scale = percent_rank(value)) %>%
  mutate_each(funs(factor(.)), -value, -scale) %>%
  ggvis(~variable, ~Net, fill=~scale) %>%
  layer_rects(width = band(), height = band(), stroke := NA) %>%
  layer_text(
    x = prop("x", ~variable, scale = "xcenter"),
    y = prop("y", ~Net, scale = "ycenter", ),
    text:=~value, fontSize := 14, fontWeight := "bold", fill:="black", 
    baseline:="middle", align:="center") %>%
  scale_nominal("x", padding = 0, points = FALSE) %>%
  scale_nominal("y", padding = 0, points = FALSE) %>% 
  scale_nominal("x", name = "xcenter", padding = 1, points = TRUE) %>%
  scale_nominal("y", name = "ycenter", padding = 1, points = TRUE) %>%
  scale_numeric("fill", range = c("white", "steelblue")) %>%
  add_axis("x", properties = axis_props(grid = list(stroke = NA))) %>%
  add_axis("y", properties = axis_props(grid = list(stroke = NA))) %>%
  hide_legend("fill")

:

enter image description here

+2

All Articles