2

バージニア州の一部で郵便番号のコロプレスを作成して、会社のデータを表示しようとしています。最後の行を除いて、すべてを正しく実行できますaes(fill = growth)。そこで、エラーが発生します:

エラー: 美学は長さ 1 であるか、dataProblems:growth と同じ長さでなければなりません

ここに私のデータがあります:

私のコード:

library(ggplot2)
library(maptools)
library(rgdal)
library(plyr)

#set working directory
setwd("F:/Dropbox/Zip Codes")

#load Shapefile NOVA
Zips <- readOGR(dsn="F:/Dropbox/Zip Codes", layer="NOVA")

#load Company Data of Zip Codes
Company <- read.csv("Data.csv")

#set to data.frame
Company_df <- data.frame(Company)

#create growth vector
growth = Company_df[,'Growth']

#merge growth vector into Zips
Zips$growth = growth

#ggplot
Nmap = ggplot(Zips) +
aes(x = long, y = lat, group=group) +
geom_polygon() +
aes(fill = growth)
Nmap
4

1 に答える 1

4

ディレクトリ構造を少し違った方法で編成しました。インターネット上の古いコード スニペットにもかかわらず、データをデータ フレームにバインドする必要はありません。ただし、ggplot で使用するにはポリゴン必要です。fortifyまた、read.csvdata.frame を作成するため、その呼び出しから再作成する必要はありません。

library(ggplot2)
library(maptools)
library(rgdal)
library(ggthemes)

setwd("~/Development/nova_choro")

zips <- readOGR(dsn="zip_codes/NOVA.shp", layer="NOVA")
company <- read.csv("data.csv")

# this makes the polygons usable to ggplot2
# and by using ZCTA5CE10 as the region id, 
# you can then use that equivalent id field 
# from the actual company data frame for the
# choropleth colors

zips_map <- fortify(zips, region="ZCTA5CE10")

gg <- ggplot()
# draw the base map polygons
gg <- gg + geom_map(data=zips_map, map=zips_map,
                    aes(x=long, y=lat, map_id=id),
                    color="#7f7f7f", fill="white", size=0.25)
# fill in the polygons
gg <- gg + geom_map(data=company, map=zips_map,
                    aes(fill=Growth, map_id=zip_code_area),
                    color="#7f7f7f", size=0.25)
# better color scheme
gg <- gg + scale_fill_distiller(palette="Greens")
# no "slashes" in the legend boxes
gg <- gg + guides(fill=guide_legend(override.aes=list(colour=NA)))
# use an actual projection (there may be a better one for NOVA
gg <- gg + coord_map()
# get rid of map chart junk
gg <- gg + theme_map()
gg

ここに画像の説明を入力

私はいくつかのチェックを行い、VA は修正されたランベルト円錐正角図法を使用しているため、必要に応じてデフォルトのメルカトル図法を置き換えることができますcoord_mapgg <- gg + coord_map("lambert", lat0=38.34427, lat1=39.14084)これは、公式機関が使用するものに十分近いはずです。

于 2015-07-26T22:32:34.117 に答える