10

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.

0

1 Answer 1

11

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

2
  • Thank you! So if I want the map to stay on my final, compiled output, I should leave out dev.off() altogether? Or should I clear the device after I graph all my maps?
    – Anshu Chen
    May 5, 2015 at 0:57
  • @Anshu: Compiled how? In a knitr document? You typically only need to use dev.off() when you are writing plots to a png/tiff/etc. file, in which case dev.off() instructs R to stop sending plotting commands to that file and close the connection.
    – jbaums
    May 5, 2015 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.