我正在尝试使用ggvis中的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")
Run Code Online (Sandbox Code Playgroud)
它看起来像那样

我在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)
Run Code Online (Sandbox Code Playgroud)
结果并不完美

我试图通过手动添加边距来修复数字对齐,但这种情况不可调整大小.
有任何想法吗?
一种解决方法是使用xcenter和ycenter缩放:
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2175 次 |
| 最近记录: |