我正在使用以下代码在地图上绘制条形图:Bars to be plotted over map
ddf = read.table(header=T, sep=",", text="
country, val1, val2, val3
Nigeria,5,10,8
China,25,12,20
Brazil,50,9,20
Italy,75,25,5
Australia,100,5,25
")
ddf = structure(list(country = structure(c(5L, 3L, 2L, 4L, 1L), .Label = c("Australia",
"Brazil", "China", "Italy", "Nigeria"), class = "factor"), val1 = c(5L,
25L, 50L, 75L, 100L), val2 = c(10L, 12L, 9L, 25L, 5L), val3 = c(8L,
20L, 20L, 5L, 25L)), .Names = c("country", "val1", "val2", "val3"
), class = "data.frame", row.names = c(NA, -5L))
require (rworldmap)
sPDF <- joinCountryData2Map(ddf
, joinCode = "NAME"
, nameJoinColumn = "country"
, verbose = TRUE)
plot(getMap())
require(TeachingDemos)
df <- cbind(ddf, sPDF@data[sPDF@data$ADMIN %in% ddf$country, c("LON", "LAT")])
for (i in 1:nrow(df))
subplot(barplot(height=unlist(df[i, 2:4], use.names=F),
axes=F,
col=rainbow(3), ylim=range(df[,2:4])),
x=df[i, 'LON'], y=df[i, 'LAT'], size=c(.3, .3))
条形图的绘制得非常好,但尺寸不一致。情节看起来如下(注意最高的第一个酒吧是在尼日利亚,而不是澳大利亚和巴西的第一个酒吧应该比中国还要大):
答案 0 :(得分:1)
背后的原因是你正在做 blind cbind ... sPDF@data[sPDF@data$ADMIN %in% ddf$country, c("LON", "LAT")]
返回的数据与ddf中的seq不同。试试这个
df <- merge(x = ddf, y = sPDF@data[sPDF@data$ADMIN %in% ddf$country, c('ADMIN',"LON", "LAT")],
by.x= 'country', by.y = 'ADMIN')
for (i in 1:nrow(df))
subplot(barplot(height=unlist(df[i, 2:4], use.names=F),
axes=F,
col=rainbow(3), ylim=range(df[,2:4])),
x=df[i, 'LON'], y=df[i, 'LAT'], size=c(.3, .3))
此后的结果将是
在上图中,所有条形图的范围都相同,即您在代码段中提到的5到100个。