互动欧洲地图r闪亮ggmap

发布于 2025-01-23 10:31:36 字数 3516 浏览 0 评论 0原文

对于一个小组项目,我想基于此数据集创建一个交互式欧洲地图: https://我们的worldindata.org/causes-feath
我设法创建了这张地图,该地图将显示2019年欧洲溺水造成的死亡人数。但是我想创建一个交互式地图,用户可以在其中输入死亡原因,例如艾滋病毒或火灾,以便该地图可以显示。我试图使用输入,但它不起作用,并向我显示了此错误消息:“错误:提供给连续规模的离散值”。我知道这个问题的原因是参数大小是int,但输入是char。 不幸的是我不知道该如何纠正这个问题:(

任何人可以帮我吗?
祝你有美好的一天,
是。

library("maptools")
library(rgdal)
library(maps)
library("jsonlite")
library(RColorBrewer)
library(ggmap)

rowdb <- read.csv("annual-number-of-deaths-by-cause.csv", sep = ';')
rowdb <- select(rowdb, -X.1, -X)
        
europe <- c(left = -12, bottom = 35, right = 30, top = 63)
map_E <- get_stamenmap(europe, zoom = 5,"toner-lite")
ggmap(map_E)
    
#On va cr?er une variable contenant les noms des pays du continent puis pour une date donn?e
#qu'on pourra aussi demander ? l'utilisateur de s?lectionner on affichera la carte du 
#continent et la cause choisies
Europe_E <- c("Albania", "Andorra", "Armenia", "Austria","Azerbaijan","Belarus",
                  "Belgium", "Bosnia and Herzegovina", "Bulgaria", "Croatia","Cyprus",
                  "Czechia", "Denmark", "Estonia", "Finland", "France", "Georgia",
                  "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Italy", 
                  "Kazakhstan","Latvia", "Lithuania", "Luxembourg", "Malta", "Moldova", "Monaco",
                  "Montenegro", "Netherlands","North Macedonia", "Norway", "Poland", "Portugal",
                  "Romania", "Russia", "San Marino","Serbia", "Slovakia", "Slovenia", "Spain", 
                  "Sweden", "Switzerland", "Turkey", "Ukraine", "United Kingdom")
    
    #Fonction pour avoir la latitude et longitude des pays
    if (!(require(jsonlite))) install.packages("jsonlite")
    mygeocode <- function(adresses){
      # adresses est un vecteur contenant toutes les adresses sous forme de chaine de caracteres
      nominatim_osm <- function(address = NULL){
        ## details: http://wiki.openstreetmap.org/wiki/Nominatim
        ## fonction nominatim_osm propos?e par D.Kisler
        if(suppressWarnings(is.null(address)))  return(data.frame())
        tryCatch(
          d <- jsonlite::fromJSON(
            gsub('\\@addr\\@', gsub('\\s+', '\\%20', address),
                 'http://nominatim.openstreetmap.org/search/@addr@?format=json&addressdetails=0&limit=1')
          ), error = function(c) return(data.frame())
        )
        if(length(d) == 0) return(data.frame())
        return(c(as.numeric(d$lon), as.numeric(d$lat)))
      }
      tableau <- t(sapply(adresses,nominatim_osm))
      colnames(tableau) <- c("lon","lat")
      return(tableau)
    }

        latlon_E <- mygeocode(Europe_E)
        #is(lalo)
        dat_E <- as.data.frame(latlon_E)
        DAT_E <- rowdb
        DAT_E$Year <- year(as.Date(DAT_E$Year, format = "%Y"))
        DAT_E <- DAT_E[
          as.character(DAT_E$Year) %in% c("2019"), ]
        DAT_E <- DAT_E[
          DAT_E$Entity %in% Europe_E, ]
        
        ggmap(map_E, extent = "device") + 
          geom_point(data = DAT_E, aes(x = dat_E$lon , y =dat_E$lat, color = Europe_E, size = Drowning), alpha = 0.5) +
          geom_text(data = DAT_E, aes(x = dat_E$lon, y = dat_E$lat, label = Drowning), color = "black", size = 3.7) +
          scale_size_continuous(range = c(6, 12)) + theme(legend.position = "none")+
          facet_wrap(~ Year)
        

For a group project, I wanted to create a interactive Europe map, based on this data set: https://ourworldindata.org/causes-of-death.
I have manage to create this map, which will display the number of deaths caused by drowning in 2019 in Europe. But I would like to create an interactive map where the user could enter the cause of deaths for example HIV or Fire, so that the map could display it. I have tried to use an input but it doesn't work and shows me this error message : "Error in : Discrete value supplied to continuous scale". I know that the cause of this issue is the fact that the parameter size is a int but the input a char.
Unfortunalty I don't know how to correct this issue :(

Can anyone help me with it?
Have a nice day,
AM.

library("maptools")
library(rgdal)
library(maps)
library("jsonlite")
library(RColorBrewer)
library(ggmap)

rowdb <- read.csv("annual-number-of-deaths-by-cause.csv", sep = ';')
rowdb <- select(rowdb, -X.1, -X)
        
europe <- c(left = -12, bottom = 35, right = 30, top = 63)
map_E <- get_stamenmap(europe, zoom = 5,"toner-lite")
ggmap(map_E)
    
#On va cr?er une variable contenant les noms des pays du continent puis pour une date donn?e
#qu'on pourra aussi demander ? l'utilisateur de s?lectionner on affichera la carte du 
#continent et la cause choisies
Europe_E <- c("Albania", "Andorra", "Armenia", "Austria","Azerbaijan","Belarus",
                  "Belgium", "Bosnia and Herzegovina", "Bulgaria", "Croatia","Cyprus",
                  "Czechia", "Denmark", "Estonia", "Finland", "France", "Georgia",
                  "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Italy", 
                  "Kazakhstan","Latvia", "Lithuania", "Luxembourg", "Malta", "Moldova", "Monaco",
                  "Montenegro", "Netherlands","North Macedonia", "Norway", "Poland", "Portugal",
                  "Romania", "Russia", "San Marino","Serbia", "Slovakia", "Slovenia", "Spain", 
                  "Sweden", "Switzerland", "Turkey", "Ukraine", "United Kingdom")
    
    #Fonction pour avoir la latitude et longitude des pays
    if (!(require(jsonlite))) install.packages("jsonlite")
    mygeocode <- function(adresses){
      # adresses est un vecteur contenant toutes les adresses sous forme de chaine de caracteres
      nominatim_osm <- function(address = NULL){
        ## details: http://wiki.openstreetmap.org/wiki/Nominatim
        ## fonction nominatim_osm propos?e par D.Kisler
        if(suppressWarnings(is.null(address)))  return(data.frame())
        tryCatch(
          d <- jsonlite::fromJSON(
            gsub('\\@addr\\@', gsub('\\s+', '\\%20', address),
                 'http://nominatim.openstreetmap.org/search/@addr@?format=json&addressdetails=0&limit=1')
          ), error = function(c) return(data.frame())
        )
        if(length(d) == 0) return(data.frame())
        return(c(as.numeric(d$lon), as.numeric(d$lat)))
      }
      tableau <- t(sapply(adresses,nominatim_osm))
      colnames(tableau) <- c("lon","lat")
      return(tableau)
    }

        latlon_E <- mygeocode(Europe_E)
        #is(lalo)
        dat_E <- as.data.frame(latlon_E)
        DAT_E <- rowdb
        DAT_E$Year <- year(as.Date(DAT_E$Year, format = "%Y"))
        DAT_E <- DAT_E[
          as.character(DAT_E$Year) %in% c("2019"), ]
        DAT_E <- DAT_E[
          DAT_E$Entity %in% Europe_E, ]
        
        ggmap(map_E, extent = "device") + 
          geom_point(data = DAT_E, aes(x = dat_E$lon , y =dat_E$lat, color = Europe_E, size = Drowning), alpha = 0.5) +
          geom_text(data = DAT_E, aes(x = dat_E$lon, y = dat_E$lat, label = Drowning), color = "black", size = 3.7) +
          scale_size_continuous(range = c(6, 12)) + theme(legend.position = "none")+
          facet_wrap(~ Year)
        

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文