0

I have tried by myself but I don't know where is the mistake.

I am trying to animate a Spanish province map with covid19 data. I can plot without any problem the map and the data from a chosen day, but I can't animate all the data along the time period.

This is the .xml file xml file with the data

This is the link to download the shape files: shape files here

and this is my code.

library(tidyverse)    
library(rgdal)     
library(broom)
library(ggplot2)
library(readxl)
library(gganimate)

setwd("C:/Users/...../Provincias_ETRS89_30N")

carto_base <- readOGR("Provincias_ETRS89_30N.shp")
data_provincias <- tidy(carto_base)

dfprovincias <- read_excel("provincias3.xls")
dfprovincias$id <- as.character(dfprovincias$id)
dfprovincias$hospitalized_per_100000 = as.numeric(gsub(",", "\\.",
                                                  dfprovincias$hospitalized_per_100000))
dfprovincias$date <- as.Date(dfprovincias$date, "%y/%m/%d")

dfprovincias_grafico <- data_provincias %>%
  left_join(dfprovincias, by= "id")
dfprovincias_grafico <- na.omit(dfprovincias) 

cols <- RColorBrewer::brewer.pal(8,'Paired')[c(3,6)]

dfprovincias_grafico %>%
  #filter(date == "2020-10-01")%>% 
  ggplot(aes(x=long, y= lat, group = group)) +
  geom_polygon(aes(fill=hospitalized_per_100000), color= "black", size = 0.2) +
  scale_fill_gradient(low=cols[1],high=cols[2]) +
  ggtitle("Hospitalizados por 100.000 habitantes en las provincias de España") +
  transition_reveal(date)
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Raul
  • 139
  • 9
  • i have this error: Error in seq.default(range[1], range[2], length.out = nframes) : 'from' must be a finite number – Raul Oct 05 '20 at 21:47
  • I'm hesitant to download unknown files from google drive. Looking at your code, though, I wonder if `dfprovincias_grafico <- na.omit(dfprovincias)` is doing what you expected? Since this renders the previous `left_join` step useless. – Z.Lin Oct 06 '20 at 07:53
  • I think that these line don´t work. Where I can upload the xml file where you guess its legit? – Raul Oct 06 '20 at 11:05
  • I think that the problem is in the date format. How must interpretate? I have tried this: date<-as.Date(dfprovincias$date, "%d/%m/%y") / this too : as.POSIXct("%Y/%m/%d") ..but not work – Raul Oct 06 '20 at 11:19
  • 1
    Looks like you have two separate problems here. 1) data processing; & 2) plotting animation. On 1), Excel files can be pretty weird about dates. Suggest you save the file (only the relevant tab if there's more than one, & only the relevant columns) as a CSV file, import *that* into R instead, & go from there. Leave 2) alone until you've sorted the first part out. – Z.Lin Oct 07 '20 at 02:08
  • 1
    When you've got the desired dataframe in R, look [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for how to share minimal relevant data to get more targeted help on plotting. – Z.Lin Oct 07 '20 at 02:09

0 Answers0