I am currently trying to plot some coordinates on a map in the rworldmap package.
From what I've seen looking it up, this should be relatively straightforward, however it is giving me trouble.
An example of my data can be found here
To plot the map (I'm focusing on Eurasia), I've used:
library(rworldmap)
plot(newmap, xlim = c(-27.0, 174.0), ylim = c(17.5, 77.7), col = "grey",border = "darkgrey", bg = "lightblue")
The next step is plotting my coordinates on the map as points. My data frame containing the coordinates is Coordinate_AD_Clean
. I've done as follows:
points(Coordinate_AD_Clean$long, Coordinate_AD_Clean$lat, col = "red", pch = "+")
This is where it breaks down however as the resulting plot looks like this. I know this is wrong as none of my coordinates are in the ocean, and there should be 116 points on the map (the number in my data frame) while there are many fewer on the plot.
I am unsure why I have incorrect and missing coordinates. The coordinates I have in my data frame are from google maps, which shouldn't be an issue since I tested 2 or 3 different coordinates I pulled from google maps at random and these were plotted to the map fine. Is the issue how the data is formatted within my data frame? I also have multiple duplicate coordinates, could this be an issue? I'm at a bit of a loss so any help would be much appreciated.
Many thanks for reading!
After consulting with someone much more experienced in R than I, the solution has been shown to me.
After using dput
to see what was going on within the data, I could see a vast number of levels within the data frame that weren't present when I viewed the data itself. Some sort of hidden formatting has occurred at some point, either in the original .csv data or while I was formatting the data inside R.
The solution to this was to use write.csv(dataframe, "filename.csv")
to create a new .csv file of just the data I had subsetted inside R. Then I read the data from this new .csv back in to R, and retried the plot using this dataframe instead, and the problem was solved.
Hope this helps!