悬赏 1 个论坛币 未解决
代码如下:
library(ggplot2)
raw_data <- t(matrix(c(
  31, 35,        86,        45,        106,        149,
  35, 33,        88,        42,        101,        144,
  28, 33,        78,        28,        107,        151,
  34, 32,        96,        39,        113,        170,
  31, 30,        70,        52,        100,        129,
  50, 37,        94,        28,        108,        163,
  36, 38,        82,        42,        109,        158,
  35, 29,        81,        38,        102,  146,
  35, 37,        86,        42,        105,        149
),6,9))
pos_max <- rep(0,9)
for (i in 1:9){
  pos_max[i]=max(raw_data[i,1:6])
}
sre <- sort(pos_max, index.return=TRUE)
sort_index <- sre$ix
type_name <- c('SO2','C0','N02','03_8h','PM10','PM2.5')
position <- c('奥体中心','草场门','迈皋桥','仙林大学城','浦口','瑞金路','山西路','玄武湖','中华门')
data <- rep(0,6*9)
type <- rep("",6*9)
pos <- rep("",6*9)
for(i in 1:9){
  for(j in 1:6){
    index <- (i-1)*6+j
    data[index] <- raw_data[i,j]
    type[index] <- type_name[j]
    pos[index] <- position[i]
  }
}
data_all <-  data.frame(
  data,
  type,
  pos
)
gp <- ggplot(data=data_all, aes(x=reorder(data_all$pos, data_all$data, max), y=data_all$data, fill=data_all$type)) + geom_bar(stat="identity")
for(i in 1:9){
  ik <- sort_index[i]
  for(j in 1:6){
    gp <- gp + annotate("text", x=i, y=sum(raw_data[ik,1:j])-raw_data[ik,j]/2, label=as.character(raw_data[ik,j]))
  }
}
gp
该代码出图,我想让它的y轴按照reorder(data_all$type, data_all$data, max)的顺序显示,请问代码应该如何调整?在不改动data.frame结构的前提下。