4

I'm trying to load and plot a layer from a simple shapefile. It is state boundaries in the US. I can load it fine:

> library("sp","rgdal")

> shape = readOGR("/home/username/data/share/mapnik/world_boundaries/states_EPSG4326.shp", layer="states_EPSG4326")

    OGR data source with driver: ESRI Shapefile 
    Source: "/home/username/data/share/mapnik/world_boundaries/states_EPSG4326.shp", layer:       "states_EPSG4326"
    with 2895 features and 9 fields
    Feature type: wkbPolygon with 2 dimensions

But it fails to plot:

> plot(shape)

There were 50 or more warnings (use warnings() to see the first 50)
There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: Path drawing not available for this device
2: Path drawing not available for this device
3: Path drawing not available for this device
...

What am I missing? Nothing shows up in the plot window. There is only the single layer in this shapefile. I could plot it in qgis, which I am trying to find an alternative for. R seems like a popular choice, but I'm not having much luck so far. I googled this warning, but did not turn up anything useful.

Edit: This same response is received with every shape file I have tried. Here is a shapefile that I have verified responds the same way:

http://dds.cr.usgs.gov/pub/data/nationalatlas/statep010_nt00798.tar.gz

Here is the output of sessionInfo():

> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C         LC_TIME=C           
 [4] LC_COLLATE=C         LC_MONETARY=C        LC_MESSAGES=C       
 [7] LC_PAPER=C           LC_NAME=C            LC_ADDRESS=C        
[10] LC_TELEPHONE=C       LC_MEASUREMENT=C     LC_IDENTIFICATION=C 

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rgdal_0.7-22 sp_1.0-2    

loaded via a namespace (and not attached):
[1] grid_2.15.2     lattice_0.20-10 tools_2.15.2 

Also this is a gentoo system, the following use flags are enabled on dev-lang/R:

  • X icu java jpeg nls openmp png readline tiff

The following use flags are not enabled (capabilities not compiled):

  • -bash-completion -cairo -doc -lapack -minimal -perl -profile -static-libs -tk"

The only two that look suspicious to me are cairo and tk, are they needed for plotting?

I ran R from the terminal with similiar, but not exactly the same results:

> shape = readOGR("/home/username/Downloads/state/statep010.shp", layer="statep010")
OGR data source with driver: ESRI Shapefile 
Source: "/home/username/Downloads/state/statep010.shp", layer: "statep010"
with 62 features and 9 fields
Feature type: wkbPolygon with 2 dimensions
> plot(shape)
There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In polypath(x = mcrds[, 1], y = mcrds[, 2], border = border,  ... :
  Path drawing not available for this device
2: In polypath(x = mcrds[, 1], y = mcrds[, 2], border = border,  ... :
  Path drawing not available for this device
3: In polypath(x = mcrds[, 1], y = mcrds[, 2], border = border,  ... :
  Path drawing not available for this device
...
Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
nomadicME
  • 1,389
  • 5
  • 15
  • 35
  • It's difficult to say without being able to reproduce your problem. Can you share the data somehow, such as via Dropbox or a file sharing service? – SlowLearner Dec 02 '12 at 00:30
  • @SlowLearner, thanks I edited the original question with a link to a shape file, which cannot be plotted here. – nomadicME Dec 02 '12 at 00:46
  • @nomadicME I test your shape file under windows . it works and I have the map. – agstudy Dec 02 '12 at 00:54
  • @nomadicME under Unix too it works! – agstudy Dec 02 '12 at 01:04
  • @nomadicME I have downloaded the shape file and with your code `plot` gives the output one would expect. Please add the output of `sessionInfo()` to your question; that may contain a clue. – SlowLearner Dec 02 '12 at 01:28
  • 1
    works on my System (Ubuntu 64bit). Have you tried to run from command line? – EDi Dec 02 '12 at 01:31
  • @SlowLearner, I've provided the output of sessionInfo() in the original question. Thanks. – nomadicME Dec 02 '12 at 01:45
  • @EDi, I tried it from the R command line (outside rstudio), and the results were similiar, but not exactly the same. I placed the output in the original question. Is cairo or tk compiled into R required to plot? Thanks. – nomadicME Dec 02 '12 at 01:53
  • @nomadicME Your installation looks OK and up to date. Does `plot(shape, usePolypath = FALSE)` in RGui (the command line) make any difference? – SlowLearner Dec 02 '12 at 02:13
  • @SlowLearner, that did it. Thanks. That wasn't too bad for a first plot in R. :) Any idea why I need to add that switch when everyone seems to work fine with the defaults? – nomadicME Dec 02 '12 at 02:58
  • @nomadicME It has to do with the interaction between X11 and the `sp` library when drawing polygons. – SlowLearner Dec 02 '12 at 05:34

3 Answers3

3

Use plot(shape, usePolypath = FALSE) as described in this thread by Roger Bivand.

SlowLearner
  • 7,907
  • 11
  • 49
  • 80
  • 1
    As stated in the link you provided, if I issue this command: `set_Polypath(FALSE)` then I can go ahead an use `plot(shape)` which is much more convenient. – nomadicME Dec 02 '12 at 08:01
2

If you are interested in plotting administrative boundaries only, can't you just use data sources provided by some R packages?

maps provide global administrative areas. You can retrieve country boundaries as well. See the example used with ggplot here.

dismo provide data from GADM database. You can retrive data by country and level.

library(maps)
library(raster)

admlim <-getData('GADM', country='USA', level=1)
proj4string(admlim) <- CRS('+init=epsg:4326')
# plot(admlim) # plain plot
# dataframe for ggplot()
# This will take a long, long time for USA level 1 data from GADM
t.adm <- fortify(admlim)
ggplot(aes(long, lat, group = group), data = t.adm,
  colour = 'grey20', fill = NA) +
  geom_path() +
  scale_y_continuous(breaks=(-2:2) * 30) +
  scale_x_continuous(breaks=(-4:4) * 45) +
  coord_map('ortho', orientation=c(41, -74, 0))

NY centered map of usa

epo3
  • 2,991
  • 2
  • 33
  • 60
Paulo E. Cardoso
  • 5,778
  • 32
  • 42
1

I had the same problem and could solve it by compiling R again after installing libcairo2-dev and libpango1.0-dev

On debian:

sudo apt install libcairo2-dev
sudo apt install libpango1.0-dev

I am sure that in particular libpango1.0-dev was missing before. Then I used

./configure --prefix=/.../.../R --with-cairo
make && make install

and that fixed it.

Hope it helps.

ze miguel
  • 306
  • 3
  • 9
  • You are great! Missing pango was the reason. Make absolutely sure you have `pango-devel` installed before compiling R. Also the `./configure` results of R will tell you nothing whether pango is available (only cairo is there). Thank you so much for this answer! – robertspierre Jun 23 '21 at 17:12