In R I have a dataframe that looks like this:
head(map)
mapstates mapdelay
1 Alabama 457
2 Alaska 374
3 Arizona 2852
4 Arkansas 339
5 California 11609
6 Colorado 3298
I would like to plot the frequencies on a map.
Searching the www i have found the usmap package f.e. . But all the solutions that I have found require lattitude and longitude data which I do not have.
Using the usmap package and dplyr I tried connecting lattitude and longitude data from usmap to my dataframe:
library(dplyr)
newdata<- left_join(mapstates, delaymap, by= "state")
and this error message popped up:
"Fehler: by can't contain join column state which is missing from LHS
Run rlang::last_error() to see where the error occurred."
Is there any way to plot my data just with the name of the states?
It looks like your
data.frame“map” doesn’t have a column named “states” and therefore the join command is failing. I can’t test this, since I don’t have the exact datasets you are using, but it might just work, to change your command as follows:I executed the following code, to reproduce your idea in my environment and it does work. I am using the “statepop”
data.framefrom theusmappackage to join your data to, as it has thefipscode needed by the package, to identify states (or counties). Also see the documentation on thedataargument of?plot_usmap().Note: Some of the states are not colored, because I do not have your full dataset.