I have a bathymetric shapefile structured as below.
str(Ab_bath)
Classes ‘sf’ and 'data.frame': 126 obs. of 13 variables:
$ CLRRGB : chr "2844D9FF" "2651DCFF" "215DDEFF" "2266E0FF" ...
$ COLOR : int 85 91 91 91 91 55 98 62 62 62 ...
$ LAYER : int 30 30 30 30 30 30 30 30 30 30 ...
$ L_LIMIT : int 0 0 0 0 0 0 0 0 0 0 ...
$ MAJ_CAT : chr "Nautical" "Nautical" "Nautical" "Nautical" ...
$ MAX_ELEV: num -14.8 -14.5 -14.2 -14 -13.8 ...
$ MIN_CAT : chr "Depth Area" "Depth Area" "Depth Area" "Depth Area" ...
$ MIN_ELEV: num -15 -14.8 -14.5 -14.2 -14 ...
$ MODE : chr "D_MR" "D_MR" "D_MR" "D_MR" ...
$ VALUE : chr "14.75" "14.5" "14.25" "14" ...
$ VALUE2 : chr "15" "14.75" "14.5" "14.25" ...
$ U_LIMIT : int 8 8 8 8 8 8 8 8 8 8 ...
$ geometry:sfc_MULTIPOLYGON of length 126; first list element: List of 2
..$ :List of 2
.. ..$ : num [1:3358, 1:2] 0.887 0.887 0.887 0.887 0.887 ...
.. ..$ : num [1:2678, 1:2] 0.888 0.888 0.888 0.888 0.888 ...
..$ :List of 2
.. ..$ : num [1:1342, 1:2] 0.886 0.886 0.886 0.886 0.886 ...
.. ..$ : num [1:1049, 1:2] 0.886 0.886 0.886 0.886 0.886 ...
..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
- attr(*, "sf_column")= chr "geometry"
- attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
..- attr(*, "names")= chr [1:12] "CLRRGB" "COLOR" "LAYER" "L_LIMIT" ...
I was able to plot it simply using the code below
ggplot() +
geom_sf(data = Ab_bath_utm, fill = "lightblue", color = "blue", alpha = 0.5)+
coord_sf(datum = 32631) #makes axes in UTM
I thought I'd try and scale the colour by min elevation using this code
ggplot() +
geom_sf(data = Ab_bath_utm, aes(fill = MIN_ELEV), color = "black", size = 0.1) +
scale_fill_gradientn(
colors = c("navy", "blue", "lightblue", "white"),
name = "Min Elevation (m)"
) +
coord_sf(datum = 32631) +
theme_minimal()
But it just plots a single colour. How can I get it to plot a scale of colours by elevation? A link to the shapefile can be found here
Very weird, this one took me a while. @JonSpring hit the nail on the head! The problem lies within the last 5 polygones which indeed overlap all previous ones. You can see that, if you run:
plot(st_geometry(Ab_bath_utm[126,]))
So you can sort in ascending order by MIN_ELEV and this will fix your issue
library(sf)
library(ggplot2)
Ab_bath_utm <- st_read("D_Areas.shp")
Ab_bath_utm <- Ab_bath_utm[order(Ab_bath_utm$MIN_ELEV), ] # resort
ggplot() +
geom_sf(data = Ab_bath_utm, aes(fill = MIN_ELEV), color = "black", lwd = 0.001) +
scale_fill_gradientn(colors = c("navy", "blue", "lightblue", "white"),name = "Min Elevation (m)") +
coord_sf(datum = 32631) + theme_minimal()