I am trying to add geom_text layer on top of geom_polygon in order to display each polygon's label. Without geom_text, it produces a plot as shown below:
I want it to have texts on each polygon that looks something like this:
Here is what my data M looks like:
I created a new data frame called county_text_df because using M data for geom_text would consume too much memory. Here's what the data county_text_df and M looks like:
structure(list(county = c("종로구", "중구", "용산구", "성동구",
"광진구", "동대문구", "중랑구", "성북구", "강북구", "도봉구",
"노원구", "은평구", "서대문구", "마포구", "양천구", "강서구",
"구로구", "금천구", "영등포구", "동작구", "관악구", "서초구",
"강남구", "송파구", "강동구"), lat = c(37.5990998, 37.5579452,
37.5311008, 37.5506753, 37.5481445, 37.5838012, 37.5953795, 37.606991,
37.6469954, 37.6658609, 37.655264, 37.6176125, 37.5820369, 37.5622906,
37.5270616, 37.5657617, 37.4954856, 37.4600969, 37.520641, 37.4965037,
37.4653993, 37.4769528, 37.4959854, 37.5048534, 37.5492077),
long = c(126.9861493, 126.9941904, 126.9810742, 127.0409622,
127.0857528, 127.0507003, 127.0939669, 127.0232185, 127.0147158,
127.0317674, 127.0771201, 126.9227004, 126.9356665, 126.9087803,
126.8561534, 126.8226561, 126.858121, 126.9001546, 126.9139242,
126.9443073, 126.9438071, 127.0378103, 127.0664091, 127.1144822,
127.1464824)), class = "data.frame", row.names = c(NA, -25L
))
Here is my code:
library(raster)
library(rgdal)
library(maptools)
library(rgeos) # rgeos version: 0.5-5, (SVN revision 640)
library(ggplot2)
county_text_df<- data.frame(county = unique(M$county),
lat = unique(M$lat.y),
long = unique(M$long.y))
county_text_df %>% head()
ggplot() +
geom_polygon(data = M,
aes(x = long.x,
y = lat.x,
group = group,
fill = cnt),
color = "white") +
scale_fill_gradient(low = "#FBCF61",
high = "#00CC99",
space = "Lab",
guide = "colourbar") +
geom_text(data = county_text_df,
mapping = aes(x = long,
y = lat,
label = county,
size = 4,
colour = "blue"))
I would greatly appreciate any ideas!