我正在尝试使用ggvis创建一个地图,该地图根据Shiny输入值以交互方式更新fill属性。但是,我遇到了三个问题:
有关可重现的代码示例,请参阅下文。在此先感谢帮助Shiny / ggvis新手!
# libraries
suppressMessages(library(dplyr))
suppressMessages(library(ggvis))
library(shiny)
library(RColorBrewer)
# create dummy data
base.long <- c(0, 1, 1, 0)
base.lat <- c(0, 0, 1, 1)
test <- data.frame(long = rep(c(base.long, base.long + 1), 3),
lat = c(rep(base.lat, 2), rep(base.lat + 1, 2), rep(base.lat + 2, 2)),
order = rep(1:4, 6), group = rep(seq(1:6), 4)[order(rep(seq(1:6), 4))],
var = rep(seq(5, 30, 5), 4)[order(rep(seq(5, 30, 5), 4))])
# define color palette
spectrum <- colorRampPalette(brewer.pal(n = 4, name="YlGn"))
shinyServer(function(input, output) {
# determine fill color based on variable
toplot <- reactive({
test$fill.col <- as.character(cut(test$var, seq(10, 30, 5),
include.lowest = T, labels = spectrum(4)))
test$fill.col <- ifelse(is.na(test$fill.col) |
test$var < input$range[1] | test$var > input$range[2],
"#FFFFFF", test$fill.col)
return(test)
})
toplot %>%
group_by(group) %>%
ggvis(x = ~long, y = ~lat) %>%
layer_paths(fill = ~fill.col) %>%
scale_ordinal("fill", range = c("#FFFFFF", spectrum(4)),
label = "Variable Value") %>%
set_options(width = 400, height = 600, keep_aspect = T) %>%
bind_shiny("test.ggvis")
})
suppressMessages(library("ggvis"))
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("range", label = "Filter Range",
min = 5, max = 30, step = 5, value = c(5, 25))
),
mainPanel(ggvisOutput("test.ggvis"))
)
))