0
votes

Here is my code below. I want to plot CO2 distribution over the South African map.

Loading packages

library(tidyverse)
theme_set(theme_bw())
library("sf")
library("rnaturalearth")
library("rnaturalearthdata")

Assigning world to the countries of the world data

world <- ne_countries(scale = "medium", returnclass = "sf")

Reading the local file with CO2 and geographic coordinates (Lon and Lat)

SA_CO2 <- read_csv2("C:/Users/Xolile Ncipha/Documents/SA_CO2_DJF_2004_2009.csv")

Converting the data frame to a sf object and the coordinate reference system projection to WGS84, which is the CRS code #4326.

(SA_CO2 <- st_as_sf(SA_CO2, coords = c("Lon", "Lat"), crs = 4326, agr = "constant"))

Plotting the map and overlaying it with CO2 dataThe output of my code/script.

ggplot(data = world) + geom_sf() + geom_sf(data = SA_CO2, aes(fill = CO2)) +

CO2 legend

scale_fill_gradientn(colors = sf.colors(10)) + 

Confining the map to South African domain.

coord_sf(xlim = c(15, 35), ylim = c(-36, -22.3), expand = FALSE) + 

Axis labels

xlab("Longitude") + ylab("Latitude")

The results is the geographic points on the map. I don't get the overlay of CO2 data and its spatial distribution. I have attached a picture of the resulting map and the spatial data.

1
Dear Hoppo, Thanks for your helpful response. Sorry I did not include the data file. I have included a link to the data file (dropbox.com/s/g648u16rszjnnu1/SA_CO2_DJF_2004_2009.csv?dl=0) and names of the relevant towns in SA (dropbox.com/s/ezingxbfq0x0m5w/Town_Names.csv?dl=0). I'm seeking to do a spatial distribution of ambient CO2 data of densely spatially placed data. Could you please also help me with adding the code for including the names of the towns on the map. I hope the supporting info I provided will be helpful for your appreciated help. XolileXolile Ncipha

1 Answers

0
votes

This is a good question. But, unfortunately, you did not provide a link to the source of your data ( "SA_CO2_DJF_2004_2009.csv"), so it was necessary for me to create some data that is likely somewhat similar to your data, but probably not exactly the same.

I found a spelling error in the following line of your code. Even after correcting the spelling error, this line of code continued causing an error.

scale_fill_gradientn(colors = sf.colors(10)) + 

The data I used included tons of CO2 from SA during the years, 2010-2016. I also selected a few SA cities with populations from another source, and then allocated the SA annual CO2 by the ratio of the combined city populations. Therefore, those cities with smaller populations were allocated smaller proportions of the annual CO2 and the larger cities were allocated larger ratio's of CO2.

https://howsouthafrica.com/major-cities-international-airports-south-africa/
https://data.worldbank.org/indicator/EN.ATM.CO2E.KT?locations=ZA&view=map

The code used to create the plot shown at the link below is:

ggplot(data = world) + 
    geom_sf() + 
    geom_sf(data = coor.sf, aes(size = tons), 
    fill = "blue", color = "blue", alpha = .3) +
    coord_sf(xlim = c(15, 35), ylim = c(-36, -22.3), 
    expand = FALSE) + xlab("Longitude") + ylab("Latitude")

Please email me if you have any questions.

[[![SA CO2]]