将美国+加拿大(特定省)与Plotly(plot_geo)或ggplot2组合

时间:2018-08-13 07:54:40

标签: r ggplot2 shiny plotly

我正在构建一个简单的应用程序,以显示我和我的研究小组在过去几个月中获得的一些结果。
数据来自美国的特定州和加拿大的省。
我想在北美地图中合并来自艾伯塔省和安大略省的数据,但是当在locationmode =函数中处理plot_geo时,我无法将它们与数据合并来自美国各州。
这里附有开发的应用程序和我们拥有的数据的示例。

library(shiny)
library(DT)
library(ggplot2)
library(plotly)

data<-read.csv(file="data.csv",header=TRUE,sep=";")
head(data)
data$results <- with(data,paste("% of subjects",'<br>', "State:",data$States_Extended, 
                                '<br>', "Nr. of Samples:", data$Samples))
numbers <- paste("State:",data$States_Extended, '<br>',
                 "Nr. of Samples:", data$Samples)


ui1 <- fluidPage(
 titlePanel("Map of Positive Findings"),
  sidebarLayout(
    sidebarPanel(
      selectInput("variable", "Choose a target variable",
                  c("variable1" = "variable1",
                    "variable2" = "variable2")),
      radioButtons("maps", "Select type of data:",
                   c("Number of samples" = "samples",
                     "% of subjects" = "target"))),

    mainPanel(
     h3(textOutput("caption")),
      plotlyOutput("MapPlot"),
      fluidRow(
        DT::dataTableOutput("datatable")
      )
    )
  )
)




server1 <- function(input, output) {

  # Compute the formula text ----
  # This is in a reactive expression since it is shared by the
  # output$caption and output$mpgPlot functions
  formulaText <- reactive({
    paste("You chose: Map of", input$variable)
  })

  # Return the formula text for printing as a caption ----
  output$caption <- renderText({
    formulaText()
  })

  datasetInput <- reactive({
    switch(input$variable,
           "variable1" = data[,c("States_Extended","variable1")],
           "variable2" = data[,c("States_Extended","variable2")]
           )
  })
  output$datatable <- DT::renderDataTable({
    datasetInput()
  })
# Generate a plot of the requested variable against mpg ----
  # and only exclude outliers if requested
  output$MapPlot <- renderPlotly({
    # give state boundaries a border
    l <- list(color = toRGB("white"), width = 1)
    # specify some map projection/options
    g <- list(
      scope = 'north america',
      projection = list(type = 'albers'),
      showland = TRUE,
      landcolor = toRGB('grey'),
      showlakes = TRUE,
      lakecolor = toRGB('blue'),
      landcolor = toRGB("gray95"),
      subunitcolor = toRGB("gray85"),
      countrycolor = toRGB("gray85"),
      countrywidth = 0.5,
      subunitwidth = 0.5  
    )
    maps <- switch(input$maps,
                   samples = plot_geo(data, locationmode = 'USA-states',
                                      marker = list(line = l)) %>%
                     add_trace(
                       z = ~Samples, text = numbers, locations = ~States,
                       color = ~Samples, colors = 'Reds'
                     ) %>%
                     colorbar(title = paste('Number of <br>','analysis')) %>%
                     layout(
                       title = paste('Number of samples'),
                       geo = g
                     ),
                   target = plot_geo(data, locationmode = 'USA-states',
                                     marker = list(line = l)) %>%
                     add_trace(
                       z = ~data[,input$variable], text = ~results, locations = ~States,
                       color = ~data[,input$variable], colors = 'Oranges'
                     ) %>%
                     colorbar(title = paste(input$variable,'%')) %>%
                     layout(
                       title = paste('% of subjects  <br>', input$variable),
                       geo = g
                     )
    )

  })

}

# Run the application 
shinyApp(ui = ui1, server = server1)

以及相关数据:

structure(list(States = structure(c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 
35L, 36L, 37L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 
49L, 50L, 51L, 52L, 1L, 38L), .Label = c("AB", "AK", "AL", "AR", 
"AZ", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "IA", "ID", "IL", 
"IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", 
"MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", 
"ON", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VA", "VT", 
"WA", "WI", "WV", "WY"), class = "factor"), States_Extended = structure(c(2L, 
1L, 5L, 4L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 16L, 13L, 14L, 15L, 
17L, 18L, 19L, 22L, 21L, 20L, 23L, 24L, 26L, 25L, 27L, 34L, 35L, 
28L, 30L, 31L, 32L, 29L, 33L, 36L, 37L, 39L, 40L, 41L, 42L, 43L, 
44L, 45L, 46L, 48L, 47L, 49L, 51L, 50L, 52L, 3L, 38L), .Label = c("Alabama", 
"Alaska", "Alberta", "Arizona", "Arkansas", "California", "Colorado", 
"Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", 
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", 
"Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", 
"Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", 
"New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", 
"Ohio", "Oklahoma", "Ontario", "Oregon", "Pennsylvania", "Rhode Island", 
"South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", 
"Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", 
"Wyoming"), class = "factor"), Samples = c(1L, 2L, 0L, 14L, 4L, 
2L, 1L, 2L, 3L, 7L, 0L, 2L, 0L, 0L, 6L, 0L, 12L, 2L, 0L, 0L, 
0L, 2L, 4L, 4L, 1L, 2L, 5L, 0L, 2L, 0L, 4L, 4L, 1L, 25L, 3L, 
4L, 0L, 8L, 1L, 6L, 0L, 4L, 7L, 4L, 5L, 0L, 0L, 1L, 0L, 0L, 3L, 
1L), variable1 = c(0L, 50L, NA, 64L, 0L, 50L, 100L, 50L, 100L, 
57L, NA, 50L, NA, NA, 100L, NA, 33L, 50L, NA, NA, NA, 100L, 50L, 
50L, 100L, 100L, 60L, NA, 0L, NA, 75L, 75L, 100L, 72L, 100L, 
50L, NA, 63L, 100L, 50L, NA, 50L, 57L, 75L, 100L, NA, NA, 0L, 
NA, NA, 100L, 100L), variable2 = c(0L, 0L, NA, 0L, 0L, 0L, 0L, 
0L, 0L, 29L, NA, 0L, NA, NA, 17L, NA, 17L, 0L, NA, NA, NA, 0L, 
0L, 0L, 0L, 0L, 20L, NA, 0L, NA, 25L, 25L, 0L, 8L, 0L, 0L, NA, 
0L, 0L, 17L, NA, 0L, 14L, 0L, 40L, NA, NA, 0L, NA, NA, 0L, 0L
)), class = "data.frame", row.names = c(NA, -52L))

在此我还附上当前开发的应用程序的示例:

Example of the app

0 个答案:

没有答案