0

I have two data sets named a and b.

The classes of a and b are as follow:

> class(a)
[1] "SpatialPolygonsDataFrame"

attr(,"package")
[1] "sp"
> class(b)
[1] "data.frame"

I added a column of b to a, and then I merged them.

Then using plot(a) I drow the map:

But I need to have a map that include circles witht different sizes as population.

Rojer
  • 335
  • 2
  • 9

1 Answers1

0

Since your dput only shows the result of dput(head(covid)) I decided to plot the map only for the 6 provinces provided.

You can try to use the packages sf and ggplot2:

library(rnaturalearth) #get SA map
library(sf)
library(tidyverse)

#Covid data
covid <- structure(list(Province = c("EC", "FS", "GT", "KZ", "LM", "MP"), Cases = c(2748L, 208L, 2993L, 1882L, 132L, 103L), Population = c(11.56, 
                                                                    2.75, 12.27, 10.27, 5.4, 4.04)), row.names = c(NA, 6L), class = "data.frame")                                                                     
#Map as an sf object
map <- st_as_sf(ne_states(country="South Africa"))

#Modify the covid dataframe before merging 
covid <- covid %>%
  mutate(name = case_when(Province=="EC" ~ "Eastern Cape",
                          Province=="FS" ~ "Free State",
                          Province=="GT" ~ "Gauteng",
                          Province=="KZ" ~ "KwaZulu-Natal",
                          Province=="LM" ~ "Limpopo",
                          Province=="MP" ~ "Mpumalanga"))

#Merging covid data + map
covid <- st_as_sf(left_join(covid,map,by="name"))

#We want to create breaks in order to have the desired legend
covid <- covid %>%
  mutate(breaks = case_when(Cases > 45 & Cases < 107 ~ "45 to 107",
                            Cases > 107 & Cases < 147 ~ "107 to 147",
                            Cases > 147 & Cases < 1547 ~ "147 to 1547",
                            Cases > 1547 & Cases < 2846 ~ "1547 to 2846",
                            Cases > 2846 & Cases < 15396 ~ "2846 or + "))

covid %>%
  mutate(breaks=fct_relevel(breaks,"45 to 107","107 to 147","147 to 1547","1547 to 2846","2846 or + ")) %>% #we reorder the labels
  ggplot() + 
  geom_sf(aes(fill=breaks)) +
  scale_fill_manual(values=c("#feedde","#fdd0a2","#fd8d3c","#f16913","#d94801")) +
  geom_point(aes(x=longitude, y=latitude, size=Population),colour="grey70") +
  labs(fill="Cases") +
  theme_void()

enter image description here

You can certainly improve the result by using the appropriate coordinate reference system (have a look at the documentation of the sf package).

A.Chrlt
  • 296
  • 2
  • 7
  • I tried to use your commands, unfortunately, I received errors in installing the package tidyverse, the errors is this: `Error: package or namespace load failed for ‘tidyverse’ in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]): namespace ‘vctrs’ 0.3.5 is already loaded, but >= 0.3.6 is required`. @A.chrlt – Rojer May 06 '21 at 18:56
  • I could not find `ggplot` in `R`. I can find `ggplot2` but not `ggplot`!. We have `ggplot` function but we don't have a package named `ggplot`. @A.Chrlt – Rojer May 06 '21 at 19:00
  • You are right, I made a typo. It is indeed `ggplot2`. I edited my answer. Regarding the tidyverse you can have a look at [this post](https://stackoverflow.com/questions/60451908/tidyverse-not-loaded-it-says-namespace-vctrs-0-2-0-is-already-loaded-but) – A.Chrlt May 07 '21 at 08:26