The data for this demonstration was given through a homework assignment in Economics 362 Data Analysis as part of a homework set. The data originally comes from The Economist that shows the Human Development Index (HDI) and the Corruption Perception Index (CPI) for each country of the world. The data set contains five colums: Country, HDI Rank, HDI, CPI, and Region. Under simple plotting you will find plots that were related to the homework. Under location plotting, you will see plots that were creativly made in attempt for better visualization not associated with the homework. Since we are not given location data, we use the Google Maps data set and simple string manipulation to extract the latitude and longitude of each country and plot them on a world map.

economistData <- read.csv("EconomistData.csv")

Simple Plotting

The first plot we will make is a simple scatter plot of CPI and HDI using the built in plot function with R. With this simple plot we can see some sort of underlying relations between the two variables. However we can greatly boost the look and information contained within the plot.

plot( economistData$CPI, economistData$HDI, xlab = "CPI", ylab = "HDI", main = "Scatter Plot of CPI vs HDI", pch=16 )

We now move into using ggplot2 library in R to get a more visually appealing plot.

library(ggplot2)
ggplot(economistData, aes(x = CPI, y = HDI)) +
  geom_point( size = 2 ) +
  ggtitle("CPI vs HDI with Region Indication")

Now we have a plot that looks a lot better maybe we can highlight some relationships that we see. For instance is there a linear trend line?

ggplot(economistData, aes(x = CPI, y = HDI)) +
  geom_point( size = 2 ) +
  ggtitle("CPI vs HDI with Region Indication") +
  geom_smooth(mapping = aes(linetype = "Fit"), 
              method = "lm",
              formula = y ~ x, se = TRUE, 
              color = "black")

The linear trend is nice, but from a simple look at the data, we can see that the relation is cvloser to \(y=ln(x)\) as oppose to \(y=x\). So lets look at how this shows in our plot.

ggplot(economistData, aes(x = CPI, y = HDI)) +
  geom_point( size = 2 ) +
  ggtitle("CPI vs HDI with Region Indication") +
  geom_smooth(mapping = aes(linetype = "Fit"), 
              method = "lm",
              formula = y ~ log(x), se = TRUE, 
              color = "black" )

Now lets look into another relation we might have inquiry over: Is CPI or HDI related to the region? We can do this by using the variable region in the data set and color the points based on the region they belong to.

library(ggplot2)
ggplot(economistData, aes(x = CPI, y = HDI)) +
  geom_point(aes(color = Region), size = 2 ) +
  ggtitle("CPI vs HDI with Region Indication")

Location Plotting

As with the scatter plot we were able to indicate region which unlocked some insights to the data, there is even more information we can convey. By using the google maps API through the library ggmaps to get latitude amd longitude coordinates of each country. Then by ploting through the use of the maps library, we can directly plot the HDI and CDI onto the map of the world.

The first step involves changing the strings in the country column. If we were to just input these values as is into the API, we might not gaurentee that we are getting the actual countries latitude and longitude coordinates. I tried this at first, but with the way the API works, it pulled the coordinates of locations within the United States that shared similar names, this lead to multiple data points plotted within the country. The solution to this is to add “the country of” before the name of the country in a new column so that the API knows what we want to search for. The process is very simple and is shown below.

economistData$country <- economistData$`ï..Country`
economistData$country <- sub("^", "Country of ", economistData$country )

Next you need an API key from google. You can set that up here https://developers.google.com/maps/documentation/geocoding/start. The cost is about $5 dollars for every 1000 or so requests and google gives you $ 300 of free use. I have initialized my api key as the variable \(API\_KEY\) in a hidden chunk as I can not afford to publicly give that out.

Now we will add a new country column to the data set using ggmap and initialize the world map plot with maps

library(maps)
library(ggmap)
worldMap = map_data("world")
register_google(key = API_KEY)
economistData <- mutate_geocode(economistData, country)

Now comes for the mapping of the data points, we will use the size of the dot to indicate the HDI and the color of the dot to indicate the CPI with the help of the library viridis. We apply some transforms to make the plot more visually appealing in relation to the colors and sizes of the dots.

library(viridis)
## Loading required package: viridisLite
ggplot() +
  geom_polygon(data = worldMap, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
  geom_point( data=economistData, aes(x=lon, y=lat, size=HDI, color=CPI))+
  scale_color_viridis(trans="log")+
  scale_size_continuous(range = c(0,4))+
  guides( colour = guide_legend()) +
  ggtitle("CPI vs HDI")+theme_void()

We now have a clean plot of HDI and CDI based on location of the points to unlock some further insights of the levels of CPI and HDI in relation to a countries neighbors as well as the region they belong to in the world. I hope this short project was informative and helped with any difficulties in any location based plotting in R. Please take a look at the references below for any further information.

References