散点图滑块,数据显示有问题

时间:2020-07-12 03:15:39

标签: r plotly r-plotly

我正在尝试在R中使用plotly创建地图,我想根据数据的年/月插入一个滑块。滑块正在显示,但似乎没有改变我移动它时显示的数据。我认为这是通过设置steps.args来完成的,“可见”等于与添加的跟踪相对应的TRUE / FALSE值的列表。如果有人能告诉我我做错了,我将不胜感激。

这是我的代码,包括数据集的加载和清理,因此应具有可重现性:

### load and clean data
  library(plotly)
  library(dplyr)
  library(lubridate)

  ##getting the data
  data<- data.frame()
  z<-c(2016,2017,2018,2019,2020)
  for(i in z) {
    url <- paste("http://www.aire.cdmx.gob.mx/opendata/promedios_diarios/promedios_",i,"_ps.csv", sep = "")
    data <- rbind(data, read.csv(url,header = T, skip = 8))
  }
  ##create tibble and summary of tibble
  pm <- tibble(data)
  summary(pm)

  ##clean and arrange data 
  pmclean <- pm %>%
    mutate(datex = strptime(date,"%d/%m/%Y"), 
         month = month(datex), 
         year = year(datex)) %>%
    filter(!is.na(value)) %>%
    select(datex, id_station:value, month, year) %>%
    rename(date = datex)

    ## download Mexico City air quality monitoring station data with lat/long
    url <- "http://www.aire.cdmx.gob.mx/opendata/catalogos/cat_estacion.csv"
      datastations <- tibble(read.csv(url,header = T, skip = 1))

    ##select columns and rename our join column
    stations <- datastations %>%
        select(cve_estac:obs_estac)%>%
        rename(id_station = cve_estac)

    ##join clean PM data with station data
    pmstations<- inner_join(pmclean,stations)
    pm2.5_stations <- subset(pmstations, id_parameter == "PM2.5")

    ## data 
    pm2.5_station <- pm2.5_stations %>% 
      group_by(year, month, id_station, nom_estac, longitud, latitud,alt) %>% 
      summarize("mean" = mean(value))

#########################################################################

### figure

## create field unique for each month
pm2.5_station$yearmonth <- paste(pm2.5_station$year, "/", pm2.5_station$month, sep = "")

##create figure
figx <- pm2.5_station %>% plot_ly(type = 'scattermapbox', mode = 'markers') 

### add traces for each year/month combo
for( i in unique(pm2.5_station$yearmonth)){
  figx <- figx %>% 
    add_trace(type = "scattermapbox",
              lat=pm2.5_station$latitud[pm2.5_station$yearmonth == i], 
              lon=pm2.5_station$longitud[pm2.5_station$yearmonth == i],
              text = pm2.5_station$hovertext[pm2.5_station$yearmonth == i],
              hoverinfo = 'text',
              name= paste(i),
              marker = list(
                color = pm2.5_station$mean[pm2.5_station$yearmonth == i],
                size = pm2.5_station$mean[pm2.5_station$yearmonth == i]))
}


### create steps for slider
daterange <- data.frame(unique(pm2.5_station$yearmonth))
colnames(daterange) <- "x"

steps <- list()
for (i in 1:length(unique(pm2.5_station$yearmonth))) {  
  steps[[i]] <- list(method = "update", 
                     args = list("visible", list(unique(pm2.5_station$yearmonth) == daterange$x[i])),
                     label = daterange$x[i]
  ) 
} 

### layout
figx <- figx %>% layout(
  title = 'map',
  mapbox = list(
    style = 'carto-positron',
    zoom = 10,
    center = list(lon = median(pm2.5_station$longitud), 
                  lat = median(pm2.5_station$latitud))),
  
  sliders = list(
    
    list(
      active = (length(daterange[,])), 
      currentvalue = list(prefix = "Month: "), 
      pad = list(t=60),
      
      steps = steps)
  ))
figx

谢谢!

0 个答案:

没有答案