要在地图上绘制的条形图

时间:2014-06-15 16:13:18

标签: r dictionary plot rworldmap

我想在此页面上绘制条形图:http://docs.oracle.com/cd/E16162_01/web.1112/e16181/dv_map.htm#BEHHAFEJ

example

我使用了以下数据和代码,但它不起作用。

ddf = read.table(header=T, sep=",", text="
country, val1, val2, val3
Nigeria,5,10,8
China,8,12,20
Brazil,15,9,20
Italy,9,25,5
Australia,15,5,25
")

require (rworldmap)

sPDF <- joinCountryData2Map(ddf
    , joinCode = "NAME"
    , nameJoinColumn = "country"
    , verbose = TRUE)

mapBars( sPDF, 
       , nameZs=c(ddf$val1, ddf$val2, ddf$val3)
       , symbolSize=4 )

我该如何纠正?请帮忙。


编辑: 我尝试在下面的答案中使用代码,但我得到一个错误的空窗口图:par中的错误(plt = c(xyx,xyy),new = TRUE):   为图形参数指定的无效值&#34; plt&#34; enter image description here

1 个答案:

答案 0 :(得分:6)

您可以尝试使用子图:

# ...
plot(getMap())
df <- merge(x=ddf, y=sPDF@data[sPDF@data$ADMIN, c("ADMIN", "LON", "LAT")], by.x="country", by.y="ADMIN", all.x=TRUE)
require(TeachingDemos)
for (i in 1:nrow(df)) 
  subplot(barplot(height=as.numeric(as.character(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))
legend("topright", legend=names(df[, 2:4]), fill=rainbow(3))

enter image description here