有谁知道如何在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代码,但是它不再起作用。它给我错误“秤必须全部都是可数的,否则不可数”。

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)

10-06 13:43
查看更多