R 带有ggplot2的每列热图
我正在使用这个R脚本:R 带有ggplot2的每列热图,r,ggplot2,R,Ggplot2,我正在使用这个R脚本: 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
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
tableau要向每个单元格添加值
作为文本标签,可以使用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"))
p使用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")
其中:
Hi@eipi10,我意识到第一列的缩放方式应该与其他列相反。也就是说,1.88应该是最暗的,2.13应该是白色的。有没有办法只反转一列的缩放比例?(我应该为此问一个新问题吗?)我真的去问了一个新问题。。。干杯