热图与ggvis中的数字

时间:2014-07-11 15:33:07

标签: r ggvis

我正在尝试使用ggp中ggplot2中的数字来复制热图。 ggplot2版本是

library(ggplot2)
hec <- as.data.frame(xtabs(Freq ~ Hair + Eye, HairEyeColor))
ggplot(hec, aes(Hair, Eye)) +
geom_tile(aes(fill = Freq)) + 
geom_text(aes(label = Freq),colour="white") 

它看起来像那样 enter image description here

我在ggvis中的版本是

hec%>%
ggvis(~Hair, ~Eye, fill=~Freq)%>%
layer_rects(width = band(), height = band()) %>%
layer_text(text:=~Freq,fontSize := 20, fill:="white",baseline:="top",align:="center") %>%
scale_nominal("x", padding = 0, points = FALSE) %>%
scale_nominal("y", padding = 0, points = FALSE) 

并且结果并不完美

enter image description here

我尝试通过手动添加边距来修复数字对齐,但这种情况不可调整大小。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

一种解决方法是使用xcenterycenter比例:

hec <- as.data.frame(xtabs(Freq ~ Hair + Eye, HairEyeColor))

hec%>%
  ggvis(~Hair, ~Eye, fill=~Freq) %>%
  layer_rects(width = band(), height = band()) %>%
  layer_text(
    x = prop("x", ~Hair, scale = "xcenter"),
    y = prop("y", ~Eye, scale = "ycenter"),
    text:=~Freq, fontSize := 20, fill:="white", 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)

答案 1 :(得分:0)

layer_text()中,尝试align:="left"。这似乎适用于this example