ggplot2:基于geom_area中的组着色

时间:2016-02-09 06:17:02

标签: r ggplot2

感谢您的回复!我试图弄清楚输出是什么。

我有一个数据集d,就像从文件中读取一样。

   pass site value region
1     0    1     1      A
2     0    2    10      A
3     0    3    10      A
4     0    4    10      B
5     0    5    10      B
6     0    6    10      C
7     0    7    10      C
8     0    8    10      C
9     0    9    10      D
10    0   10    10      E
11    0   11     0      E
12    1    1   100      A
13    1    2    25      A
14    1    3    34      A
15    1    4    54      B
16    1    5    64      B
17    1    6    98      C
18    1    7    59      C
19    1    8    92      C
20    1    9    37      D
21    1   10    96      E
22    1   11    20      E
23    2    1    93      A
24    2    2    80      A
25    2    3     3      A
26    2    4    86      B
27    2    5    10      B
28    2    6    81      C
29    2    7     7      C
30    2    8    60      C
31    2    9    90      D
32    2   10     0      E
33    2   11   200      E

我根据该段落中的站点值绘制每个站点中每个站点的比例。我希望网站按区域值着色。我已经按区域颜色进行着色,但我希望该地区的每个网站都是我选择的三种颜色之一。这些数据集将长达数千个站点,因此我需要循环浏览三种颜色中的每一种。我附上了一些我做过的例子。

该示例确实为我提供了图表,但使用了默认颜色。

ggplot(data=d, aes(x=rev(pass),y=rev(value),group=site)) + 
  geom_area(aes(fill=factor(rev(r_var))), position = "Fill")

Example 1

下一个示例仅循环显示区域A的颜色的第一个选项(灰色/黑色的所有阴影),而不是转到下一个区域颜色。

r_var<-d$region
number = 3000
ggplot(data=d, aes(x=rev(pass),y=rev(value),group=site)) + 
  geom_area(aes(fill=factor(rev(r_var))), position = "Fill") + 
  scale_fill_manual(values = c(rep(c("#000000", "#151111", "#131111"),length.out = number),
                               rep(c("#231652", "#0e72ef", "#2c46c9"),length.out = number),
                               rep(c("#808080", "#a39f9f", "#514e4e"),length.out = number),
                               rep(c("#a50000", "#ff0000", "#e10000"),length.out = number),
                               rep(c("#56e729", "#b6ff15", "#6aff0c"),length.out = number)))

Example 2

最后一个示例输出一个没有数据的图表。

r_var<-d$region
number = 3000
ggplot(data=d, aes(x=rev(pass),y=rev(value),group=site)) + 
  geom_area(aes(fill=factor(rev(r_var))), position = "Fill") + 
  scale_fill_manual(values = c("A" =rep(c("#000000", "#151111", "#131111"),length.out = number),
                               "B" = rep(c("#231652", "#0e72ef", "#2c46c9"),length.out = number),
                               "C" = rep(c("#808080", "#a39f9f", "#514e4e"),length.out = number),
                               "D" = rep(c("#a50000", "#ff0000", "#e10000"),length.out = number),
                               "E" = rep(c("#56e729", "#b6ff15", "#6aff0c"),length.out = number)))

1 个答案:

答案 0 :(得分:0)

仅供参考,我使用此代码处理数据框d:

small_d<-subset(d, pass == 0)
count_d<-aggregate(data.frame(count = small_d$region), list(value = small_d$region), length)

color.codes = c(rep(c("#000000", "#151111", "#131111"),length.out = count_d[1,2]),
                rep(c("#231652", "#2c46c9","#000099"),length.out = count_d[2,2]),
                rep(c("#404040", "#808080", "#514e4e"),length.out = count_d[3,2]),
                rep(c("#a50000", "#F505F5", "#ff0000"),length.out = count_d[4,2]),
                rep(c("#56e729", "#b6ff15", "#2FB531"),length.out = count_d[5,2]),
                rep(c("#909090", "#C8C8C8", "#B0B0B0"),length.out = count_d[6,2]),
                rep(c("#ff9900","#ffff00","#cc9900"),length.out = count_d[7,2]),
                rep(c("#00ffff","#99ffff","#00ccff"),length.out = count_d[8,2]),
                rep(c("#000000", "#151111", "#131111"),length.out = count_d[9,2]))

 ggplot(data=d,aes(rev(pass),rev(value),fill=rev(factor(interaction(region,site))),group=site))+
      geom_area(position="Fill")+
      scale_fill_manual(values = color.codes)