楼主: dabap
5521 1

[程序分享] 堆叠图,颜色为什么如此分配?? [推广有奖]

  • 0关注
  • 0粉丝

大专生

46%

还不是VIP/贵宾

-

威望
0
论坛币
2 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
928 点
帖子
17
精华
0
在线时间
63 小时
注册时间
2012-4-1
最后登录
2016-8-8

楼主
dabap 发表于 2015-11-24 22:52:41 |AI写论文
50论坛币
  1. library(ggplot2)
  2. library(plyr)
  3. library(grid)
  4. rm(list=ls())
  5. ##################
  6. #变量

  7. raw_data <- t(matrix(c(
  8.   31, 35,        86,        45,        106,        149,
  9.   35, 33,        88,        42,        101,        144,
  10.   28, 33,        78,        28,        107,        151,
  11.   34, 32,        96,        39,        113,        170,
  12.   31, 30,        70,        52,        100,        129,
  13.   50, 37,        94,        28,        108,        163,
  14.   36, 38,        82,        42,        109,        158,
  15.   35, 29,        81,        38,        102,  146,
  16.   35, 37,        86,        42,        105,        149
  17. ),6,9))

  18. type_name <- c('SO2','C0','N02','03_8h','PM10','PM2.5')

  19. position <- c('奥体中心','草场门','迈皋桥','仙林大学城','浦口','瑞金路','山西路','玄武湖','中华门')
  20. ################## IAQI排序
  21. b=sort(colSums(raw_data),index.return=TRUE,decreasing = TRUE)$ix
  22. my_color = c('orange','springgreen3','purple','blue','red','yellow')

  23. j<-1:6
  24. name_new <- type_name[b[j]]

  25. raw_data_1 = raw_data
  26. type_name1 = type_name

  27. for (i in 1:6){
  28.   raw_data_1[,i] = raw_data[,b[i]]
  29.   type_name1[i]   = type_name[b[i]]
  30. }
  31. ###################
  32. #变量合并

  33. data_new <- rep(0,6*9)
  34. type <- rep("",6*9)
  35. pos <- rep("",6*9)

  36. for(i in 1:9){
  37.   for(j in 1:6){
  38.     index <- (i-1)*6+j
  39.     data_new[index] <- raw_data_1[i,j]
  40.     type[index] <- type_name1[j]
  41.     pos[index] <- position[i]
  42.   }
  43. }

  44. data_all <-  data.frame(
  45.   pos,
  46.   type,
  47.   data_new
  48. )


  49. ce<-ddply(data_all, "pos", transform, label_y = cumsum(data_new)-0.5*data_new)

  50. gp <- ggplot(ce, aes(x=reorder(pos,data_new, sum), y = data_new, fill = type)) +
  51.        geom_bar(stat = "identity",width=0.7) +
  52.          scale_fill_manual(values = my_color,breaks=name_new,labels=name_new)+
  53.           geom_text(aes(y = label_y, label = data_new), vjust = 0.4, colour = "Black")+
  54.            guides(fill=guide_legend(reverse = TRUE))+
  55.               xlab("国控点位置")+ylab("Accumulation  of  IAQI")+
  56.               theme_bw()+
  57.                theme(
  58.                  plot.margin = unit(c(2,0.5,1,1),'lines'),
  59.                  axis.title.x = element_text(colour = 'black',size=15,face = 'bold',vjust=-0.5),
  60.                  axis.text.x = element_text(colour='black',size=10),
  61.                  axis.title.y = element_text(colour = 'black',size=15,face = 'bold',vjust=1.5),
  62.                  axis.text.y = element_text(colour='black',size=12)
  63.                )

  64. print(gp)
复制代码
代码和图如上所示,目前输出为我所求,但有一个疑问就是颜色的顺序为什么和我想的不太一样。我认为颜色应该是按照我的设定,一个一个去填充,如先是orange,接着是springgreen3,purple这样,为什么其中的顺序变混掉了,是哪里出了问题?虽然最终程序实现,但这个问题还是困扰着我,希望各路大神能帮我解答,谢谢啦

QQ图片20151124225552.jpg (88.4 KB)

QQ图片20151124225552.jpg

关键词:堆叠图 Library ggplot2 BRARY gplot 如何

沙发
suimong 发表于 2015-12-12 20:41:46
stack的顺序可以通过在ggplot函数里添加order这个参数来指定。例如你可以在fill=type后面加上order=desc(type)

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-16 14:19