书面作业:
在课程资源中下载作业素材“WHO数据集”,选取10个你认为具有典型性的国家/地区和10项指标变量,画出热力图展示这10个国家各项指标的情况
解答:
#读数据
WHO <- read.csv("WHO.csv")
#选择10行10列
rows <- c("United Kingdom", "United States of America", "Turkey", "South Africa", "Singapore",
"Russia", "Nigeria", "Mexico", "China", "Japan")
cols <- c("Total_income",
"Adolescent_fertility_rate",
"Agricultural_land",
"Agriculture_contribution_to_economy",
"Aid_given",
"Aid_received",
"Aid_received_total",
"Arms_exports",
"Arms_imports",
"CO2_emissions")
rownames(WHO) <- WHO[,1]
data <- WHO[rows,cols]
data_matrix <- t(scale(data.matrix(data)))
pal=brewer.pal(6,"Blues")
par(mar = c(1,17,21,4),oma=c(0.2,0.2,0.2,0.2),mex=0.5)
#Heat map
image(x=1:nrow(data_matrix),y=1:ncol(data_matrix),
z=data_matrix,xlab="",ylab="",col=pal,axes=FALSE)
text(1:nrow(data_matrix), par("usr")[4] + 1,
srt = 30, adj = 0,labels = cols,
xpd = TRUE, cex=0.85)
#Y axis labels
axis(side=2,at=1:ncol(data_matrix),
labels=colnames(data_matrix),
col="white",las=1, cex.axis=0.85)
#White separating lines
abline(h=c(1:ncol(data_matrix))+0.5,
v=c(1:nrow(data_matrix))+0.5,
col="white",lwd=1,xpd=F)
#Graph Title
text(par("usr")[1]+5, par("usr")[4] + 12,
"10个国家10项指标热力图",
xpd=TRUE,font=2,cex=1.5)