5

我正在用这个rworldmap包制作世界地图。我正在使用一个函数来访问来自 UN Comtrade 的贸易数据。

我编辑了我原来的问题,所以我可以展示一个真实的例子来说明我在做什么。这是我可以制作的地图:

功能

library(rjson)
library(rworldmap)

get.Comtrade <- function(url="http://comtrade.un.org/api/get?", maxrec=50000,
                         type="C", freq="A", px="HS", ps="now", r, p, rg="all",
                         cc="TOTAL", fmt="json") {
  string <- paste(url
                  , "max=", maxrec,"&"  # maximum no. of records returned
                  , "type=", type, "&"  # type of trade (c=commodities)
                  , "freq=", freq, "&"  # frequency
                  , "px=", px, "&"      # classification
                  , "ps=", ps, "&"      # time period
                  , "r=", r, "&"        # reporting area
                  , "p=", p, "&"        # partner country
                  , "rg=", rg, "&"      # trade flow
                  , "cc=", cc, "&"      # classification code
                  , "fmt=", fmt         # Format
                  , sep="")

  if (fmt == "csv") {
    raw.data <- read.csv(string,header=TRUE)
    return(list(validation=NULL, data=raw.data))
  } else {
    if (fmt == "json" ) {
      raw.data <- fromJSON(file=string)
      data <- raw.data$dataset
      validation <- unlist(raw.data$validation, recursive=TRUE)
      ndata <- NULL
      if (length(data) > 0) {
        var.names <- names(data[[1]])
        data <- as.data.frame(t(sapply(data,rbind)))
        ndata <- NULL
        for (i in 1:ncol(data)) {
          data[sapply(data[, i], is.null), i] <- NA
          ndata <- cbind(ndata, unlist(data[, i]))
        }
        ndata <- as.data.frame(ndata)
        colnames(ndata) <- var.names
      }
      return(list(validation=validation, data=ndata))
    }
  }
}

用法

dt2 <- get.Comtrade(r=32, p="all", rg=1, fmt="csv")
dt2df <- as.data.frame(do.call(rbind, dt2))
total <- sum(dt2df$Trade.Value..US..)
dt2df$p <- 100*dt2df$Trade.Value..US../total
dt2df <- dt2df[order(-dt2df[, "p"]), ]

top3 <- dt2df[4, "p"]
top10 <- dt2df[11, "p"]
q3 <- dt2df[as.integer(1*nrow(dt2df)/4), "p"]
q2 <- dt2df[as.integer(2*nrow(dt2df)/4), "p"]
q1 <- dt2df[as.integer(3*nrow(dt2df)/4), "p"]

mapped_data <- joinCountryData2Map(dt2df, joinCode="ISO3", 
                                   nameJoinColumn="Partner.ISO")

mapCountryData(mapped_data, nameColumnToPlot="p", numCats=6, 
               catMethod=c(0, q1, q2, q3, top10, top3, 100), 
               colourPalette=c('cornsilk', 'cornsilk2', 'palegreen1', 
                               'palegreen2', 'palegreen4', 'darkgreen'), 
               mapTitle="", addLegend=FALSE)

结果就是我要找的地图,除了我不需要去看南极洲。我怎样才能删除它?

我尝试使用xlim& ylim,但没有成功。

4

1 回答 1

6

尝试 new_world <- subset(mapped_data, continent != "Antarctica")

mapped_data <- joinCountryData2Map(dt2df, joinCode = "ISO3", nameJoinColumn = "Partner.ISO")

然后继续

mapCountryData(new_world, nameColumnToPlot = "p", numCats=6, catMethod = 
c(0,q1,q2,q3,top10,top3,100), colourPalette =     c('cornsilk','cornsilk2','palegreen1','palegreen2','palegreen4','darkgreen'), mapTitle="", addLegend=FALSE)
于 2017-10-09T16:09:16.193 回答