2014-06-26 20 views
5

Segue Uniti d'America Stati esempio dati da state.x77 set di dati in R:Tracciare barchart sulla Uniti mappa in R

mydf = structure(list(usa_state = structure(1:5, .Label = c("Alabama", 
"Alaska", "Arizona", "Arkansas", "California"), class = "factor"), 
    Life.Exp = c(69.05, 69.31, 70.55, 70.66, 71.71), HS.Grad = c(41.3, 
    66.7, 58.1, 39.9, 62.6)), .Names = c("usa_state", "Life.Exp", 
"HS.Grad"), class = "data.frame", row.names = c(NA, -5L)) 

> mydf 
    usa_state Life.Exp HS.Grad 
1 Alabama 69.05 41.3 
2  Alaska 69.31 66.7 
3 Arizona 70.55 58.1 
4 Arkansas 70.66 39.9 
5 California 71.71 62.6 
> 

voglio tracciare sulla mappa Uniti d'America Stati. Posso tracciare la mappa utilizzando il codice seguente:

all_states <- map_data("state") 
ggplot() + geom_polygon(data=all_states, aes(x=long, y=lat, group = group),colour="gray", fill="white") 

enter image description here

Ma io non sono in grado di tracciare barcharts sulla mappa. Grazie per l'aiuto.

+1

È necessario sapere che ci sono tre distinti paradigmi di trama 2D in R. –

+0

Alcuni ulteriori dettagli relativi a questo da una persona esperta come te saranno molto apprezzati. – rnso

+0

@mso: hai avuto la possibilità di vedere se la mia risposta di seguito sarà di qualche aiuto? – micstr

risposta

1

ho disegnato su due grandi fonti di rispondere a questa:

SOLUZIONE

mydf <- structure(list(usa_state = 
         structure(1:5, 
            .Label = c("Alabama", "Alaska", "Arizona", "Arkansas", "California"), class = "factor"), 
         Life.Exp = c(69.05, 69.31, 70.55, 70.66, 71.71), 
         HS.Grad = c(41.3, 66.7, 58.1, 39.9, 62.6)), 
       .Names = c("usa_state", "Life.Exp", "HS.Grad"), 
       class = "data.frame", row.names = c(NA, -5L)) 

library(ggplot2) 
library(maps) 
library(RColorBrewer) # lots of color palettes for these kind of charts 

library(data.table) # for sorting by key 
library(mapproj) #coord_maps() needed this 

all_states <- map_data("state") 

# You need to merge dataset with maps one with long and lat. 
# But you need same key so lets change state to region used in maps all_states 
# Note I lowercased it to get the match 

mydf$region <- tolower(mydf$usa_state) 
totaldf <- merge(all_states, mydf, by = "region") 

# switched to data.table to fix the cut up map issue 
# getting sort by region then order 
totaldt <- as.data.table(totaldf) 
setkey(totaldt, region, order) 

ggplot(data = totaldt, 
     aes(x = long, y = lat, group = group, fill = HS.Grad)) + 
    geom_polygon() + coord_map() + 
    scale_fill_gradientn("", colours=brewer.pal(9, "GnBu")) 

Non dimenticate di SELEZIONARE ME

Se i dati non sono ordinati correttamente per regione e quindi per ordine, il n otterrai mappe disomogenee come questa.

sliced up map

Io uso data.table pacchetto e fondamentali i dati. Anche data.table è molto più veloce se hai bisogno di unire molti dati. Usa il formato X [Y] per questo. Vedi data.table cheatsheet se sei nuovo in questo pacchetto.

mappa finale

Questo è per la HS.Grid nel tuo esempio. Ottenere il vostro altro grafico cambiando il fill = myvariable

map example for HS.grid

nota si dimostra che non tutti gli stati, in quanto i dati di prova è limitata a questi stati. In un esempio più completo vedrai più stati.

Inoltre vedrai che l'Alaska è scomparso. Non è nelle mappe - vedi this answer da @jazzurro per test pratici sui nomi di stato con setdiff.

Problemi correlati