Changing scale and color of a map

I have ploted a grapht using the code below;

mapdata <- left_join(mapdata, data, by= "region")
mapdata <- mapdata %>% filter(!$UHCindex2017))
map1 <- ggplot(mapdata, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = indexchange), color = "yellow") +

I would like to change the color of the graph and also the scale.
How can I do this?

Hard to tell without having your data...

But that is too easy answer to give, so in principle:

  • for colors consider adjusting the scale_fill_* part of your ggplot call
  • for zoom consider adjusting the coords_sf() part of your ggplot call; note that you can set both X and Y limits separately, with interesting interpretation of the special value of NA as "leave it as it stands"

So consider this piece of code as a baseline - it draws a dull default:


shape <- st_read(system.file("shape/nc.shp", package="sf"))   # included with sf package

# default zoom, default colors
ggplot(data = shape,
       aes(fill = BIR74)) +

And now consider this, slightly adjusted, piece of code:

# zoom + colours from - just for the heck of it...
ggplot(data = shape,
aes(fill = BIR74)) +
  geom_sf() +
  scale_fill_gradientn(colours = wesanderson::wes_palette("Zissou1", 100, type = "continuous")) + 
  coord_sf(xlim = c(-90, NA),
           ylim = c(NA, 40))

What it does differently is:

  • it sets fill colors to a Wes Anderson palette - Zissou's Life Aquatic, because why not?
  • it zooms the map out by extending the X coordinate westward to -90° and Y coordinate northward to 40°, leaving the east and south margins unchanged (again, for no particular reason other than just showing off...)

This topic was automatically closed 21 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.