使用此dat
data.frame
set.seed(123)
stationID <- c(rep("station1", 3), rep("station2", 3), rep("station3", 3),rep("station4", 3))
x <- c(rep(-77.2803, 3), rep(-79.1243, 3), rep(-93.4991, 3),rep(-117.632, 3))
y <- c(rep(35.8827, 3), rep(42.4356, 3), rep(30.0865, 3),rep(34.0905, 3))
parameter <- rep(c("A", "B", "C"), 4)
value <- c(sample(1200:1800, 3), sample(900:1300, 3), sample(1400:2200, 3), sample(850:1400, 3))
dat <- data.frame(stationID, x, y, parameter, value)
head(dat)
我使用rCharts
创建了多条形图,如下所示
library(rCharts)
n1 <- nPlot(value ~ stationID,
group = "parameter",
data = dat,
type = "multiBarChart")
n1$xAxis(axisLabel = 'Station')
n1$yAxis(axisLabel = 'Value')
n1$chart(margin=list(left=100,bottom=100))
n1$params$width <- 1700
n1$params$height <- 700
n1
这是结果
我创建了传单地图,如下所示
library(tidyr)
dat1 <- tidyr::spread(dat, parameter, value)
library(leaflet)
leaflet(dat1) %>%
addProviderTiles("OpenStreetMap", group = "OpenStreetMap") %>%
addCircleMarkers(~x, ~y, radius= ~A/50, color ="red",
stroke = FALSE,
fillOpacity = 0.4,
group = "A", popup = ~as.character(A)) %>%
addCircleMarkers(~x, ~y, radius= ~B/100, color ="green",
stroke = FALSE,
fillOpacity = 0.4,
group = "B", popup = ~as.character(B)) %>%
addCircleMarkers(~x, ~y, radius= ~C/150, color ="blue",
stroke = FALSE,
fillOpacity = 0.4,
group = "C", popup = ~as.character(C)) %>%
addLayersControl(position = "bottomleft",
baseGroups = c("OpenStreetMap"),
overlayGroups = c("A","B","C"))
这是结果
我将它们两者结合在闪亮的应用程序中
library(shiny)
library(rCharts)
library(leaflet)
ui <- fluidPage(sidebarLayout(
sidebarPanel(h1("Shiny app"),
tags$hr(),
h2("several options here"), width =2),
mainPanel(width = 10,
uiOutput("tb"))))
server <- function(input,output){
output$chart1 <- renderChart({
n1 <- nPlot(value ~ stationID,
group = "parameter",
data = dat,
type = "multiBarChart")
n1$xAxis(axisLabel = 'Station')
n1$yAxis(axisLabel = 'Value')
n1$chart(margin=list(left=100,bottom=100))
n1$addParams(dom="chart1")
n1$params$width <- 1700
n1$params$height <- 700
return(n1)
})
output$map1 <- renderLeaflet({
leaflet(dat1) %>%
addProviderTiles("OpenStreetMap", group = "OpenStreetMap") %>%
addCircleMarkers(~x, ~y, radius= ~A/50, color ="red",
stroke = FALSE,
fillOpacity = 0.4,
group = "A", popup = ~as.character(A)) %>%
addCircleMarkers(~x, ~y, radius= ~B/100, color ="green",
stroke = FALSE,
fillOpacity = 0.4,
group = "B", popup = ~as.character(B)) %>%
addCircleMarkers(~x, ~y, radius= ~C/150, color ="blue",
stroke = FALSE,
fillOpacity = 0.4,
group = "C", popup = ~as.character(C)) %>%
addLayersControl(position = "bottomleft",
baseGroups = c("OpenStreetMap"),
overlayGroups = c("A","B","C"))
})
output$tb <- renderUI({
tabsetPanel(tabPanel("First plot", tags$br(), tags$br(), tags$br(),
showOutput("chart1", lib ="nvd3"),
tags$br(), tags$br(), tags$br(),
leafletOutput("map1", height = 750)))
})
}
shinyApp(ui = ui, server = server)
rCharts
情节工作正常,但leaflet
地图上没有数据。
任何建议都会受到赞赏吗?