가 여기에 ggplot
솔루션입니다. 당신은 기본 R 방법을 요구하고있는 것처럼 보이지만, IMO는 ggplot을 배워서 유연성을 높이는 것이 가치가 있습니다. 이렇게하면 시작할 수 있습니다.
귀하가 제공하지 않았으므로 귀하의 데이터 형식에 대해 몇 가지 가정을해야했습니다. 아래 코드는이 예제에서 데이터가 어떻게 구성되어 있는지를 보여줍니다.
# 2010 and 2014 Congressional Election results in Virginia.
data.2010 <- data.frame(district=sprintf("%02i",1:11),
R=c(63.9,53.1,27.2,62.3,50.8,76.3,59.2,37.3,51.2,62.9,48.8),
D=c(34.8,42.4,70.0,37.5,47.0,00.0,34.1,61.0,46.4,34.8,49.2))
data.2014 <- data.frame(district=sprintf("%02i",1:11),
R=c(63.0,57.9,000.0,60.3,61.0,75.5,60.9,31.7,74.9,56.6,40.4),
D=c(34.5,42.1,100.0,37.5,35.8,00.0,36.9,63.0,00.0,40.4,56.9))
data <- cbind(year=rep(c(2010,2014),each=11),rbind(data.2010,data.2014))
data$delta <- with(data,D-R)
head(data)
# year district R D delta
# 1 2010 01 63.9 34.8 -29.1
# 2 2010 02 53.1 42.4 -10.7
# 3 2010 03 27.2 70.0 42.8
# 4 2010 04 62.3 37.5 -24.8
# 5 2010 05 50.8 47.0 -3.8
# 6 2010 06 76.3 0.0 -76.3
이 그림은지도를 표시합니다. 6 및 9 지구가 경쟁하는 동안 2014 년 3 지구에서 민주당이 반대가 실행하지만, 실행중인 민주당이 없음을
는
library(ggplot2)
library(rgdal)
setwd("c:/users/jlh/desktop/map/virginia.congressional.districts")
map <- readOGR(dsn=".",layer="tl_rd13_51_cd113")
map.data <- data.frame(id=rownames([email protected]),[email protected]$CD113FP)
map.data <- merge(map.data,data)
map.df <- fortify(map)
map.df <- merge(map.df,map.data)
ggplot(map.df,aes(x=long,y=lat,group=group))+
geom_polygon(aes(fill=delta),color="grey20")+
facet_wrap(~year,nr=2)+
scale_fill_gradient2(low="red",high="blue",mid="white",limits=c(-100,100))+
coord_map()+labs(x="",y="")+
theme_bw()+
theme(panel.grid=element_blank(),
panel.border=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank())
![enter image description here](https://i.stack.imgur.com/GhLTH.png)
참고.
게시물 here 및 here에는 워크 플로에 대한 설명이 있습니다.