ggplot world maps

Trying to add a title and perhaps lat/long lines to a map made, what am I missing? It doesn't display a title with any additional syntax. Here is the code for the map, what would I include for a title? New to creating maps with r.

base_world_messy <- p + geom_polygon(data=world_map, aes(x=long, y=lat, group=group),
colour="black", fill="light green")

I can't exactly reproduce your example because p is not defined. Here's an example of how to add a title. It's the same as adding titles to other ggplots.

library(tidyverse)

world_map <- map_data("world")

base_world_messy <- ggplot(data=world_map, aes(x=long, y=lat, group=group)) + 
   geom_polygon(colour="black", fill="light green") +
   ggtitle("Here's my title")

base_world_messy

Created on 2021-12-20 by the reprex package (v2.0.1)

1 Like

Thanks StatSteph. Would you happen to know how to include the latitude and longitude with more detail, i.e. "0º", "15º" etc. ? I was not able to find much simplistic models online.

There's probably a lot of ways to do this but here's one way and I borrowed some guidance from here: r - Revisiting the "Format latitude and longitude axis labels in ggplot" - Stack Overflow

library(tidyverse)

world_map <- map_data("world")

base_world_messy <- ggplot(data=world_map, aes(x=long, y=lat, group=group)) + 
   geom_polygon(colour="black", fill="light green") +
   ggtitle("Here's my title")

xr <- seq(-180, 180, 15)
xlabels <- parse(text=str_c(abs(xr), "^o"))
yr <- seq(-90, 90, 15)
ylabels <- parse(text=str_c(abs(yr), "^o"))


base_world_messy +
   scale_x_continuous("Longitude", breaks = xr, labels = xlabels) +
   scale_y_continuous("Latitude", breaks = yr, labels = ylabels)

Created on 2021-12-20 by the reprex package (v2.0.1)

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.