微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 R 中使用滑块随时间变化的地图图表制作 R Plotly Choropleth

如何解决如何在 R 中使用滑块随时间变化的地图图表制作 R Plotly Choropleth

在 R(不是 Python)中,我正在逐年制作带有滑块的绘图地图 - 当您移动滑块时,地图会根据所选年份的数据发生变化,并且颜色条(图例)值也必须根据那一年的数据—— 我做了一年,但不是多年。我在谷歌上搜索过帮助,但我能找到的只是 Python 的例子,然后我试图复制它,但我似乎无法让它工作。 这是我尝试在 R 中复制的 python 示例的链接(如果您愿意,请跳到多个地图部分以节省时间) https://didaskalia-jpc.blogspot.com/2019/02/step-by-step-how-to-plot-map-with.html

使用的数据可以从上面的链接下载或直接从这里下载: https://arch.library.northwestern.edu/concern/generic_works/ws859f85j (数据文件名为:CrimeStatebyState_1960-2014.csv)

现在,这是我到目前为止的代码,我认为我的问题始于填充“步骤”列表时的第二个循环以及之后的任何内容

任何帮助将不胜感激,我的代码也附上。

library('jsonlite')
library('data.table')
library('htmlwidgets')
library('plotly')

path <- "You path here" 

Outfile <- paste0(path,"Outfile\\")

df_merged <- read.csv(paste0(path,"CrimeStatebyState_1960-2014.csv"),stringsAsFactors=FALSE,sep = ",")

year = 1960

scl <- c('#ffffff','#ff9999','#ff4d4d','#ff1a1a','#cc0000','#4d0000')
redvals <- as.factor(c(0.0,0.2,0.4,0.6,0.8,1.0))

names(scl) = levels(redvals)

# create empty list for data object:    
data_slider <- c()

# populate the data object:
while (year %in% unique(df_merged$Year)){
  
  # select the year (and remove DC for Now)
  df_sected_crime <- subset(df_merged,df_merged$State!= 'district of Columbia' & (df_merged$Year== year))
  
  #### Not sure if this is necessary and how to do it in R since most columns are numeric so I skipped it

  # for col in df_sected_crime.columns:  # I transform the columns into string type so I can:
  #   df_sected_crime[col] = df_sected_crime[col].astype(str)
  
  # create the text for mouse-hover for each state,for the current year:
  df_sected_crime$text <- paste0(df_sected_crime$State,",Pop: ",'<br>',df_sected_crime$Population,Murder rate: ",df_sected_crime$Murder_per100000)
  
  # create the list with the data for the current year:
  data_one_year  <- plot_ly(df_sected_crime,locationmode = 'USA-states')
  data_one_year  <- data_one_year  %>% add_trace(
    type='choropleth',locations = ~State_code,z = ~Murder_per100000,colorscale = scl,text = ~text 
  ) 
  
  # add the Map-list to the list of dictionaries/lists for the slider
  data_slider <- append(data_slider,data_one_year,after = length(data_slider ))
  
  year= year+1
  
}

# create the steps for the slider
steps <- c()

#reset year value as it changed in the first loop
year = 1960

i <- 1
# while(i %in% seq(length(data_slider))){
while(i  %in% seq(length(unique(df_merged$Year)))){     #Not sure if this is the right condition as I don't kNow what the length of data_slider is in python code but i assume its the length of years
  step <- list(method='restyle',args=c('visible',FALSE * length(unique(df_merged$Year))),label= paste0("Year ",i + (year-1))                     # label to be displayed for each step (year))
  )
  step['args'][1][i]=TRUE
  steps <- append(steps,step,after = length(steps))
  
  
  i=i+1
}

# create the 'sliders' object from the 'steps':
sliders <- list(active=0,pad=("t"= 1),steps=steps)

# set up the layout (including slider option)
layout <- list(geo=list(scope='usa',projection = list(type = 'albers usa')),sliders=sliders)


fig <- list(data=data_slider,layout=layout) 

fig

# After this I don't kNow how to continue,the plot_ly function doesnt work with list and when I use plotly_build I don't get results.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。