r - ggplot2 结合 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
理想情况下,我想使用 eggrid.arrange
或cowplot
包将 rworldmap 图和 ggplot 组合在一个图中。我知道这对于不同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 包(v0.3.0)于 2019 年 8 月 26 日创建
推荐阅读
- unit-testing - 在 Spock 中使用模拟单元测试流畅的 API
- kubernetes - 是否可以将新 pod 动态添加到现有部署中?
- javascript - Nightwatch 变量范围和访问外部 .element 调用
- r - 您如何在 R Studio 中查看查看器项目?
- reactjs - 正确处理麦克风音频的 React Hooks
- python-3.x - 逐行从数据帧中读取数据
- bash - 将多个文本文件的第一行复制到单个文件中
- javascript - Vuex:从不同模块获取器中的另一个模块访问状态
- python - 循环所有列以获取任何列中的值
- android - MutableLiveData 通过从 ViewModel 转换 LiveData 来暴露