rpngrworldmap

Export a Map created with rworldmap in png


I created a map on R with the package rworldmap as follow:

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))

I am satisfied with the result but I do not know how to export the image obtained in .png.

Any Idea?


Solution

  • 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()
    

    Have a look at this :

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

    You can play with resolution by cahnging arguments of png() function