0

I am trying to create a simple map using ggplot2 to display a series of river basins and data points.

The data points are stored in a .csv file and look like this:

Points

I've projected the points and created a SpatialPointDataFrame and then stored them in a dataframe:

points <- read.csv(c(paste0("./Points/", "points.csv")), header = T, sep = ",")
coordinates(points) <- ~Lat + Lon
cord <- "+init=epsg:2163"
proj4string(points) <- CRS(cord)
points_df <- as.data.frame(points, region = "id")

The basin files are all stored in the same directory and have been read into a single spatialpolygonsdataframe which looks like this:

spatialpolygonsdataframe

then are converted to a dataframe using this:

basins_TX$id <- rownames(basins@data)
basins_TX_df <- tidy(basins, region = "id")

Then I try to create a map using this code:

ggplot() +
  geom_polygon(data = basins_df, 
               aes(x = long, y = lat, group = group), 
               fill = "white", color = "black") + 
  geom_point(data = points_df, 
             aes(x = Lon, y = Lat),
             color = "red")

this creates a map with polygons and one point at 0,0:

plot

This happens no matter what subsection of the data I am looking at. I want to accurately display the points and the polygons.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Jack_d
  • 37
  • 6
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 28 '18 at 21:21
  • 1
    Without seeing the data, can't say. Maybe wrong projection, since the basin `Longs` are in the 1,000,000s. – Anonymous coward Nov 28 '18 at 21:54
  • I tried to update the question to include more information/a reproducible example. I did not know how to share a sample of the basin shapefiles. does this make any more sense? – Jack_d Nov 28 '18 at 22:09
  • I also think it is probably a projection issue, but both should be in the same projection. crs(points) returns: CRS arguments: +init=epsg:2163 +proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs Which matches the spatialpolygonsdataframe in the question – Jack_d Nov 28 '18 at 22:11
  • You may want to double-check the dataframes passed into `ggplot()` directly, rather than the original datasets' CRS. From the plot, `basins_df`'s lat & lon values are clearly much larger. Perhaps the issue arose at the conversion to data frame step? – Z.Lin Nov 30 '18 at 09:09

1 Answers1

0

The issue was in fact related to the projection of my data. I had defined the projection of the points file rather than projecting it, so it was incorrect. I was able to correct this by redefining it correctly and the projecting the points file using spTransform

leaving this up in case other run into similar issues, although the error was not in the ggplot2 script.

Jack_d
  • 37
  • 6