使用ShinyApp R中的plot_geo()渲染空白图

时间:2016-11-26 03:24:08

标签: r plot shiny plotly

我正在进行数据可视化的最终项目。 我想在ShinyApp中使用Plotly渲染带有标记的地图。我正在使用的功能是 plot_geo()。但是,该图在普通R环境中完美运行,但无法在ShinyApp中呈现,仅显示空白图。并且还报告没有错误消息

我被困在这里很久了。有人可以帮忙吗?谢谢!

我的代码(ShinyApp)

library(shiny)
library(plotly)
library(RColorBrewer)
library(ggplot2)


# Define UI for application 
ui <- fluidPage(

 # Application title
 titlePanel("Times University Ranking"),

 # Sidebar with a slider input for number of bins 
 sidebarLayout(
   sidebarPanel(
    selectInput("year",
                "Select a year",
                c(2011:2016))


 ),

  # Show a plot of the generated distribution
  mainPanel(
    plotOutput("map")
  )
 )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
 times <- data.frame(read.csv("./times.csv", header = TRUE))

 d <- reactive({
 a <- subset(times, year == input$year)
 return (a)
}) 

g <- list(
 scope = 'world',
 projection = list(type = 'Mercator'),
 showframe = FALSE,
 showland = TRUE,
 showsubunits = TRUE,
 landcolor = toRGB("gray95"),
 subunitcolor = toRGB("gray85"),
 countrycolor = toRGB("gray65"),
 countrywidth = 0.5,
 subunitwidth = 0.5
)

output$map <- renderPlotly({

  p <- plot_geo(data = d(), lat = ~lat, lon = ~lon, 
               color = ~as.numeric(rescale), 
               mode = 'markers', colors = "Spectral") %>%
   add_markers(text = ~paste(paste("Rank:", world_rank), 
                             university_name, country, 
                             year, sep = "<br />"), 
               hoverinfo = "text") %>%
   colorbar(title = "World Rank") %>%
   layout(title = paste(input$year, "Times University Ranking on the Map"),
          geo = g)

  })
}

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

普通R代码

library(plotly)
library(RColorBrewer)

input <- read.csv("times.csv", header = TRUE)
df <- data.frame(subset(input, year == 2015))
df <- df[sort.int(df$No, decreasing = TRUE), ]


# geo styling
g <- list(
  scope = 'world',
  projection = list(type = 'Mercator'),
  showframe = FALSE,
  showland = TRUE,
  showsubunits = TRUE,
  landcolor = toRGB("gray95"),
  subunitcolor = toRGB("gray85"),
  countrycolor = toRGB("gray65"),
  countrywidth = 0.5,
  subunitwidth = 0.5
)

  p <- plot_geo(data = df, lat = ~lat, 
  lon = ~lon, color = ~as.numeric(rescale), mode = 'markers', 
  colors = "Spectral") %>%
  add_markers(
    text = ~paste(paste("Rank:", world_rank), university_name,
    country, year, sep = "<br />"),
    hoverinfo = "text") %>%
    colorbar(title = "World Rank") %>%
    layout(title = '2016 Times University Rankings on the map', geo = g)   

0 个答案:

没有答案