rnso rnso - 25 days ago 6
R Question

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

Answer

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

Comments