在 png 中导出使用 rworldmap 创建的地图

Export a Map created with rworldmap in png

我用包 rworldmap 在 R 上创建了一个地图,如下所示:

library(rworldmap)


fta.df <- data.frame(country = c("CAN", "MEX", "USA"),
                     country = c(1,2,3))

MyMap <- joinCountryData2Map(fta.df, joinCode = "ISO3",
                             nameJoinColumn = "country")
MyMap <- subset(MyMap, continent != "Antarctica")

FTA_map <- mapCountryData(MyMap, nameColumnToPlot="country", 
                          catMethod = "categorical",
                          missingCountryCol = gray(.8), 
                          colourPalette=c("forestgreen","royalblue1", "red3"),
                          mapTitle = "",
                          addLegend = FALSE)

FTA_map$legendText <- c("CAN", "MEX", "USA")            
do.call( addMapLegendBoxes, c(FTA_map,x="bottomleft",title = "title",horiz=FALSE))

我对结果很满意,但我不知道如何导出.png格式的图像。

有什么想法吗?

png("map.png",width=800,height=800,units="px")
MyMap <- subset(MyMap, continent != "Antarctica")

FTA_map <- mapCountryData(MyMap, nameColumnToPlot="country", 
                                                     catMethod = "categorical",
                                                     missingCountryCol = gray(.8), 

colourPalette=c("forestgreen","royalblue1", "red3"),
                                                     mapTitle = "",
                                                    addLegend = FALSE)

 FTA_map$legendText <- c("CAN", "MEX", "USA")            
 do.call( addMapLegendBoxes, c(FTA_map,x="bottomleft",title = "title",horiz=FALSE))
 dev.off()

看看这个:

https://www.rdocumentation.org/packages/grDevices/versions/3.4.1/topics/png

您可以通过更改 png() 函数的参数来解决问题