R:在同一个世界地图上绘制两个不同的数据集

时间:2018-01-17 20:07:52

标签: r

我是R.的新手。目前我正在尝试在世界地图上绘制两个不同的数据集。我正在使用包rworldmap。 我的数据:

> dput(array_with_data1)
structure(list(Country = structure(c(35L, 7L, 57L, 42L, 55L, 
3L, 12L, 2L, 58L, 63L, 19L, 15L, 10L, 45L, 30L, 70L, 14L, 6L, 
38L, 43L, 9L, 54L, 68L, 31L, 22L, 36L, 25L, 61L, 16L, 23L, 69L, 
51L, 20L, 62L, 17L, 37L, 39L, 56L, 59L, 26L, 27L, 50L, 71L, 48L, 
60L, 24L, 44L, 66L, 46L, 67L, 52L, 32L, 21L, 49L, 72L, 13L, 5L, 
11L, 34L, 1L, 40L, 8L, 65L, 53L, 64L, 28L, 33L, 18L, 4L, 41L, 
47L, 29L), .Label = c("Argentina", "Australia", "Austria", "Bangladesh", 
"Belarus", "Belgium", "Bolivia", "Brazil", "Bulgaria", "Canada", 
"Chile", "China", "Colombia", "Croatia", "Cyprus", "Czechia", 
"Denmark", "Egypt", "Estonia", "Finland", "France", "Georgia", 
"Germany", "Greece", "Hong Kong", "Hungary", "India", "Indonesia", 
"Iran", "Ireland", "Israel", "Italy", "Japan", "Kazakhstan", 
"Kosovo", "Latvia", "Lebanon", "Lithuania", "Malaysia", "Mexico", 
"Morocco", "Netherlands", "New Zealand", "Nigeria", "Norway", 
"Pakistan", "Peru", "Philippines", "Poland", "Portugal", "Romania", 
"Russia", "Saudi Arabia", "Serbia", "Singapore", "Slovakia", 
"Slovenia", "South Africa", "South Korea", "Spain", "St. Helena", 
"Sweden", "Switzerland", "Taiwan", "Thailand", "Turkey", "Ukraine", 
"United Arab Emirates", "United Kingdom", "United States", "Venezuela", 
"Vietnam"), class = "factor"), `Variable1` = c(100, NA, 59, 
NA, 52, 37, 37, NA, 35, 34, 32, 31, 30, 30, 28, NA, 27, 26, 25, 
25, 25, 24, 24, 23, NA, 23, 23, 23, 22, 22, NA, 20, 20, 20, 19, 
NA, 19, 18, 16, 14, NA, 13, 12, 12, 12, 11, 11, 11, NA, 10, 10, 
10, 9, 8, 8, 8, 7, 7, 6, 5, 5, 5, 5, 4, 4, 4, NA, 3, NA, NA, 
3, 3)), .Names = c("Country", "Variable1"), row.names = c(NA, 
-72L), class = "data.frame")


> dput(array_with_data2)
structure(list(Country = structure(c(35L, 7L, 57L, 42L, 55L, 
3L, 12L, 2L, 58L, 63L, 19L, 15L, 10L, 45L, 30L, 70L, 14L, 6L, 
38L, 43L, 9L, 54L, 68L, 31L, 22L, 36L, 25L, 61L, 16L, 23L, 69L, 
51L, 20L, 62L, 17L, 37L, 39L, 56L, 59L, 26L, 27L, 50L, 71L, 48L, 
60L, 24L, 44L, 66L, 46L, 67L, 52L, 32L, 21L, 49L, 72L, 13L, 5L, 
11L, 34L, 1L, 40L, 8L, 65L, 53L, 64L, 28L, 33L, 18L, 4L, 41L, 
47L, 29L), .Label = c("Argentina", "Australia", "Austria", "Bangladesh", 
"Belarus", "Belgium", "Bolivia", "Brazil", "Bulgaria", "Canada", 
"Chile", "China", "Colombia", "Croatia", "Cyprus", "Czechia", 
"Denmark", "Egypt", "Estonia", "Finland", "France", "Georgia", 
"Germany", "Greece", "Hong Kong", "Hungary", "India", "Indonesia", 
"Iran", "Ireland", "Israel", "Italy", "Japan", "Kazakhstan", 
"Kosovo", "Latvia", "Lebanon", "Lithuania", "Malaysia", "Mexico", 
"Morocco", "Netherlands", "New Zealand", "Nigeria", "Norway", 
"Pakistan", "Peru", "Philippines", "Poland", "Portugal", "Romania", 
"Russia", "Saudi Arabia", "Serbia", "Singapore", "Slovakia", 
"Slovenia", "South Africa", "South Korea", "Spain", "St. Helena", 
"Sweden", "Switzerland", "Taiwan", "Thailand", "Turkey", "Ukraine", 
"United Arab Emirates", "United Kingdom", "United States", "Venezuela", 
"Vietnam"), class = "factor"), `Variable2` = c(NA, NA, NA, 
54, NA, NA, NA, 36, NA, NA, NA, NA, NA, NA, NA, 27, NA, NA, NA, 
NA, NA, NA, NA, NA, 23, NA, NA, NA, NA, NA, 21, NA, NA, NA, NA, 
19, NA, NA, NA, NA, 14, NA, NA, NA, NA, NA, NA, NA, 11, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 3, 
NA, NA, NA, NA, NA)), .Names = c("Country", "Variable2"), row.names = c(NA, 
-72L), class = "data.frame")

array_with_data1的类型是一个列表。第一列(国家/地区)包含国家/地区的名称。第二列(Variable1)包含我在地图上表示的数字变量,相同的颜色,不同的亮度,使用以下代码:

library(rworldmap)

plot_dataset <- function(array_with_data1){
    sPDF <- joinCountryData2Map(array_with_data1,
                          joinCode = "NAME",
                          nameJoinColumn = "Country",
                          mapResolution = "coarse"
    )
    mapParams <- mapCountryData( sPDF,
                           nameColumnToPlot= "Variable1",
                           addLegend=FALSE,
                           oceanCol="lightblue",
                           missingCountryCol=NA
    )
}

map <- plot_dataset(array_with_data1)

现在我想在同一个地图上添加另一个数据集(array_with_data2),使用不同的颜色覆盖第一个数据集。

0 个答案:

没有答案