Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
802 views
in Technique[技术] by (71.8m points)

r - Bars to be plotted over map

I want to draw barchart over map as on this page: http://docs.oracle.com/cd/E16162_01/web.1112/e16181/dv_map.htm#BEHHAFEJ

example

I used following data and code but it is not working.

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 )

How can I correct this? Please help.


Edit: I tried using code in answer below but I am getting an empty window plot with error: Error in par(plt = c(xyx, xyy), new = TRUE) : invalid value specified for graphical parameter "plt" enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could try using subplots:

# ...
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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...