我正在尝试通过此页面上的代码制作堆积条形图:Bars to be plotted over map
但是下面包含= WALSE的代码仍然会绘制并排条形图而不是堆积条形图。
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)
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, beside=FALSE,
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))
可能是什么原因,我该如何解决这个问题?
答案 0 :(得分:1)
为了除了工作之外,hight
必须是一个包含1列的矩阵。所以试试:
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))
height <- matrix(as.numeric(as.character(unlist(df[i, 2:4], use.names=F))), ncol=1)
subplot(barplot(height=height,
axes=F, beside=FALSE,
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))