Background —
This independent study was more so learning R rather than spending more time in project-based learning as I did with python. R is a very powerful data-driven language and is great for using large data sets. R is vastly used for geospatial analysis as well. I created a county population map of the United States using census data from built-in libraries whose primary use is for geospatial analysis. The code looks very simple, but it was not. I had significantly more problems with R than I did with Python. The syntax used in R is an absolute cluster and I despise it.
Steps
- Import libraries usmap and ggplot2.
- Add some data to plot the map, data = countypop draws the county lines and values creates data with the built-in census data via a CSV.
- Create a scale and create breaks
- Change the scale fill to one that’s not a constant color for easier viewing
- Cry because you can’t create an interactive map with tmap
The Code —
library(usmap)
library(ggplot2)
plot_usmap(data = countypop, values = “pop_2015”, color = “grey”) +
scale_fill_viridis_c(name = “County Population 2015”, label = scales::comma, option = ‘D’, trans = “log2”, breaks = trans_breaks(“log2”, function(x) 2^x))+
theme(legend.position = “right”)
Problems —
Like I stated before, I had tons of issues with this project. I programmed the map at least 15 different ways and it never looked like how I wanted it to. I think R is a little over my head when it comes to the math-based stuff like the scale. The breaks were never how I wanted them but I chose one that seemed the most reasonable.
Another issue that I had was this map is a static map, it cannot move and you cannot click on counties to see their population. I tried for at least three weeks to create an interactive map through the library T map. It’s literally very simple, you just join the counties and the county population CSV to create the interactive map. The issue that I had was joining the two tables by their geoID. It never seemed to work and I literally even created my own spreadsheet with the counties’ populations, names, states, and geoID. Didn’t work. This was the most frustrating part of the project that I dealt with.
Again, I took more of a book approach to this course so it definitely felt like I could’ve figured it out by making other things within R. I can follow the code and see what someone is doing, but trying to do it on your own starting out is very difficult. Also, R is just way worse and not as user-friendly as Python. I feel that I could definitely become better at R if I just spent more time with it, and also brushed up on my math skills a little more.