Reputation: 21
I would like to plot the R map without the longitude and latitude values. Most of the map functions use longitude and latitude values. The only information I have is the name of the state and frequency. Please let me know how to plot the R map.
state freq
1 california 14717
2 texas 6842
3 new york 6729
4 florida 6720
5 illinois 5921
6 NA 5897
7 georgia 5008
8 ohio 4197
9 michigan 3593
10 virginia 3278
11 new jersey 3097
12 north carolina 3084
13 washington 3048
14 pennsylvania 2972
15 maryland 2821
16 missouri 2615
17 minnesota 2318
18 massachusetts 2242
19 colorado 2210
20 indiana 2078
21 arizona 1901
22 wisconsin 1842
23 oregon 1817
24 tennessee 1737
25 alabama 1679
26 connecticut 1627
27 south carolina 1122
28 nevada 1090
29 kansas 1062
30 kentucky 983
31 oklahoma 971
32 louisiana 954
33 utah 877
34 arkansas 855
35 mississippi 787
36 nebraska 674
37 idaho 599
38 new hampshire 551
39 new mexico 472
40 rhode island 435
41 hawaii 409
42 west virginia 391
43 montana 330
44 delaware 300
45 vermont 207
46 alaska 200
47 south dakota 189
48 iowa 186
49 wyoming 150
50 maine 101
51 north dakota 52
Upvotes: 1
Views: 3174
Reputation: 4294
Here is a plotly
alternative using some of the techniques from previous respondents:
library(plotly)
# create df but taking a subset of original poster's data
df <- data.frame(state = c("california","texas","nevada","north dakota", rep("NA", 47)),
freq = c(14717, 6842, 1090, 52, rep(0, 47)),
stringsAsFactors = FALSE )
# generate location information for all states (using built-in data)
state.info <- inner_join(data.frame(state=tolower(state.name),
long=state.center$x, lat=state.center$y,
stringsAsFactors=FALSE),
data.frame(state=tolower(datasets::state.name),
abbrev=datasets::state.abb))
# join the test data to the states location info
map.df <- inner_join(state.info, df, by="state")
# set up plotly to zoom in to US only
g <- list(scope='usa', projection=list(type='albers usa'),
showlakes=TRUE, lakecolor=toRGB('white'))
# plot on the US map
plot_ly(map.df, type='choropleth', locationmode='USA-states',
locations=map.df$abbrev, z=map.df$freq, text=map.df$state) %>%
layout(geo=g, title='Frequency by State')
This produces:
Upvotes: 0
Reputation: 263301
This is the code that Deepayan Sarkar offered in his book "Lattice: " to plot a pseudo-3d barplot with continental US states as the x.y location for the bars. You should be able to substitute the 'density' values with the values in your dataset. You will probably need to remove the exclusion of AK and HI.
state.info <- data.frame(name = state.name, long = state.center$x, lat = state.center$y,
area = state.x77[, "Area"],
population = 1000 * state.x77[, "Population"])
state.info$density <- with(state.info, population / area)
library("maps")
state.map <- map("state", plot=FALSE, fill = FALSE)
panel.3dmap <- function(..., rot.mat, distance, xlim, ylim, zlim, xlim.scaled,
ylim.scaled, zlim.scaled) { scaled.val <- function(x, original, scaled) {
scaled[1] + (x - original[1]) * diff(scaled) / diff(original) }
m <- ltransform3dto3d(rbind(scaled.val(state.map$x, xlim, xlim.scaled),
scaled.val(state.map$y, ylim, ylim.scaled), zlim.scaled[1]), rot.mat, distance)
panel.lines(m[1,], m[2,], col = "grey76") }
cloud(density ~ long + lat, state.info, subset = !(name %in% c("Alaska", "Hawaii")),
panel.3d.cloud = function(...) { panel.3dmap(...)
panel.3dscatter(...) },
type = "h", scales = list(draw = FALSE), zoom = 1.1, xlim = state.map$range[1:2],
ylim = state.map$range[3:4], xlab = NULL, ylab = NULL, zlab = NULL,
aspect = c(diff(state.map$range[3:4]) / diff(state.map$range[1:2]), 0.3),
panel.aspect = 0.75, lwd = 2, screen = list(z = 30, x = -60),
par.settings = list(axis.line = list(col = "transparent"),
box.3d = list(col = "transparent", alpha = 0)))
Upvotes: 1
Reputation: 7654
Here is a partial choropleth, using the partial data frame contributed by @akhmed.
df <- data.frame( state = c("california","texas","nevada","north dakota", rep("NA", 47)),
freq = c(14717, 6842, 1090, 52, rep(0, 47)),
stringsAsFactors = FALSE )
library(maps)
library(ggthemes)
states_map <- map_data("state", region = c("california","texas","nevada","north dakota"))
new_map <- merge(states_map, df, by.x = "region", by.y = "state")
new_map <- arrange(new_map, group, order) # to sort polygons in right order
ggplot(new_map, aes(x = long, y = lat, group = group, fill = freq)) +
geom_polygon(color = "black") +
coord_map("polyconic") + theme_tufte() + labs(x = "", y = "")
You can revise the color scheme with scale_fill_gradient2
, for example.
Upvotes: 1
Reputation: 3635
Lacking a reproducible example, I manually typed just 4 states as an illustration:
library(dplyr)
library(ggplot2)
df <- data.frame( state = c("california","texas","nevada","north dakota"),
freq = c(14717, 6842, 1090, 52),
stringsAsFactors = FALSE )
state_level_df <- data.frame(state = tolower(state.name),
long = state.center$x,
lat = state.center$y,
stringsAsFactors = FALSE) %>%
inner_join( df, by="state" )
ggplot(state_level_df, aes(long, lat)) +
borders("state") +
geom_point(aes(color=freq,size=freq), show_guide=FALSE) +
theme(text=element_text(size=18)) +
scale_size(range=c(2,20)) +
scale_color_continuous(low="red",high="green") +
theme_bw()
which gives me this:
Your full data frame df
should work as well.
Upvotes: 1