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:
newdata <- left_join(mapstates, delaymap, by= c("mapstates" = "state"))
I executed the following code, to reproduce your idea in my environment and
it does work. I am using the “statepop” data.frame
from the usmap
package to join your data to, as it has the fips
code needed by the
package, to identify states (or counties). Also see the documentation on
the data
argument of ?plot_usmap()
.
library(dplyr)
library(usmap)
library(ggplot2)
map_df <- tibble::tribble(
~"mapstates",~"mapdelay",
"Alabama" , 457,
"Alaska" , 374,
"Arizona" , 2852,
"Arkansas" , 339,
"California" , 11609,
"Colorado" , 3298
)
new_df <- left_join(statepop, map_df, by = c("full" = "mapstates"))
plot_usmap(data = new_df, values = "mapdelay", color = "white") +
scale_fill_continuous(
low = "white", high = "red", name = "Delay", label = scales::comma
) + theme(legend.position = "right")
Note: Some of the states are not colored, because I do not have your full dataset.