I have a dataframe with latitude and longitude coordinates for all the places (4500 cities) I want to plot.
For example:
CITY | lat | long | cluster |
---|---|---|---|
A | -16.75881189 | -49.44054783 | 1 |
B | -18.48756496 | -47.39683244 | 2 |
C | -48.71881214 | -16.18267186 | 3 |
I need something like this - But here it teaches how to do using python and I need to use R. (FROM: https://www.kaggle.com/crisparada/brazilian-cities-a-simple-exploration):
However, I could not find on internet how I can plot in R using coordinates. The only thing i found is how i can get the coordinates on google maps and then plot direclty. But, since I have to do a kind of "scatterplot map" for more than 4500 cities it is not pratical do in this way.
I recently started studying R language and there are some interpretations that I cannot make in relation to what I find on the internet.
You can try using the sf
package.
Assuming you've already loaded the data from a .csv or other type, and have a dataframe (or tibble) named my_df
:
library(sf)
library(ggplot2)
my_sf <- st_as_sf(my_df, coords = c('LON', 'LAT'))
my_sf <- st_set_crs(my_sf, crs = 4326)
#Plot it:
ggplot(my_sf) +
geom_sf(aes(color = cluster))
The sf package (and associated geom_sf
) is R's way of plotting geographic data. 4500 points shouldn't be a problem.