Home > Software engineering >  How can I plot a map using ggplot2 that includes the latitude and longitude of the data?
How can I plot a map using ggplot2 that includes the latitude and longitude of the data?

Time:01-27

I have data that includes the longitude and latitude of certain locations in Arizona however its over 9000 observations and ggplot2 keeps giving me "Error in st_as_sf.data.frame(data, coords = c(x, y), remove = FALSE, crs = crs) : missing values in coordinates not allowed". This data also has missing longitudes and latitudes that may be affecting the code. I need help plotting geolocations for over 9000 observations located in an excel file, excluding missing data.

I tried using ggplot2 but it kept giving me error codes and I tried mapview but I think that it may only work for smaller data observations. I still have to merge this data with 2 other 9000 observations. I think it may be because I need a bigger data visualization program but I don't know. I was expecting all the observations to be displayed on a map by themselves and then all together (layered). Any assistance would be greatly appreciated.

CodePudding user response:

Would ggmap package be helpful? You may use it to download a map of Arizona and plot the data points according to their longitude and latitude on the map. For example,

AZmap = get_stamenmap(bbox=c(left = -115.63, bottom = 31.25, right =
                                 -108.84, top = 37.18),zoom = 2)
ggmap(AZmap)  geom_point(aes(x = your longitude,
                                y = your latitude,
                                data = your data,
                                alpha = 0.1, size = 1, color = 'red')

I tried get_map or get_googlemap before but they required API key. get_stamenmap works best for me.

  • Related