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.

Community
  • 1
  • 1
Anshu Chen
  • 423
  • 2
  • 9
  • 23

1 Answers1

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

jbaums
  • 27,115
  • 5
  • 79
  • 119
  • 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 05 '15 at 00: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 05 '15 at 01:07