Search code examples
rrworldmap

How to overlay two plots using rworldmap?


Hi I want to overlay two world map plots using rworldmap, but the blow code does not work, it only shows map2. Can anyone help me with this please?

library(rworldmap)
data(countryExData)

sPDF <- joinCountryData2Map(countryExData
                             , joinCode="ISO3"
                             , nameJoinColumn="ISO3V10")

#map1
mapDevice()
map1 <- mapCountryData(sPDF, nameColumnToPlot='BIODIVERSITY')

#map2
map1 + mapBubbles(sPDF, nameZSize="ECOSYSTEM")

Solution

  • You can call par(new = TRUE) after plotting the first map. The second map will then be plotted on top. For details see documentation.

    Updated code:

    library(rworldmap)
    data(countryExData)
    
    sPDF <- joinCountryData2Map(countryExData,
                                joinCode = "ISO3",
                                nameJoinColumn = "ISO3V10")
    
    #map1
    mapDevice()
    mapCountryData(sPDF, nameColumnToPlot = 'BIODIVERSITY')
    
    # draw next frame as if it were on a new device
    par(new=TRUE)
    
    #map2
    mapBubbles(sPDF, nameZSize = "ECOSYSTEM")