I'm following a tutorial to create a cartogram and the line to do so creates an error, as does the solution to fix the error which is to transform the data using st_transform(). I've looked online for similar issues/fixes and can't seem to find much.
Initial Code:
library(cartogram)
library(maptools) # world boundaries coordinates
data(wrld_simpl)
afr=wrld_simpl[wrld_simpl$REGION==2,]
plot(afr)
afr@data
#Line causing problems
afr_cartogram <- cartogram_cont(afr, "POP2005", itermax=7)
Initial Error:
> afr_cartogram <- cartogram_cont(afr, "POP2005", itermax=7)
Error: Using an unprojected map. This function does not give correct centroids and distances for longitude/latitude data:
Use "st_transform()" to transform coordinates to another projection.
Code trying to fix previous error by calling st_Transform():
afr_proj <- st_transform(afr, "+init=epsg:4326")
afr_cartogram <- cartogram_cont(afr_proj, "POP2005", itermax=7)
New Error:
> afr_proj <- st_transform(afr, "+init=epsg:4326")
Error in st_transform(afr, "+init=epsg:4326") :
could not find function "st_transform"
> afr_cartogram <- cartogram_cont(afr_proj, "POP2005", itermax=7)
Error in cartogram_cont(afr_proj, "POP2005", itermax = 7) :
object 'afr_proj' not found