Я пытаюсь создать карту со странами Европейского Союза, в которых страны окрашены в соответствии с количеством подтвержденных случаев на 100000 жителей. Я могу правильно построить необработанный график .
Однако, когда я пытаюсь добавить данные, график меняется странным образом, и я не могу понять, почему. .
У вас есть идеи, как правильно это построить?
# Collect data on Covid-19
# Direct use of function fromJSON
jsonLot <-
fromJSON("https://pomber.github.io/covid19/timeseries.json")
# Export downloaded data to file 'covid19.json'
myjsonLot <- toJSON(jsonLot, pretty=TRUE)
fileConn <- file(paste0(dirData, "covid19.json"))
writeLines(myjsonLot, fileConn)
close(fileConn)
# Store data into separate dataframe
dfAustria <- as.data.frame(jsonLot$Austria)
dfBelgium <- as.data.frame(jsonLot$Belgium)
dfBulgaria <- as.data.frame(jsonLot$Bulgaria)
dfCroatia <- as.data.frame(jsonLot$Croatia)
dfCyprus <- as.data.frame(jsonLot$Cyprus)
dfCzechia <- as.data.frame(jsonLot$Czechia)
dfDenmark <- as.data.frame(jsonLot$Denmark)
dfEstonia <- as.data.frame(jsonLot$Estonia)
dfFinland <- as.data.frame(jsonLot$Finland)
dfFrance <- as.data.frame(jsonLot$France)
dfGermany <- as.data.frame(jsonLot$Germany)
dfGreece <- as.data.frame(jsonLot$Greece)
dfHungary <- as.data.frame(jsonLot$Hungary)
dfIreland <- as.data.frame(jsonLot$Ireland)
dfItaly <- as.data.frame(jsonLot$Italy)
dfLatvia <- as.data.frame(jsonLot$Latvia)
dfLithuania <- as.data.frame(jsonLot$Lithuania)
dfLuxembourg <- as.data.frame(jsonLot$Luxembourg)
dfMalta <- as.data.frame(jsonLot$Malta)
dfNetherlands <- as.data.frame(jsonLot$Netherlands)
dfPoland <- as.data.frame(jsonLot$Poland)
dfPortugal <- as.data.frame(jsonLot$Portugal)
dfRomania <- as.data.frame(jsonLot$Romania)
dfSlovakia <- as.data.frame(jsonLot$Slovakia)
dfSlovenia <- as.data.frame(jsonLot$Slovenia)
dfSpain <- as.data.frame(jsonLot$Spain)
dfSweden <- as.data.frame(jsonLot$Sweden)
# Remove rows with zero cases
dfAustria <- dfAustria[!dfAustria$confirmed == 0,]
dfBelgium <- dfBelgium[!dfBelgium$confirmed == 0,]
dfBulgaria <- dfBulgaria[!dfBulgaria$confirmed == 0,]
dfCroatia <- dfCroatia[!dfCroatia$confirmed == 0,]
dfCyprus <- dfCyprus[!dfCyprus$confirmed == 0,]
dfCzechia <- dfCzechia[!dfCzechia$confirmed == 0,]
dfDenmark <- dfDenmark[!dfDenmark$confirmed == 0,]
dfEstonia <- dfEstonia[!dfEstonia$confirmed == 0,]
dfFinland <- dfFinland[!dfFinland$confirmed == 0,]
dfFrance <- dfFrance[!dfFrance$confirmed == 0,]
dfGermany <- dfGermany[!dfGermany$confirmed == 0,]
dfGreece <- dfGreece[!dfGreece$confirmed == 0,]
dfHungary <- dfHungary[!dfHungary$confirmed == 0,]
dfIreland <- dfIreland[!dfIreland$confirmed == 0,]
dfItaly <- dfItaly[!dfItaly$confirmed == 0,]
dfLatvia <- dfLatvia[!dfLatvia$confirmed == 0,]
dfLithuania <- dfLithuania[!dfLithuania$confirmed == 0,]
dfLuxembourg <- dfLuxembourg[!dfLuxembourg$confirmed == 0,]
dfMalta <- dfMalta[!dfMalta$confirmed == 0,]
dfNetherlands <- dfNetherlands[!dfNetherlands$confirmed == 0,]
dfPoland <- dfPoland[!dfPoland$confirmed == 0,]
dfPortugal <- dfPortugal[!dfPortugal$confirmed == 0,]
dfRomania <- dfRomania[!dfRomania$confirmed == 0,]
dfSlovakia <- dfSlovakia[!dfSlovakia$confirmed == 0,]
dfSlovenia <- dfSlovenia[!dfSlovenia$confirmed == 0,]
dfSpain <- dfSpain[!dfSpain$confirmed == 0,]
dfSweden <- dfSweden[!dfSweden$confirmed == 0,]
# Import shape file
# Add the definition of the shape file subdirectory to the
# path definitions
dirNbh <- paste0(dirData, "european-union-countries/")
# Read the shape file from the subdirectory. The readOGR
# function is from the package rgdal.
dfShape <- readOGR(paste0(dirNbh, "european-union-countries.shp"),
stringsAsFactors = FALSE)
dfShape <- spTransform(dfShape, CRS("+init=epsg:3035"))
dfShape <- spTransform(dfShape, CRS("+proj=longlat +datum=WGS84"))
#unique(dfShape@data$brk_name)
clipRdam <- as(extent(-15, 38, 25, 75), "SpatialPolygons")
# Apply the coordinate reference system of dfShape.Rdam
# to clipRdam
proj4string(clipRdam) <- CRS(proj4string(dfShape))
# Apply the clipping. The result is stored in dfShape.RdamClip
dfShape <- intersect(dfShape, clipRdam)
ggplot(data = dfShape, aes(x = long, y = lat, group = group)) +
geom_path()
dfData.EU <- fortify(dfShape, region = "name")
# Source: https://www.worldometers.info/population/countries-in-europe-by-population/
# Read population data
popEU <- read_delim("C:/Users/wishi/OneDrive/Documenten/BIM/BDBA/Data/population.csv",
";", escape_double = FALSE, trim_ws = TRUE)
popEU$cases <- NULL
# Get latest confirmed cases
dfAustria<-(max(dfAustria$confirmed, na.rm = TRUE))
dfBelgium<-(max(dfBelgium$confirmed, na.rm = TRUE))
dfBulgaria<-(max(dfBulgaria$confirmed, na.rm = TRUE))
dfCroatia<-(max(dfCroatia$confirmed, na.rm = TRUE))
dfCyprus<-(max(dfCyprus$confirmed, na.rm = TRUE))
dfCzechia<-(max(dfCzechia$confirmed, na.rm = TRUE))
dfDenmark<-(max(dfDenmark$confirmed, na.rm = TRUE))
dfEstonia<-(max(dfEstonia$confirmed, na.rm = TRUE))
dfFinland<-(max(dfFinland$confirmed, na.rm = TRUE))
dfFrance<-(max(dfFrance$confirmed, na.rm = TRUE))
dfGermany<-(max(dfGermany$confirmed, na.rm = TRUE))
dfGreece<-(max(dfGreece$confirmed, na.rm = TRUE))
dfHungary<-(max(dfHungary$confirmed, na.rm = TRUE))
dfIreland<-(max(dfIreland$confirmed, na.rm = TRUE))
dfItaly<-(max(dfItaly$confirmed, na.rm = TRUE))
dfLatvia<-(max(dfLatvia$confirmed, na.rm = TRUE))
dfLithuania<-(max(dfLithuania$confirmed, na.rm = TRUE))
dfLuxembourg<-(max(dfLuxembourg$confirmed, na.rm = TRUE))
dfMalta<-(max(dfMalta$confirmed, na.rm = TRUE))
dfNetherlands<-(max(dfNetherlands$confirmed, na.rm = TRUE))
dfPoland<-(max(dfPoland$confirmed, na.rm = TRUE))
dfPortugal<-(max(dfPortugal$confirmed, na.rm = TRUE))
dfRomania<-(max(dfRomania$confirmed, na.rm = TRUE))
dfSlovakia<-(max(dfSlovakia$confirmed, na.rm = TRUE))
dfSlovenia<-(max(dfSlovenia$confirmed, na.rm = TRUE))
dfSpain<-(max(dfSpain$confirmed, na.rm = TRUE))
dfSweden<-(max(dfSweden$confirmed, na.rm = TRUE))
temp <-
as.data.frame(cbind(dfAustria, dfBelgium, dfBulgaria, dfCroatia,dfCyprus, dfCzechia,dfDenmark, dfEstonia, dfFinland, dfFrance, dfGermany, dfGreece, dfHungary, dfIreland, dfItaly, dfLatvia, dfLithuania, dfLuxembourg, dfMalta, dfNetherlands, dfPoland, dfPortugal, dfRomania, dfSlovakia, dfSlovenia, dfSpain, dfSweden))
# Switch the rows and the columns
temp = as.data.frame(t(temp))
popEU$cases <- temp$V1
# Calculation
popEU['Relative'] = (popEU['cases'] / popEU['population']) * 100000
# remove redundant columns
popEU$population <- NULL
popEU$cases <- NULL
dfData.EUrel <-
merge(dfData.EU, popEU, by = "id", all.x = TRUE)
ggplot(data = dfData.EUrel, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = Relative), colour = "white") +
scale_fill_continuous(low="white", high="red")