统一箱线图因子组颜色 [英] Unify boxplot factor group colours

查看:188
本文介绍了统一箱线图因子组颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某种程度上是一个R和ggplot新手,所以我努力绘制这个数据作为一个盒子图与Flux在y和周上的X,与盒子图按照物种分组(和在每个物种组治疗)。

 处理物种周通量
1 L- Heisteria 1 0.19
2 L- Heisteria 1 0.03
3 L- Heisteria 1 NA
4 L- Heisteria 1 0.12
5 L- Simarouba 1 0.22
6 L- Simarouba 1 0.19
7 L- Simarouba 1 NA
8 L- Simarouba 1 -0.65
9 C Heisteria 1 -0.99
10 C Heisteria 1 0.10
11 C Heisteria 1 0.26
12 C Heisteria 1 NA
13 C Simarouba 1 -1.41
14 C Simarouba 1 0.17
15 C Simarouba 1 NA
16 C Simarouba 1 0.35
17 L + Heisteria 1 0.71
18 L + Heisteria 1 0.25
19 L + Heisteria 1 0.08
20 L + Heisteria 1 4.14
21 L + Simarouba 1 -1.36
22 L + Simarouba 1 0.06
23 L + Simarouba 1 -0.65
24 L + Simarouba 1 -0.25
25 L- Heisteria 2 0.31
26 L- Heisteria 2 0.15
27 L- Heisteria 2 -0.09
28 L- Heisteria 2 -0.08
29 L- Simarouba 2 0.04
30 L- Simarouba 2 0.06
31 L- Simarouba 2 0.05
32 L- Simarouba 2 -0.07
33 C Heisteria 2 0.20
34 C Heisteria 2 0.15
35 C Heisteria 2 -0.03
36 C Heisteria 2 0.18
37 C Simarouba 2 0.10
38 C Simarouba 2 0.08
39 C Simarouba 2 0.09
40 C Simarouba 2 0.05
41 L + Heisteria 2 0.24
42 L + Heisteria 2 0.09
43 L + Heisteria 2 0.16
44 L + Heisteria 2 0.11
45 L + Simarouba 2 NA
46 L + Simarouba 2 0.21
47 L + Simarouba 2 -0.07
48 L + Simarouba 2 1.51
49 L- Heisteria 3 0.15
50 L- Heisteria 3 0.07
51 L- Heisteria 3 NA
52 L- Heisteria 3 -1.02
53 L- Simarouba 3 -0.02
54 L- Simarouba 3 0.08
55 L- Simarouba 3 -0.06
56 L- Simarouba 3 -0.08
57 C希氏菌3 0.23
58 C希氏菌3 0.19
59 C希氏菌3 0.09
60 C Heisteria 3 -0.10
61 C Simarouba 3 0.77
62 C Simarouba 3 0.07
63 C Simarouba 3 0.20
64 C Simarouba 3 0.62
65 L + Heisteria 3 0.19
66 L + Heisteria 3 -0.09
67 L + Heisteria 3 NA
68 L + Heisteria 3 0.06
69 L + Simarouba 3 NA
70 L + Simarouba 3 -0.17
71 L + Simarouba 3 0.13
72 L + Simarouba 3 0.64
73 L- Heisteria 4 0.13
74 L- Heisteria 4 0.54
75 L- Heisteria 4 0.18
76 L- Heistoia 4 3.59
77 L- Simarouba 4 0.00
78 L- Simarouba 4 0.10
79 L- Simarouba 4 0.20
80 L- Simarouba 4 NA
81 C Heisteria 4 -0.14
82 C Heisteria 4 -0.32
83 C希氏菌4 0.21
84 C希氏菌4 0.12
85 C Simarouba 4 0.10
86 C Simarouba 4 NA
87 C Simarouba 4 0.11
88 C Simarouba 4 0.42
89 L + Heisteria 4 0.14
90 L + Heisteria 4 0.05
91 L + Heisteria 4 0.25
92 L + Heisteria 4 0.74
93 L + Simarouba 4 NA
94 L + Simarouba 4 0.05
95 L + Simarouba 4 -0.06
96 L + Simarouba 4 -0.13

我可以使用此代码绘制数据

  ggplot(treeflux,aes(Week,Flux,fill = interaction(Week,Species,Treatment),dodge = Species,Treatment))+ 
stat_boxplot(geom ='errorbar') +
geom_boxplot()

它给我一个情节,太多的颜色和项目在图例部分。我希望每个物种的治疗是单一颜色的变种,传说读得像这样L- Heisteria。

解决方案

这是一个开始? (alpha的传说需要一点调整...)这比设置一个填充颜色的整个自定义调色板容易得多...

  theme_set(theme_bw())##我的审美偏好,也更容易
##区分光与暗色
ggplot(treeflux,aes Flux,fill = Species,alpha = Treatment),
dodge = Species,Treatment)+
stat_boxplot(geom ='errorbar')+
geom_boxplot()


I'm somewhat of an R and ggplot novice so I'm struggling to plot this data as a box plot with Flux on the y and Week on the X, with the boxplots grouped by species (and within each species group treatment).

   Treatment   Species Week  Flux
1         L- Heisteria    1  0.19
2         L- Heisteria    1  0.03
3         L- Heisteria    1    NA
4         L- Heisteria    1  0.12
5         L- Simarouba    1  0.22
6         L- Simarouba    1  0.19
7         L- Simarouba    1    NA
8         L- Simarouba    1 -0.65
9          C Heisteria    1 -0.99
10         C Heisteria    1  0.10
11         C Heisteria    1  0.26
12         C Heisteria    1    NA
13         C Simarouba    1 -1.41
14         C Simarouba    1  0.17
15         C Simarouba    1    NA
16         C Simarouba    1  0.35
17        L+ Heisteria    1  0.71
18        L+ Heisteria    1  0.25
19        L+ Heisteria    1  0.08
20        L+ Heisteria    1  4.14
21        L+ Simarouba    1 -1.36
22        L+ Simarouba    1  0.06
23        L+ Simarouba    1 -0.65
24        L+ Simarouba    1 -0.25
25        L- Heisteria    2  0.31
26        L- Heisteria    2  0.15
27        L- Heisteria    2 -0.09
28        L- Heisteria    2 -0.08
29        L- Simarouba    2  0.04
30        L- Simarouba    2  0.06
31        L- Simarouba    2  0.05
32        L- Simarouba    2 -0.07
33         C Heisteria    2  0.20
34         C Heisteria    2  0.15
35         C Heisteria    2 -0.03
36         C Heisteria    2  0.18
37         C Simarouba    2  0.10
38         C Simarouba    2  0.08
39         C Simarouba    2  0.09
40         C Simarouba    2  0.05
41        L+ Heisteria    2  0.24
42        L+ Heisteria    2  0.09
43        L+ Heisteria    2  0.16
44        L+ Heisteria    2  0.11
45        L+ Simarouba    2    NA
46        L+ Simarouba    2  0.21
47        L+ Simarouba    2 -0.07
48        L+ Simarouba    2  1.51
49        L- Heisteria    3  0.15
50        L- Heisteria    3  0.07
51        L- Heisteria    3    NA
52        L- Heisteria    3 -1.02
53        L- Simarouba    3 -0.02
54        L- Simarouba    3  0.08
55        L- Simarouba    3 -0.06
56        L- Simarouba    3 -0.08
57         C Heisteria    3  0.23
58         C Heisteria    3  0.19
59         C Heisteria    3  0.09
60         C Heisteria    3 -0.10
61         C Simarouba    3  0.77
62         C Simarouba    3  0.07
63         C Simarouba    3  0.20
64         C Simarouba    3  0.62
65        L+ Heisteria    3  0.19
66        L+ Heisteria    3 -0.09
67        L+ Heisteria    3    NA
68        L+ Heisteria    3  0.06
69        L+ Simarouba    3    NA
70        L+ Simarouba    3 -0.17
71        L+ Simarouba    3  0.13
72        L+ Simarouba    3  0.64
73        L- Heisteria    4  0.13
74        L- Heisteria    4  0.54
75        L- Heisteria    4  0.18
76        L- Heisteria    4  3.59
77        L- Simarouba    4  0.00
78        L- Simarouba    4  0.10
79        L- Simarouba    4  0.20
80        L- Simarouba    4    NA
81         C Heisteria    4 -0.14
82         C Heisteria    4 -0.32
83         C Heisteria    4  0.21
84         C Heisteria    4  0.12
85         C Simarouba    4  0.10
86         C Simarouba    4    NA
87         C Simarouba    4  0.11
88         C Simarouba    4  0.42
89        L+ Heisteria    4  0.14
90        L+ Heisteria    4  0.05
91        L+ Heisteria    4  0.25
92        L+ Heisteria    4  0.74
93        L+ Simarouba    4    NA
94        L+ Simarouba    4  0.05
95        L+ Simarouba    4 -0.06
96        L+ Simarouba    4 -0.13

I can plot the data using this code

ggplot(treeflux, aes(Week, Flux, fill=interaction(Week, Species, Treatment), dodge=Species, Treatment)) + 
    stat_boxplot(geom ='errorbar') + 
    geom_boxplot()

It gives me a plot in the order I want but with way too many colours and items in the legend section. I want the treatments for each species to be variants of a single colour and the legend to read like this "L- Heisteria".

解决方案

How about this for a start? (The legend for alpha needs a little tweaking ...) This is much easier than setting up an entire custom palette of fill colours and getting the legend right ...

theme_set(theme_bw())  ## my aesthetic preference, also easier for    
                       ## distinguishing light vs dark colours
ggplot(treeflux, aes(factor(Week), Flux, fill=Species, alpha=Treatment),
   dodge=Species, Treatment) + 
stat_boxplot(geom ='errorbar') + 
geom_boxplot()

这篇关于统一箱线图因子组颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆