I'm having kind of a peculiar issue that I'm hoping someone can shed some light on. I'm actually creating my map in R, but I'm using QGIS as a tool to check my mapping process (R newb here). So let's dig into it.
I'm trying to plot rivers in my area of interest (Tanzania), but the source shapefile (diva-gis.org) includes tons of unnecessary water lines. For the sake of reproducability, I'll be including all edited DBFs and shapefiles. Here is the original DBF file and here is the original shapefile. It looks like this.
As you can see in the DBF, many of the rivers are unknown/unnamed and listed as "UNK" which isn't very useful for me. In QGIS, I am simply able to open the attribute table, sort by name, and remove all UNK values. Alternatively, I can filter out those values through the SQL expression:
"NAM" != "UNK"
Both of these yield the same results. Here is the QGIS edited DBF and here is the QGIS edited shapefile. This is what the map looks like:
Then things get quirky. Again, I'm actually creating this map in R. Using the original source data, I edit the DBF with the "foreign" package, overwrite the DBF, and then map using ggplot. I'm getting a totally different result than the one pictured above. Here's a little snippet of example code (not for the full map, but this is essentially what this portion of the map code will look like):
library(rgdal)
library(foreign)
library(ggplot2)
#set your appropriate working directory
setwd("D:/Mapping-R/Returns-Practice")
#read dbf, sql expression to remove all UNK values in NAM
water.lines <- read.dbf("original-waterlines.dbf")
water.lines <- water.lines[water.lines$NAM != "UNK",]
#overwrite dbf
write.dbf(water.lines, "TZA_water_lines_dcw.dbf")
#read shapefile, fortify to create readable data frame
water.lines.shp <- readOGR(dsn = "D:/Mapping-R/Returns-Practice",
layer = "original-waterlines")
water.lines.shp <- fortify(water.lines.shp)
#plot
ggplot() + geom_polygon(data = water.lines.shp,
aes(long, lat, group = group),
color = "slategray4")
The result of this code looks like this:
Kind of stretched out and funky, but clearly does not follow the same line pattern as the QGIS-edited image. This has been driving me absolutely nuts. I actually went through and checked the R attributes against the QGIS attributes and they matched up perfectly, but the image result is completely different. After playing around with it some more, I figured that my code was to blame and that's where I messed up. But then I opened the original DBF and manually removed all UNK values and plugged it back into QGIS. Here is the resulting DBF and here is its corresponding shapefile, which matches the R image/plot and looks like this:
What gives?! The attribute tables are identical but the images are different. I feel like there has to be something really obvious that I'm missing but I've been playing with this for literally hours and can't figure it out.
water.lines.shp <- subset(water.lines.sho, NAM != "UNK")
afterreadOGR()
. 2. addcoord_map()
to your plot.