0

I want to fill the colour on a map. However, the plot doesn't come out as aspected.

How can I visualize the data with longitude and latitude?

install.packages("WDI")
install.packages("tidyverse")

library(WDI)
library(tidyverse)

literacy_globe <- WDI(country = "all", indicator = "SE.ADT.LITR.ZS", start = 2015, end = 2018, extra = TRUE)

literacy_globe <- na.omit(literacy_globe)

ggplot(literacy_globe, aes(x = longitude, y = latitude, group = iso3c)) +
    geom_point(aes(fill = income), colour = "white")

I'd like the result similar to: example map

JohanC
  • 71,591
  • 8
  • 33
  • 66
James Huang
  • 63
  • 1
  • 10
  • You need a shapefile with the country boundaries to make a map. See for example https://www.rdocumentation.org/packages/choroplethr/versions/3.6.3/topics/choroplethr_wdi – Kent Johnson Jan 04 '20 at 02:37

2 Answers2

1

You can use the following code

#Loading the required packges
library(WDI)
library(tidyverse)
library(maptools)
library("ggplot2")
library("sf")

#Downloading the data
literacy_globe <- WDI(country = "all", indicator = "SE.ADT.LITR.ZS", start = 2015, end = 2018, extra = TRUE)

#Removing the NAs
literacy_globe_1 <- na.omit(literacy_globe)

#Saving the data as .csv file as your data contains blank cells which are not NAs
write.csv(literacy_globe_1, "literacy_globe_1.csv")

#Reading the data from .csv file
data <- read.csv("literacy_globe_1.csv")

#Removing the NAs
literacy_globe <- na.omit(data)
summary(literacy_globe)
head(literacy_globe,2)

#Mapping using ggplot2 package
data(wrld_simpl)

#sp to sf conversion
world <- st_as_sf(wrld_simpl)

# now create the map
ggplot(world) +
  geom_sf(colour = "black", fill = NA) + coord_sf(expand = FALSE) + 
  theme_bw() + geom_point(aes(longitude, latitude),data= literacy_globe, colour=alpha("red",0.7))

enter image description here

For white fill of polygon and grey outside area, you can use

ggplot(world) +
  geom_sf(colour = "black", fill = "white") + coord_sf(expand = FALSE) + 
  geom_point(aes(longitude, latitude),data= literacy_globe, colour=alpha("red",0.7)) 

enter image description here

Update

choropleth map

literacy_globe <- WDI(country = "all", indicator = "SE.ADT.LITR.ZS", start = 2015, end = 2018, extra = TRUE)

literacy_globe <- na.omit(literacy_globe)
summary(literacy_globe)
head(literacy_globe,2)

#Using ggplot2 package
data(wrld_simpl)

#fortify shape file to get into dataframe 
wrld_simpl.f <- fortify(wrld_simpl, region = "NAME")
class(wrld_simpl.f)

head(wrld_simpl.f)

#merge with coefficients and reorder
merge.shp<-merge(wrld_simpl.f,literacy_globe, by.x = "id", by.y = "country", all.x=TRUE)
final.plot<-merge.shp[order(merge.shp$order), ] 

head(final.plot, 2)
#basic plot
ggplot() +
  geom_polygon(data = final.plot, 
               aes(x = long, y = lat, group = group, fill = income), 
               color = "black", size = 0.25) 

enter image description here

Community
  • 1
  • 1
UseR10085
  • 7,120
  • 3
  • 24
  • 54
  • many thanks for your answer could you explain what the purpose of this step? #sp to sf conversion , world <- st_as_sf(wrld_simpl) – James Huang Jan 04 '20 at 15:47
  • If i need to draw a choropleth map, how could I add layers to fill in? thank you very much – James Huang Jan 04 '20 at 16:16
  • @James Huang because `geom_sf( )` requires `sf` object. The `wrld_simpl` is a `sp` object. So, I have converted `sp` object to `sf` object. If it has answered your question, you may consider accepting the answer by clicking √. – UseR10085 Jan 04 '20 at 16:22
  • I have got few questions here 1) I suppose I've used a left_join() to merge spatial data and literacy data. What's the difference between these two? 2)if I use the map_data, do the procedures remain the same? – James Huang Jan 04 '20 at 20:49
  • See this to have answer to your first question https://stackoverflow.com/q/1299871/6123824 – UseR10085 Jan 05 '20 at 01:45
0

I found another way to draw the hierarchy scatter on world map, but i were not so sure if it has some drawbakcs.

literacy_globe <- WDI(country = "all", indicator = "SE.ADT.LITR.ZS", start = 
2015, end = 2018, extra = TRUE)
literacy_globe <- na.omit(literacy_globe)

lit.long <- literacy_globe$longitude
lit.lat <- literacy_globe$latitude
income <- literacy_globe$income

# prepare a NULL map
mp<-NULL 
mapworld<-borders("world",colour = "gray50",fill="white")
#mp = empty map

#plot a map
mp <- ggplot() + mapworld + ylim(-60,90)

#geom_point plot the data on it 
mp2 <- mp + geom_point(aes(x = lit.long, y = lit.lat), color = "darkblue",
fill = income) + 
scale_size(range = c(1,1))

enter image description here

UseR10085
  • 7,120
  • 3
  • 24
  • 54
James Huang
  • 63
  • 1
  • 10