1

I have created following map for disaster by district as shown below. enter image description here

The code I used is:-

install.packages("cartography")
install.packages("sf")
install.packages("tidyverse")
library(cartography)
library(sf)
library(tidyverse)
setwd("C:/Users/lenovo/Desktop/IEM")
Province2Disaster<-st_read("DisasterBagmatiProvince.shp")
View(Province2Disaster)
Province2Disaster$DeathClass<-cut(Province2Disaster$Deaths,
             breaks = c(0,500,1500,3000,4500, Inf),
             labels=c('<500','50-1500','1500-3000','3000-4500','>4500'))
ggplot()+geom_sf(aes(fill=DeathClass),data=Province2Disaster)+
ggtitle("Total Death by Disaster in Province 2")

But I want to label feature by district name (dist_name) as shown in attribute table. enter image description here

But using the code (after the code above) :-

ggplot()+geom_sf(aes(fill=DeathClass),data=Province2Disaster)+
geom_text(aes(label = dist_name))+
ggtitle("Total Death by Disaster in Province 2")

The error display as:- Error in FUN(X[[i]], ...) : object 'dist_name' not found. Though the column name dist_name is present in table. Why is this occuring? How to fix it?

1 Answer 1

1

Your error is because here:

ggplot()+geom_sf(aes(fill=DeathClass),data=Province2Disaster)+
 geom_text(aes(label = dist_name))+
 ggtitle("Total Death by Disaster in Province 2")

you've specified data in the geom_sf function, and so column names are only valid in that function. If instead you started with ggplot(data=Province2Disaster) + ... then the data is applicable in the rest of the ggplot elements and so you can use all column names (unless there's another ggplot element with a data= argument)

For geom_text you also need to specify the x and y coordinates for the text. We don't have your data so we can't tell if you already have suitable point coordinates, otherwise you can do something like add the polygon centroids (example using GADM Nepal level 1 data):

> np = st_read("./gadm40_NPL_1.shp")
Reading layer `gadm40_NPL_1' from data source 
  `/nobackup/rowlings/Downloads/nepal/gadm40_NPL_1.shp' using driver `ESRI Shapefile'
Simple feature collection with 5 features and 11 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 80.06014 ymin: 26.34752 xmax: 88.20401 ymax: 30.44702
Geodetic CRS:  WGS 84
> np = cbind(np, st_coordinates(st_centroid(np)))
Warning message:
In st_centroid.sf(np) :
  st_centroid assumes attributes are constant over geometries of x
> names(np)
 [1] "ID_0"      "COUNTRY"   "ID_1"      "NAME_1"    "VARNAME_1" "NL_NAME_1"
 [7] "TYPE_1"    "ENGTYPE_1" "CC_1"      "HASC_1"    "ISO_1"     "X"        
[13] "Y"         "geometry" 
> ggplot(np) + geom_sf(aes(fill=HASC_1)) + geom_text(aes(label=NAME_1, x=X, y=Y))

enter image description here

Also I think you can maybe use geom_sf_text instead of plain geom_text here - although I can't find documentation of how it exactly chooses X and Y coordinate, I imagine its probably centroid.

3
  • @Is this the correct way to code , 'ggplot(data=Province2Disaster)+geom_sf(aes(fill=DeathClass))+ geom_text(aes(label= dist_name))+ ggtitle("Total Death by Disaster in Province 2")'. But still It isn't working. Or I didn't get you right.
    – Walker
    Jun 18 at 13:21
  • My code was untested, but it was a step in the right direction. Are you getting a different error message now? One about geom_text requiring x and y? That's the problem now - geom_text doesn't know where to put its text. Replace geom_text with geom_sf_label and it will work, since geom_sf_label gets its coords from the geometry and geom_text doesn't understand geometries and needs to be told the X and Y coords.
    – Spacedman
    Jun 18 at 13:34
  • 1
    On this code 'ggplot(data=Province2Disaster)+geom_sf(aes(fill=DeathClass))+ geom_text(aes(label= dist_name))+ ggtitle("Total Death by Disaster in Province 2")' just use geom_sf_text() instead: 'ggplot(data=Province2Disaster)+geom_sf(aes(fill=DeathClass))+ geom_sf_text(aes(label= dist_name))+ ggtitle("Total Death by Disaster in Province 2")'
    – dieghernan
    Jun 18 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.