我正在尝试在R中创建一个等值线图,我尝试了几种不同的方法但没有成功。我正在寻找一张基本的美国地图,在悬停时显示州名和工资中位数,并带有一个传奇,显示较高的工资着色。
以下是我的数据框:
> stateDF
# A tibble: 51 x 2
State Median
<fct> <dbl>
1 AL 91400.
2 AK 130000.
3 AZ 110000.
4 AK 92000.
5 CA 125000.
6 CO 101000.
7 CT 115000.
8 DE 105300.
9 DL 102000.
10 GA 104000.
# ... with 41 more rows
> dput(head(stateDF))
structure(list(State = structure(c(1L, 2L, 3L, 2L, 4L, 5L), .Label = c("AL",
"AK", "AZ", "CA", "CO", "CT", "DE", "DL", "GA", "HI", "ID", "IL",
"IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MS", "MO",
"MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK",
"OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VI", "WA",
"WV", "WI", "WY", "DC"), class = "factor"), Median = c(91400,
130000, 110000, 92000, 125000, 101000)), .Names = c("State",
"Median"), row.names = c(NA, -6L), class = c("tbl_df", "tbl",
"data.frame"))
这是我尝试过的(既不工作):
##this displays the map correctly but I can't figure out how to get the hover text to display my Median for each state
states <- map_data("state")
p <- ggplot(data = states) +
geom_polygon(aes(x = long, y = lat, fill = region, group = group), color =
"white") +
coord_fixed(1.3) +
guides(fill=FALSE)+
theme_classic() +
theme(axis.title.y = element_blank()) +
theme(axis.title.x = element_blank()) +
theme(axis.line = element_blank()) +
theme(axis.ticks = element_blank()) +
theme(axis.text.x = element_blank()) +
theme(axis.text.y = element_blank())
##This does not display a map, but the legend looks like it's reading the medians correctly
plot_ly(type="choropleth",
locations=stateDF$State,
locationmode="USA-states",
z=stateDF$Median) %>%
layout(.,geo=list(scope="usa"),)
感谢任何帮助!