1

I've tried to read in my shape file and then plot it but it seems as though RStudio is stuck on something and will not finish running the plot function. Right now I have:

library(rgdal)
new_county_path <- paste(county_path, "tl_2014_us_county.shp",sep='/')
county1 <- readOGR(new_county_path)
plot(county1)

But it doesn't produce a plot and it seems to be continuously be stuck on something as it only says

>plot(county1)

in the R console. Am I doing something wrong to cause this and is there a better way to read and plot shapefiles?

Buchlord
  • 11
  • 2
  • Did you look at county1 to see that it looks OK? E.g. `str(county1)` – G5W Jan 09 '21 at 01:34
  • Do you have another graphic "device" active? In the past, I've had a failed render leave a `pdf` or `png` graphic sitting clogging the works. What does `dev.list()` display? I'm on windows, and *not* in RStudio I just see `windows\n2` (one device); in *yes* in RStudio, I see `RStudioGD\n2` and `png\n3` (two devices), which is normal in RStudio. – r2evans Jan 09 '21 at 02:36
  • It's a spatialpolygondataframe which feels right – Buchlord Jan 09 '21 at 02:51

1 Answers1

2

Without a reproducible example, I can't say why your code doesn't work. If you have a valid shapefile, you should be able to read it in and plot it using the code that you provided:

# first get and save a shapefile to make the code easily reproducible
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
st_write(obj = nc, dsn = 'test/nc.shp')
# now there's a shapefile named "nc.shp" saved in the "test" folder.

# the functions you're using will work on a valid path to a valid shapefile:
library(rgdal)
nc1 <- readOGR("test/nc.shp")
plot(nc1)

I typically use the sf package, which provides more versatility (particularly for plotting):

library(sf)
nc2 <- st_read("test/nc.shp")
plot(st_geometry(nc2)) 

If your code doesn't work, there's likely a problem with either the path you provided to the readOGR function or with the shapefile itself.

filups21
  • 1,611
  • 1
  • 19
  • 22