To add value
as a text label to 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"))
For your second question, your current code already takes care of that. The variable rescale
scales each column separately, because you've performed the operation grouped by variable
. Since rescale
is the fill
variable, each column's values are rescaled from zero to one for the purposes of setting color values. You don't need the tableau.s ... last.plot...
code.
Here's what the plot looks like after running the code above. Note that in each column, the lowest value is white and the highest value is steel blue. (You might want to change the border color from "white" to, say, "gray90", so that there will be a border between adjacent white squares):
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…