2012-02-08 72 views
6

使用哈德利的伟大GGPLOT2和他的书,我能够产生轻松单一地区分布图绘制,使用这样的代码:网格等值线图中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") 

在哪里“价值“是我绘制的州级数据的矢量。但是如果我想绘制多个地图,按一些变量(或两个)进行分组呢?

这里有一个plot done by Andrew Gelman, later adapted in the New York Times的一个例子,关于在美国的医疗保健意见:

enter image description here

我很想能够效仿这一榜样:显示等值线图,根据两个变量网格化(或甚至一个)。所以我传递的不是一个值的向量,而是一个数据帧组织的“长”,每个状态有多个条目。

我知道ggplot2可以做到这一点,但我不知道如何。谢谢!

回答

7

您可以添加两个列所需的分组和使用方面:

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

This Works。关键是合并命令,它扩展了从map_data出现的数据帧,然后是facet_wrap选项,它的工作方式与ggplot2完全相同。谢谢! – bshor 2012-02-09 00:14:33

3

我就在这里贴上这个脚本批发。它是独立的,我只是产生一些任意的分类变量和一个随机的DV,通过这些随机的DV对各个州进行着色。代码中有一些不需要的东西;我为此道歉。

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

这也适用 - expand.grid在这里进行相同的合并工作。此外,这个答案有很多很好的小动作和选项,我会适当的!谢谢。 – bshor 2012-02-09 00:14:08