Graphic not appearing in R: null device?

Anshu says Reinstate Monica picture Anshu says Reinstate Monica · May 5, 2015 · Viewed 21.8k times · Source

I'm trying to produce a graphic using rworldmap, using my data frame dagg. ETA: data. Here's what I have so far.

library(rworldmap)
data(dagg)
sPDF <- joinCountryData2Map(dagg, joinCode='ISO2',
nameJoinColumn='country', verbose='TRUE')

mapDevice()

mapCountryData(sPDF, nameColumnToPlot='avoidance', 
numCats=10, mapTitle="Avoidance", addLegend=TRUE)
dev.off()

But when I run, nothing shows up. The console displays "null device 1". It was working just now, and I'm not sure what could have changed it...

Perhaps I'm not using the right device?

ETA: Here's the rundown on rworldmap that I'm reading from.

Answer

jbaums picture jbaums · May 5, 2015

dev.off() closes the currently active graphics device, so if you are running all of that code at once, the map will be plotted and then almost immediately disappear, with the typical output:

## null device 
##           1 

Running the following, which excludes dev.off(), should produce the map you're expecting.

library(rworldmap)
dagg <- read.csv(
  'http://raw.githubusercontent.com/pourque/country-data/master/data/dagg.csv')
sPDF <- joinCountryData2Map(dagg, joinCode='ISO2',
                            nameJoinColumn='country', verbose='TRUE')

mapDevice()   
mapCountryData(sPDF, nameColumnToPlot='avoidance', 
               numCats=10, mapTitle="Avoidance", addLegend=TRUE)

enter image description here