与R的世界地图?

我有一个数组,告诉我每个国家的观察次数。

countries <- structure(c(532L, 3L, 1L, 15L, 1L, 1L, 2L, 3L, 16L, 2L, 43L, 
1L, 2L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 4L, 4L, 16L, 13L, 2L, 2L, 
9L, 1L, 1L, 5L, 3L, 5L, 1L, 1L, 3L, 1L, 10L, 11L, 4L, 2L, 1L, 
7L, 1L, 2L, 6L, 7L, 1L, 6L, 1L, 2L, 7L, 1L, 20L, 1L, 2L, 1L, 
3L, 2L, 5L, 76L, 2L, 1L, 1L), .Dim = 63L, .Dimnames = structure(list(
    c("United States", "Argentina", "Armenia", "Australia", "Austria", 
    "Bangladesh", "Belarus", "Belgium", "Brazil", "Bulgaria", 
    "Canada", "Chile", "China", "Colombia", "Croatia", "Cuba", 
    "Cyprus", "Czech Republic", "Dominican Republic", "Ecuador", 
    "Estonia", "France", "Germany", "Greece", "Guatemala", "Hong Kong", 
    "India", "Indonesia", "Iran", "Ireland", "Israel", "Italy", 
    "Kazakhstan", "Kenya", "Latvia", "Malaysia", "Mexico", "Netherlands", 
    "New Zealand", "Norway", "Peru", "Philippines", "Poland", 
    "Portugal", "Romania", "Russia", "Saudi Arabia", "Serbia", 
    "Singapore", "Slovakia", "South Africa", "South Korea", "Spain", 
    "Sri Lanka", "Sweden", "Switzerland", "Thailand", "Turkey", 
    "Ukraine", "United Kingdom", "Uruguay", "Uzbekistan", "Venezuela"
    )), .Names = ""))

我可以使用地图库绘制地图。 但我会很感激帮助它看起来更好。

library(maps)
map(database="world")
map(database="world", col=countries, fil=countries)
legend("topleft", fill = countries, legend = countries, col = countries)
box()

第一个大问题是传说。 一个连续的规模可能会看起来比每个国家的一种颜色更好,不知道该怎么做。 修好后,任何可以做的事情,使它看起来更好,将不胜感激。

谢谢!


我可以使用googleVis制作动态地图,但我在使用ggplot2制作静态地图时遇到麻烦。 例如,对于ggplot2,看起来我在美国没有人。

这是我的代码

#Load My data
countries <- structure(list(country = c("United States", "Afghanistan", "Albania", 
                                        "Argentina", "Armenia", "Australia", "Austria", "Bahrain", "Bangladesh", 
                                        "Belarus", "Belgium", "Bosnia and Herzegovina", "Brazil", "Bulgaria", 
                                        "Canada", "Chile", "China", "Colombia", "Croatia", "Cuba", "Cyprus", 
                                        "Czech Republic", "Denmark", "Dominican Republic", "Ecuador", 
                                        "Egypt", "El Salvador", "Estonia", "Finland", "France", "Germany", 
                                        "Greece", "Guatemala", "Haiti", "Hong Kong", "Hungary", "Iceland", 
                                        "India", "Indonesia", "Iran", "Ireland", "Israel", "Italy", "Japan", 
                                        "Jordan", "Kazakhstan", "Kenya", "Korea, South", "Latvia", "Libya", 
                                        "Lithuania", "Macedonia", "Malaysia", "Malta", "Mexico", "Moldova", 
                                        "Morocco", "Netherlands", "New Zealand", "Nicaragua", "Niger", 
                                        "Nigeria", "Norway", "Pakistan", "Panama", "Peru", "Philippines", 
                                        "Poland", "Portugal", "Romania", "Russia", "Saudi Arabia", "Serbia", 
                                        "Singapore", "Slovakia", "Slovenia", "Somalia", "South Africa", 
                                        "South Korea", "Spain", "Sri Lanka", "Sweden", "Switzerland", 
                                        "Taiwan", "Thailand", "Turkey", "Ukraine", "United Arab Emirates", 
                                        "United Kingdom", "Uruguay", "Uzbekistan", "Venezuela", "Zimbabwe"
), count = c(1224L, 1L, 1L, 4L, 2L, 40L, 2L, 1L, 2L, 5L, 8L, 
             2L, 40L, 3L, 106L, 4L, 16L, 10L, 8L, 4L, 2L, 5L, 4L, 5L, 3L, 
             1L, 2L, 5L, 1L, 10L, 26L, 41L, 3L, 1L, 3L, 2L, 1L, 34L, 2L, 3L, 
             10L, 4L, 19L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 3L, 1L, 2L, 2L, 36L, 
             1L, 1L, 31L, 10L, 1L, 1L, 1L, 2L, 6L, 2L, 3L, 29L, 7L, 11L, 13L, 
             21L, 5L, 9L, 6L, 3L, 2L, 1L, 22L, 2L, 42L, 1L, 3L, 5L, 2L, 6L, 
             5L, 13L, 2L, 157L, 4L, 1L, 5L, 1L)), .Names = c("country", "count"
             ), row.names = c(NA, -93L), class = "data.frame")

#Make dynamic map
library(googleVis)
# Make the map!
geoMap <- gvisGeoMap(countries, locationvar="country", numvar="count",
                     options=list(dataMode="regions"))
plot(geoMap)

#Make ggplot2 map
library(maps)
library(ggplot2)
#load world data
world <- map_data("world")

#Delete Antarctica
world <- subset(world,region!="Antarctica")
#Add count
world$count<-countries$count[match(world$region,countries$country,nomatch=NA)]
qplot(long, lat, data = world, group = group, fill=count, geom ="polygon",ylab="",xlab="")

为什么ggplot2映射错误? 我该如何解决它?

谢谢!


这可能不是您想要的,但这里是使用googleVis包的解决方案。

# I had to change your data a little bit
countries2 <- data.frame(country=names(countries), count=as.integer(countries), 
                         stringsAsFactors=FALSE)

# Install the googleVis package and load it
# install.packages("googleVis")
library(googleVis)

# Make the map!
geoMap <- gvisGeoMap(countries2, locationvar="country", numvar="count",
                 options=list(dataMode="regions"))
plot(geoMap)

这将为您的数据创建一个交互式地理地图,并且当您将鼠标悬停在不同区域时,应该突出显示它并显示计数弹出窗口。

(我的道歉 - 这个问题只是试用这个软件包的借口:)。 )

如果你想要一个静态阴谋,我可以尝试做出这一点。


这是我的解决方案。

#Load My data
countries <- structure(list(country = c("United States", "Afghanistan", "Albania", 
                                        "Argentina", "Armenia", "Australia", "Austria", "Bahrain", "Bangladesh", 
                                        "Belarus", "Belgium", "Bosnia and Herzegovina", "Brazil", "Bulgaria", 
                                        "Canada", "Chile", "China", "Colombia", "Croatia", "Cuba", "Cyprus", 
                                        "Czech Republic", "Denmark", "Dominican Republic", "Ecuador", 
                                        "Egypt", "El Salvador", "Estonia", "Finland", "France", "Germany", 
                                        "Greece", "Guatemala", "Haiti", "Hong Kong", "Hungary", "Iceland", 
                                        "India", "Indonesia", "Iran", "Ireland", "Israel", "Italy", "Japan", 
                                        "Jordan", "Kazakhstan", "Kenya", "Korea, South", "Latvia", "Libya", 
                                        "Lithuania", "Macedonia", "Malaysia", "Malta", "Mexico", "Moldova", 
                                        "Morocco", "Netherlands", "New Zealand", "Nicaragua", "Niger", 
                                        "Nigeria", "Norway", "Pakistan", "Panama", "Peru", "Philippines", 
                                        "Poland", "Portugal", "Romania", "Russia", "Saudi Arabia", "Serbia", 
                                        "Singapore", "Slovakia", "Slovenia", "Somalia", "South Africa", 
                                        "South Korea", "Spain", "Sri Lanka", "Sweden", "Switzerland", 
                                        "Taiwan", "Thailand", "Turkey", "Ukraine", "United Arab Emirates", 
                                        "United Kingdom", "Uruguay", "Uzbekistan", "Venezuela", "Zimbabwe"
), count = c(1224L, 1L, 1L, 4L, 2L, 40L, 2L, 1L, 2L, 5L, 8L, 
             2L, 40L, 3L, 106L, 4L, 16L, 10L, 8L, 4L, 2L, 5L, 4L, 5L, 3L, 
             1L, 2L, 5L, 1L, 10L, 26L, 41L, 3L, 1L, 3L, 2L, 1L, 34L, 2L, 3L, 
             10L, 4L, 19L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 3L, 1L, 2L, 2L, 36L, 
             1L, 1L, 31L, 10L, 1L, 1L, 1L, 2L, 6L, 2L, 3L, 29L, 7L, 11L, 13L, 
             21L, 5L, 9L, 6L, 3L, 2L, 1L, 22L, 2L, 42L, 1L, 3L, 5L, 2L, 6L, 
             5L, 13L, 2L, 157L, 4L, 1L, 5L, 1L)), .Names = c("country", "count"
             ), row.names = c(NA, -93L), class = "data.frame")

suppressPackageStartupMessages({
  library(maptools)
  library(ggplot2)
})



PolygonCoords <- function(polygon) {
  polygons <- polygon@Polygons
  coords.list <- lapply(seq_along(polygons), function(i) {
    # Extract the group, sequence, area, longitude, and latitude.
    coords <- polygons[[i]]@coords
    cbind(i, 1:nrow(coords), polygons[[i]]@area, coords)
  })
  coords.df <- as.data.frame(do.call(rbind, coords.list))
  names(coords.df) <- c("order", "seq", "area", "long", "lat")
  return(coords.df)
}

ConvertWorldSimple <- function(mapdata, min.area = 0) {

  # min.area is the minimum size of the polygons. Setting to some
  # positive value will filter out tiny islands.


  coords.list <- lapply(mapdata@polygons, PolygonCoords)
  ncoords <- sapply(coords.list, nrow)
  coords.df <- do.call(rbind, coords.list)
  coords.df$country <- rep(mapdata@data$NAME, ncoords)
  country.group <- factor(paste(coords.df$country, coords.df$order))
  coords.df$group <- as.numeric(country.group)
  coords.df <- coords.df[coords.df$area >= min.area, ]
  return(coords.df)
}

data("wrld_simpl")
world <- ConvertWorldSimple(wrld_simpl, min.area = 0.1)
#Delete Antarctica
world <- subset(world,country!="Antarctica")

#Add count
world$count<-countries$count[match(world$country,countries$country,nomatch=NA)]
x<-quantile(world$count, na.rm=TRUE)
qplot(long, lat, data = world, group = group, fill=count, geom ="polygon",ylab="",xlab="") + 
  scale_fill_gradient(name="log(Number ofnStudents)", trans = "log")

谢谢您的帮助!

链接地址: http://www.djcxy.com/p/75545.html

上一篇: World map with R?

下一篇: Avoid word break while using vw for font size