0

I try for a few days to find a way to work with the sf package in R but with no success. I want to plot my region similarly to the example 2 from this example but without any points. I load my area as SpatialpolygonsDataFrame and then I use the fortify to get the lat and long as follow:

area<-readOGR(dsn="/home/ubuntu/..",layer="area")
f_area<-fortify(area)
head(f_area) 

long      lat order  hole piece id group
1 116.1045 57.23717     1 FALSE     1  0   0.1
2 116.1551 57.21548     2 FALSE     1  0   0.1
3 116.2420 57.14505     3 FALSE     1  0   0.1
4 116.1706 57.12011     4 FALSE     1  0   0.1
5 116.1222 57.12006     5 FALSE     1  0   0.1
6 116.0756 57.09926     6 FALSE     1  0   0.1

From this point I am very confused about what I have to do to get the result that I want.

Thank you for any help.

geo_dd
  • 283
  • 1
  • 5
  • 22

1 Answers1

0

With ggplot2::geom_sf, there is no need to fortify.

# install ggplot2 development version
install.packages('devtools')
devtools::install_github('tidyverse/ggplot2')

# load libraries
library(sf)
library(ggplot2)

# read in data (probably you need to change the path here to read your file)
area <- sf::st_read("/home/ubuntu/area.shp")

# ggplot
ggplot() + geom_sf(area)

# or you can simply use plot
plot(st_geometry(area))
sebdalgarno
  • 2,929
  • 12
  • 28
  • unfortunately, your solution did not work for me as I the geom_sf can not be found. – geo_dd Jun 18 '18 at 06:42
  • 1
    you need to install the development version of ggplot2 from GitHub. please see my updated solution. – sebdalgarno Jun 18 '18 at 06:47
  • @sebdalgano, still the same problem. I will do some digging, because if this works for you, it means that the code is ok. I am on ubuntu and I might need to install more packages. I will let you know. – geo_dd Jun 18 '18 at 07:42
  • did you restart R after reinstalling ggplot2? There are a number of other SO questions already answered... https://stackoverflow.com/questions/46817128/error-could-not-find-function-geom-sf?rq=1 and https://stackoverflow.com/questions/46185226/error-when-plotting-sf-object-error-could-not-find-function-geom-sf/46185290 – sebdalgarno Jun 18 '18 at 15:48