From the course: Creating Maps with R
Use geom_sf to visualize geo locations
From the course: Creating Maps with R
Use geom_sf to visualize geo locations
- I'm going to show how to build both geo scatter plots and geo bubble charts with the ggplot2 package. Specifically, we'll be looking at visualizing the cities in Brazil with a population of one million residents or more. Let's go over to the 04_01b project, and I'd like you to open this script. Let's load the packages on line 1 through 4. Line 6 through 8, we're going to filter out Brazil from the country's one-oh-one-oh data set. And online 10 through 13 we're going to filter out those cities in Brazil with a population of greater than one million. And now, we're going to start our ggplot2 chart in the same way that we start all ggplot2 charts with the ggplot function. We are then going to add geom_sf data is equal to brazil_sf. Geom_sf knows how to visualize all of the different features that can be contained within an sf object. In a case of brazil_sf, we can see the outline of Brazil. The defaults for ggplot2, don't look that great for maps. Let's change the fill color for Brazil to look something more like land. So, we're going to add the argument fill is equal to dark olive green 3, which makes that look much more like land. Now, I want to remove the X and the Y axis from the chart this kind of chart furniture isn't useful for most maps that you'll want to build. So, we make use of theme void. The next thing for us to do is to add our points to the map. So, we're going to add another geom_sf layer. I'll add a plus here, so we don't forget it. And we'll go back inside the brackets, and we'll add data is equal to brazil_cities. And now we can see our points on a map. They're a little bit small. So, let's increase the size of those points to 4. Notice how in the south of Brazil, many of these points overlap one another. We want to ensure that readers can see all of the cities in our map. What we need to do is change the shape code used for our points. This chart shows the most commonly used shape codes in ggplot2. We want to shape that has both a fill color and an outline color. The best option first is shape code 21. So, let's go and add shape code. So, we will add shape is equal to 21. And now we have discs with a border and currently no fill color. I want to change the fill color of these cities to be dependent on a capital column. To do that we need to use the a-e-s function. The a-e-s function stands for aesthetics, and it allows us to create bindings between columns in our data set and coordinate systems in our chart. So, I'm going to specify fill is equal to capital and ggplot2 uses that column to create a new fill scale. Unfortunately, our column is in numeric column and so what ggplot2 does is it creates a continuous fill scale. This is going to be confusing for readers because the fill scale is supposed to show the single capital city and the cities, which aren't capitals. So, we need to change this from a continuous variable to a discrete variable. I'm going to use as.logical to do this. As.logical and a closed bracket and we now have a much more sensible legend and color scale. So, that's our geo scatter plot pretty much finished with I want to turn this into a geo bubble chart by changing the size of the points based on the population. So, to do that, we need to remove the size argument from here and move it into a-e-s. So, size is going to be dependent on the pop column. Now, I feel in this map that the smallest cities are a little bit too small. So, what I'm going to do is I'm going to modify the scale used for the size. So, we're going to add scale, size, area. We'll add a open and closed bracket and a plus we'll go back inside the brackets, and I'm going to set max size as 10, and I think that just improves the scale of the points. However, notice that we see many fewer points now because there are cities on top of one another. What we need to do now is we need to change the order in which ggplot2 plots those points, and we do that by changing the order of the rows in the data set. So, let's go up to Brazil cities, and let's append onto here a pipe and we're going to use the function "arrange" in the standing order of the population column. I'll re-run this code and I'll weave in my ggplot2 code on line, 16 through 24, and now we can see our smaller points are on top of our larger points. Now, I think this is a fairly good looking chart. The only improvements I definitely recommend making would be to modify the appearance of the legend. Because this is a mapping course and not a ggplot2 specific course, I've provided you pre-written code for doing this, which you will find in this script.
Contents
-
-
-
-
-
-
Use geom_sf to visualize geo locations5m 53s
-
(Locked)
ggplot2 choropleth and continuous data6m 46s
-
(Locked)
ggplot2 choropleth and discrete data6m 14s
-
(Locked)
Label maps with ggrepel package5m 14s
-
(Locked)
Zoom into regions with coords_sf()3m 37s
-
(Locked)
Transform CRS with coord_sf()2m 25s
-
(Locked)
Challenge: Label a geobubble chart of Germany's biggest cities32s
-
(Locked)
Solution: Label a geobubble chart of Germany's biggest cities4m 45s
-
(Locked)
Challenge: Visualize state coastline length with choropleth33s
-
(Locked)
Solution: Visualize state coastline length with choropleth4m 11s
-
-
-
-