Does anyone know how to make an color bar in ggvis
or ggplot2
without any margins? ggvis
is preferred since I am putting this in a shiny
application.
I have a partially working example in ggplot2:
data:
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() comes from ggmap
and you can copy the function from here.
ggplot2 (has margins):
ggplot(df, aes(y = 1, x = x, fill = col)) + geom_tile() +
scale_fill_identity() + theme_nothing()
Running this gives me this image. Unfortunately this still has margins.
I have some ggvis
code but it is no longer working. It gives me the error "Scales must all be countable, or not countable".
ggvis (not working):
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)
Thanks.
thanks aosmith, this works:
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)