2012-02-08 37 views
6

Sử dụng ggplot2 lớn Hadley và cuốn sách của ông, tôi có thể sản xuất lô đồ choropleth đơn một cách dễ dàng, sử dụng mã như thế này:Lưới với bản đồ choropleth trong ggplot2

states.df <- map_data("state") 
states.df = subset(states.df,group!=8) # get rid of DC 
states.df$st <- state.abb[match(states.df$region,tolower(state.name))] # attach state abbreviations 

states.df$value = value[states.df$st] 

p = qplot(long, lat, data = states.df, group = group, fill = value, geom = "polygon", xlab="", ylab="", main=main) + opts(axis.text.y=theme_blank(), axis.text.x=theme_blank(), axis.ticks = theme_blank()) + scale_fill_continuous (name) 
p2 = p + geom_path(data=states.df, color = "white", alpha = 0.4, fill = NA) + coord_map(project="polyconic") 

đâu " giá trị "là vectơ của dữ liệu cấp trạng thái mà tôi đang vẽ. Nhưng nếu tôi muốn vẽ nhiều bản đồ, được nhóm lại bởi một số biến (hoặc hai)?

Dưới đây là một ví dụ về một plot done by Andrew Gelman, later adapted in the New York Times, về quan điểm chăm sóc sức khỏe ở các bang:

enter image description here

Tôi rất muốn để có thể bắt chước ví dụ này: lô chương choropleth chấn chia vạch theo hai biến (hoặc thậm chí một). Vì vậy, tôi vượt qua không phải là một vector của các giá trị, mà là một khung dữ liệu được tổ chức "dài", với nhiều mục nhập cho mỗi trạng thái.

Tôi biết ggplot2 có thể làm điều này, nhưng tôi không chắc chắn như thế nào. Cảm ơn!

Trả lời

7

Bạn có thể thêm hai cột cho các nhóm mong muốn và khía cạnh sử dụng:

library(ggplot2) 
library(maps) 
d1 <- map_data("state") 
d2 <- unique(d1$group) 
n <- length(d2) 
d2 <- data.frame( 
    group=rep(d2,each=6), 
    g1=rep(1:3,each=2,length=6*n), 
    g2=rep(1:2,length=6*n), 
    value=runif(6*n) 
) 
d <- merge(d1, d2, by="group") 
qplot(
    long, lat, data = d, group = group, 
    fill = value, geom = "polygon" 
) + 
    facet_wrap(~ g1 + g2) 
+0

Tác phẩm này hoạt động. Điều quan trọng là lệnh phối hợp mở rộng khung dữ liệu đang nổi lên từ map_data, và sau đó tùy chọn facet_wrap hoạt động chính xác theo cách nó sẽ cho ggplot2. Cảm ơn! – bshor

3

tôi sẽ chỉ đưa vào đoạn script này ở đây bán buôn. Nó là khép kín, và tôi chỉ tạo ra một số biến phân loại tùy ý và một DV ngẫu nhiên theo đó các trạng thái được tô màu. Có một số thứ trong mã không cần thiết; xin lỗi vì điều đó.

rm(list = ls()) 
install.packages("ggplot2") 
library(ggplot2) 
install.packages("maps") 
library(maps) 
install.packages("mapproj") 
library(mapproj) 
install.packages("spatstat") 
library(spatstat) 

theme_set(theme_bw(base_size = 8)) 
options(scipen = 20) 

MyPalette <- colorRampPalette(c(hsv(0, 1, 1), hsv(7/12, 1, 1))) 

### Map ### 
StateMapData <- map_data("state") 
head(StateMapData) 

### Some Invented Data ### 

IndependentVariable1 <- c("Low Income", "Mid Income", "High Income") 
IndependentVariable2 <- c("18-29", "30-44", "45-64", "65+") 

# Here is one way to "stack" lots of copies of the shapefile dataframe on top of each other: 
# This needs to be done, because (as far as I know) ggplot2 needs to have the state names and polygon coordinates 
# for each level of the faceting variables. 

TallData <- expand.grid(1:nrow(StateMapData), IndependentVariable1, IndependentVariable2) 
TallData <- data.frame(StateMapData[TallData[, 1], ], TallData) 
colnames(TallData)[8:9] <- c("IndependentVariable1", "IndependentVariable2") 

# Some random dependent variable we want to plot in color: 
TallData$State_IV1_IV2 <- paste(TallData$region, TallData$IndependentVariable1, TallData$IndependentVariable2) 
RandomVariable <- runif(length(unique(TallData$State_IV1_IV2))) 
TallData$DependentVariable <- by(RandomVariable, unique(TallData$State_IV1_IV2), mean)[TallData$State_IV1_IV2] 

### Plot ### 

MapPlot <- ggplot(TallData, 
aes(x = long, y = lat, group = group, fill = DependentVariable)) 
MapPlot <- MapPlot + geom_polygon() 
MapPlot <- MapPlot + coord_map(project="albers", at0 = 45.5, lat1 = 29.5) # Changes the projection to something other than Mercator. 
    MapPlot <- MapPlot + scale_x_continuous(breaks = NA, expand.grid = c(0, 0)) + 
    scale_y_continuous(breaks = NA) + 
    opts(
     panel.grid.major = theme_blank(), 
     panel.grid.minor = theme_blank(), 
     panel.background = theme_blank(), 
     panel.border = theme_blank(), 
     expand.grid = c(0, 0), 
     axis.ticks = theme_blank(), 
     legend.position = "none", 
     legend.box = "horizontal", 
     title = "Here is my title", 
    legend.key.size = unit(2/3, "lines")) 
MapPlot <- MapPlot + xlab(NULL) + ylab(NULL) 
MapPlot <- MapPlot + geom_path(fill = "transparent", colour = "BLACK", alpha = I(2/3), lwd = I(1/10)) 
MapPlot <- MapPlot + scale_fill_gradientn("Some/nRandom/nVariable", legend = FALSE, 
colours = MyPalette(100)) 

# This does the "faceting": 
MapPlot <- MapPlot + facet_grid(IndependentVariable2 ~ IndependentVariable1) 

# print(MapPlot) 

ggsave(plot = MapPlot, "YOUR DIRECTORY HERE.png", h = 8.5, w = 11) 
+0

Điều này cũng hoạt động - expand.grid đang thực hiện cùng một công việc hợp nhất tại đây. Thêm vào đó, câu trả lời này có rất nhiều chạm nhẹ và tùy chọn mà tôi sẽ thích hợp! Cảm ơn. – bshor

Các vấn đề liên quan