ggvis colorbar(或ggplot2)没有任何边距

ggvis colorbar (or ggplot2) without any margin

有谁知道如何在 ggvisggplot2 中制作没有边距的彩条? ggvis 是首选,因为我将它放在 shiny 应用程序中。

我在 ggplot2 中有一个部分工作的示例:

数据:

legendData <- c(100, 325, 550, 775, 1000)
legendColors <- c("#FF9DEB", "#9FC5FF", "#00E4D0", "#AAD44D", "#FFAE85")
df <- data.frame(x = legendData, col = legendColors, y = 1:5, stringsAsFactors = FALSE)

theme_nothing() 来自 ggmap,您可以从 here.

复制函数

ggplot2(有边距):

ggplot(df, aes(y = 1, x = x, fill = col)) + geom_tile() + 
scale_fill_identity() + theme_nothing()

运行 这给了我这个 image。不幸的是,这仍然有利润。

我有一些 ggvis 代码,但它不再有效。它给了我错误 "Scales must all be countable, or not countable".

ggvis(不工作):

df %>% ggvis(~y, ~x, fill := ~col) %>% 
layer_rects(width = band(), height = band()) %>%
scale_nominal("x", padding = 0, points = FALSE) %>%
scale_nominal("y", padding = 0, points = FALSE) %>%
add_axis("y", title = "", properties = axis_props(labels = list(fontSize = 14))) %>%
add_axis("x", title = "") %>% set_options(width = 25, height = 100)

谢谢。

感谢 aosmith,这有效:

legendData <- as.factor(c(100, 325, 550, 775, 1000))
legendColors <- c("#FF9DEB", "#9FC5FF", "#00E4D0", "#AAD44D", "#FFAE85")
df <- data.frame(x = legendData, col = legendColors, 
  y = as.factor(rep(1,length(legendData))), stringsAsFactors = FALSE)
df %>% ggvis(~x, ~y, fill := ~col, strokeWidth := 0) %>%
layer_rects(width = band(), height = band()) %>% hide_axis("x") %>% hide_axis("y") %>%
scale_nominal("x", padding = 0, points = FALSE) %>% 
scale_nominal("y", padding = 0, points = FALSE)