r - ggplot2 与 rworldmap 结合

标签 r ggplot2 grid-layout cowplot rworldmap

我做了以下rworldplot :

library(rworldmap)
#> Loading required package: sp
#> ### Welcome to rworldmap ###
#> For a short introduction type :   vignette('rworldmap')
library(RColorBrewer)
test.data <- data.frame(
  country = c('USA','Denmark','Australia','Germany'),
  value = c(4000,1000,2300,100)
)
sPDF <- joinCountryData2Map(test.data,joinCode = "NAME",nameJoinColumn = "country")
#> 4 codes from your data successfully matched countries in the map
#> 0 codes from your data failed to match with a country code in the map
#> 239 codes from the map weren't represented in your data
colourPalette <- brewer.pal(4,'Reds')
mapParams <- mapCountryData(sPDF, nameColumnToPlot="value", colourPalette=colourPalette, catMethod=c(0,500,1000,2300,5000), addLegend=FALSE, mapTitle = 'TEST')
do.call( addMapLegend, c( mapParams
                          , legendLabels="all"
                          , legendWidth=0.5 ))

为了清楚起见,我还想添加一个条形图:

library(ggplot2)
barplot <- ggplot(test.data, aes(x=reorder(country,-value), y = value)) +
  geom_bar(stat='identity', fill = 'darkred') + 
  theme_classic()
barplot

理想情况下,我想使用例如将 rworldmap 图和 ggplot 组合在一个图中。 grid.arrangecowplot包裹。我知道这对于不同的人来说很容易ggplot然而反对do.call直接绘制 rworldmap,因此我不知道如何将其变为 grid.arrange(<rworlddmapplot>, barplot) 这样的字。

最佳答案

cowplot 中的 plot_grid() 函数可以采用函数参数来绘制您想要显示的图。

library(rworldmap)
#> Loading required package: sp
#> ### Welcome to rworldmap ###
#> For a short introduction type :   vignette('rworldmap')
library(RColorBrewer)

test.data <- data.frame(
  country = c('USA','Denmark','Australia','Germany'),
  value = c(4000,1000,2300,100)
)

draw_map <- function(test.data) {
  function() {
    sPDF <- joinCountryData2Map(test.data,joinCode = "NAME",nameJoinColumn = "country")
    colourPalette <- brewer.pal(4,'Reds')
    mapParams <- mapCountryData(sPDF, nameColumnToPlot="value", colourPalette=colourPalette, catMethod=c(0,500,1000,2300,5000), addLegend=FALSE, mapTitle = 'TEST')
    do.call( addMapLegend, c( mapParams
                          , legendLabels="all"
                          , legendWidth=0.5 ))
  }
}

library(ggplot2)
barplot <- ggplot(test.data, aes(x=reorder(country,-value), y = value)) +
  geom_bar(stat='identity', fill = 'darkred') + 
  theme_classic()

library(cowplot)
#> 
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#>   default ggplot2 theme anymore. To recover the previous
#>   behavior, execute:
#>   theme_set(theme_cowplot())
#> ********************************************************

plot_grid(barplot, draw_map(test.data))
#> 4 codes from your data successfully matched countries in the map
#> 0 codes from your data failed to match with a country code in the map
#> 239 codes from the map weren't represented in your data

reprex package于2019年8月26日创建(v0.3.0)

关于r - ggplot2 与 rworldmap 结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57660158/

相关文章:

r - 链接 R 中同一时间窗口内发生的值

将数值替换为组最小值

r - 如何使用ggplot2制作弯曲的雷达图?

r - 是否存在一种算法可以使用大量变量找到最佳 Logit 模型?

r - 使用纬度和经度向量查找 R 中的步行距离

r - GGPlot2 中带有子组的森林图

r - 从geom_boxplot()获取分位数值

android - IntelliJ 和 android.support.v7.widget.GridLayout

java - 使用GridLayout创建战舰程序时遇到问题

css - Twitter Bootstrap 无法用固定网格填充容器