当与facet_wrap()一起使用时,为什么geom_rect()添加许多多层?

时间:2019-07-19 07:58:54

标签: r ggplot2 facet-wrap

我要结合使用geom_rect()和facet_wrap()来添加颜色条,但是由于某些原因,添加了30层,这意味着即使我使用alpha = 0.2,颜色条也完全是深色的。 我可以导出到Powerpoint并手动删除所有多余的行,但这是一个巨大的痛苦。有办法解决这个问题吗?

我试图重新启动终端并仅加载所需的程序包,认为可能是对已加载的函数发生了错误,但不,似乎并非如此。

dat <- structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 
22L, 22L, 22L, 22L), .Label = c("alat", "asat", "chol", "cl", 
"fhb", "ft3", "ft4", "ggt", "gluk", "hdlch", "hrs", "k", "kreae", 
"ldh", "ldlch", "lipa", "mg", "na", "phos", "prot", "trig", "tsh"
), class = "factor"), diff_A = c(0.03, -0.02, -0.01, 0, -0.03, 
-0.01, -0.01, 0.03, 0.02, 0, 0.01, 0.0099999999999999, 0, 0.02, 
0.01, 0, -0.03, -0.02, -0.01, 0.02, 0, -0.01, 0.02, 0.02, 0, 
0.03, -0.02, -0.01, 0, 0.00999999999999995, 0, 0, 0.01, 0, -0.0299999999999999, 
0.04, 0.03, -0.04, 0, 0.02, 0.03, -0.03, -0.01, -0.05, -0.07, 
-0.01, -0.00999999999999995, 0.02, 0, 0.0099999999999999, 0.01, 
0.06, -0.04, 0.01, 0.06, -0.03, 0.01, 0.03, 0.02, 0, 0.02, 0, 
0.00999999999999995, 0.00999999999999995, -0.01, 0.01, -0.02, 
0.02, 0.02, 0.13, 0.0999999999999996, 0.2, 0, 0, 0.3, 0, -0.2, 
0.100000000000001, 0, -0.2, 0.0999999999999996, -0.1, 0, -0.300000000000001, 
-0.0999999999999996, 0, 0, -0.2, 0.100000000000001, -0.100000000000001, 
-0.3, -0.0999999999999996, -0.0999999999999996, 0.3, 0, 0.2, 
0, 0, 0.100000000000001, -0.2, 0, 0, -0.199999999999999, 0.0999999999999996, 
-0.0999999999999996, 1, -1, 0, 0, 3, -1, 0, 0, -1, -2, 0, -2, 
0, -1, 1, 0, -2, -2, 0, 1, -1, 1, -1, 3, -2, 0, 0, -1, -1, 1, 
0, 0, -1, 0, 0, 1, 0, 2, 1, -1, 1, 0, 3, 2, -3, 4, 1, -2, 2, 
1, 3, 0, 2, 2, 4, -2, -1, 1, 1, 1, 1, 1, 3, 5, 0, 1.1, -1, 1, 
1, 1, 0.23, -0.71, 0.21, 0.0599999999999996, -0.4, 0.59, -0.0299999999999994, 
0.0899999999999999, 0.15, -0.0700000000000003, -0.04, -0.0999999999999996, 
0.13, -0.79, -0.27, -0.18, -0.0600000000000001, -0.26, 0.24, 
0.63, -0.0500000000000007, -0.28, -0.31, 0.43, -0.2, -0.0499999999999998, 
0.149999999999999, -0.319999999999999, 0.0999999999999996, 0.34, 
0.0499999999999998, -0.1, 0.3, 0.0699999999999998, 0.0600000000000001, 
0.699999999999999, 0.6, 0, 0.300000000000001, -0.199999999999999, 
-0.0299999999999994, -0.299999999999999, -0.0999999999999996, 
-0.199999999999999, 0.0999999999999996, 0, 0.0999999999999979, 
0.0999999999999996, -0.0999999999999996, -0.200000000000001, 
-0.0299999999999994, -0.300000000000001, -0.9, -0.0999999999999996, 
0.5, -0.5, 0.0999999999999996, -0.0999999999999996, 0.4, -0.200000000000001, 
0.300000000000001, 0, -0.199999999999999, -0.4, 0.4, -0.0999999999999996, 
0.5, 0.800000000000001, -0.100000000000001, 0.5, 0.02, 0.01, 
-0.02, -0.01, 0.05, 0, 0.02, 0, -0.00999999999999995, 0, -0.01, 
0.0599999999999999, -0.01, 0.03, 0.01, 0.04, 0.07, 0.05, -0.01, 
-0.06, 0.03, -0.03, 0, -0.03, 0.04, 0.01, -0.01, 0, 0.02, -0.03, 
0.02, 0.03, 0.03, -0.02, 0, -0.0999999999999996, 0.100000000000001, 
0.0999999999999996, -0.199999999999999, -0.4, -0.6, -0.0999999999999996, 
0.2, 0, 0.1, -0.0999999999999996, 0.0999999999999996, -0.1, 0.0999999999999996, 
-0.100000000000001, 0.0999999999999996, -0.7, -0.2, 0.4, 0.399999999999999, 
-0.0999999999999996, -0.0999999999999996, -0.100000000000001, 
-0.2, -0.100000000000001, 0.100000000000001, -0.0999999999999996, 
-0.1, 0.100000000000001, -0.3, 0, 0, -0.300000000000001, -0.1, 
0.3, 0.01, -0.02, 0.01, -0.0900000000000001, 0.11, 0.00999999999999979, 
-0.01, -0.01, 0.04, -0.0699999999999998, -0.04, -0.03, 0.03, 
-0.0399999999999998, 0.1, 0, 0.03, -0.0700000000000001, -0.0599999999999998, 
0.04, 0.03, 0.12, -0.0900000000000001, 0.1, -0.0600000000000001, 
0.0700000000000001, 0.02, 0, -0.0399999999999998, 0.0900000000000003, 
-0.02, -0.03, 0.03, 0.11, 0, 1, 2, 0, 1, 20, 8, 4, 9, -12, -23, 
1, -13, -2, 2, -10, 0, 2, 2, 2, 2, 7, 9, -7, 6, 1, -9, -3, 0, 
-12, 12, -2, 1, 14, -3, 4, 0, 0, 0.1, 0, 0.0999999999999996, 
-0.0999999999999996, 0, 0.1, 0, -0.1, 0, 0, -0.0999999999999996, 
0, 0, 0, -0.3, -0.0999999999999996, 0.1, 0.1, 0, -0.1, 0, 0, 
0.1, -0.2, 0.1, 0, 0, 0, 0, 0, 0, 0, 0.2, -3, 1, 4, 0, -2, 0, 
-1, 3, 1, -3, -5, -2, -1, -4, -2, -1, -3, -8, 4, 0, -14, 6, 1, 
16, -14, 1, 5, 1, -2, 7, 0, -8, 3, -2, -2, 0.0800000000000001, 
0.12, 0.04, 0.0600000000000001, 0.0499999999999998, -0.26, 0.0600000000000001, 
0.0499999999999998, 0.42, 0.02, 0.1, 0.3, 0, 0.32, 0.02, 0.11, 
0.0900000000000003, 0.0600000000000001, -0.2, 0.26, -0.14, -0.32, 
0.27, -0.24, 0.0300000000000002, 0.0799999999999996, 0.14, 0.59, 
0.25, 0.02, 0.11, 0.0500000000000003, 0.13, 0.27, 0.14, 0, 0.0100000000000002, 
-0.02, 0, -0.11, -0.12, -0.02, -0.13, -0.02, 0.1, 0, 0.17, 0.11, 
-0.14, 0.0500000000000003, 0.00999999999999979, 0.02, -0.0900000000000003, 
-0.0599999999999998, 0.04, -0.0899999999999999, -0.0899999999999999, 
0.13, 0.32, -0.22, 0.14, 0.00999999999999979, 0.04, -0.11, -0.01, 
0.0299999999999998, 0.0800000000000001, -0.34, 0.04, -0.2, 0.04, 
0.0799999999999996, 0.0299999999999998, 0.0499999999999998, 0.19, 
-0.0100000000000007, 0, 0.17, -0.0800000000000001, -0.12, 0.15, 
0.00999999999999979, 0.15, 0.1, -0.0299999999999998, 0.04, -0.15, 
-0.22, 0.17, 0.0899999999999999, -0.26, -0.2, 0.1, 0.2, -0.46, 
0.02, 0.13, -0.0100000000000002, -0.01, 0.0299999999999998, -0.1, 
-0.18, -0.11, -0.0899999999999999, -0.11, 0.01, -0.01, 0.02, 
0.0199999999999999, -0.01, 0.03, -0.01, 0.03, 0, -0.02, 0, 0, 
0.02, -0.04, 0.05, 0, 0.0299999999999999, 0.01, 0.0399999999999999, 
0.0499999999999999, -0.0599999999999999, -0.01, -0.01, 0.03, 
-0.0299999999999999, -0.01, -0.03, -0.01, 0.02, 0.01, -0.03, 
0, 0.0499999999999999, -0.05, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, -3, 
0, -1, 0, -2, 2, 0, 1, 0, 0, 1, -1, 1, 1, 0, 0, 0, 2, -1, -1, 
1, -1, -1, -1, 0, 0, 0.01, 0.05, -0.02, 0.01, 0.02, -0.0299999999999998, 
-0.02, 0.03, 0.03, 0.01, 0.04, 0.21, -0.03, -0.0700000000000001, 
0.01, -0.0299999999999999, -0.08, -0.0600000000000001, 0.02, 
0, -0.0699999999999998, -0.03, 0.03, -0.02, 0, -0.03, 0, -0.05, 
0.02, 0.01, -0.0399999999999999, 0, 0.03, 0.01, 0.01, 1, 0, 0, 
0, 2, -1, -2, 3, -1, -1, 1, -1, 0, 0, 0, -2, 1, 2, -3, -1, 2, 
1, 1, 1, -1, 2, 1, 1, 1, 2, 0, -1, 3, -2, -1, 0.01, 0, -0.02, 
0.04, 0, -0.04, 0.03, -0.0299999999999999, -0.01, -0.01, 0.01, 
0.01, 0.01, 0.02, 0.03, -0.09, 0.04, -0.0600000000000001, 0.05, 
0.05, -0.0499999999999998, -0.0199999999999999, 0.01, 0.05, -0.0599999999999999, 
0.0699999999999998, 0, 0.02, -0.01, -0.05, -0.02, 0.02, 0, 0.0399999999999998, 
-0.0399999999999998, 0.01, -0.03, -0.02, -0.01, 0.02, 0.0600000000000001, 
-0.05, 0, -0.12, -0.13, -0.03, 0, -0.0600000000000001, 0.03, 
-0.01, 0, 0.02, 0.04, -0.0600000000000001, 0.035, -0.02, 0.0309999999999999, 
0.0599999999999998, 0.01, 0.03, 0.0500000000000003, -0.0399999999999996, 
0.0499999999999998, 0, 0.00800000000000001, 0, -0.00900000000000001, 
0.14, 0, -0.025), MD_Fuss = c(0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.54, 0.54, 
0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 
0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 
0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 
6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 
6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 
6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 
6.49, 6.49, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 
0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 
0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 
0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.81, 0.81, 0.81, 0.81, 0.81, 
0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 
0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 
0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.35, 
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 
0.35, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 
0.16, 0.16, 0.16, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 
12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 
12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 
12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 
12.84, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 
16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 
16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 
16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 
16.79, 16.79, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 
0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 
0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 
0.36, 0.36, 0.36, 0.36, 0.36, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 
0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 
0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 
0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.35, 0.35, 0.35, 0.35, 
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.06, 0.06, 
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 
5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 
5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 
5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 
5.21, 5.21, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 
0.16, 0.16, 0.16, 0.16, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 
3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 
3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 
3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.16, 0.16, 0.16, 
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16)), class = "data.frame", row.names = c(NA, 
-770L))

这是情节:

p <- ggplot(data=dat, aes(y = diff_A))+ 
  geom_boxplot(outlier.shape = 1)+
  geom_rect(aes(ymin = -MD_Fuss, ymax = MD_Fuss), xmin = -Inf, xmax =Inf, alpha = 0.2)+
  theme_bw()+ theme(panel.grid = element_blank())+ 
  xlab('')+   ylab('[mmol/L]') +
  scale_y_continuous(expand = c(0.5, 0))+
  facet_wrap(.~variable, scales = 'free')
p

1 个答案:

答案 0 :(得分:2)

geom_rect()为数据中的每一行绘制一个矩形。要使每个构面仅获得一个矩形,您需要向其传递一个数据集,该数据集每个构面变量仅包含一行。由于MD_Fussvariable中似乎是恒定的,因此可以使用unique(dat[, c("variable", "MD_Fuss")])创建该数据集,然后将其作为data参数传递给geom_rect()

library(ggplot2)

p <- ggplot(data = dat) +
  geom_boxplot(aes(y = diff_A), outlier.shape = 1) +
  geom_rect(
    data = unique(dat[, c("variable", "MD_Fuss")]),
    aes(ymin = -MD_Fuss, ymax = MD_Fuss),
    xmin = -Inf, xmax = Inf, alpha = 0.2,
  ) +
  theme_bw() + theme(panel.grid = element_blank()) +
  xlab("") + ylab("[mmol/L]") +
  scale_y_continuous(expand = c(0.5, 0)) +
  facet_wrap(. ~ variable, scales = "free")

p
#> Warning: Removed 2 rows containing missing values (geom_rect).

reprex package(v0.3.0.9000)于2019-07-19创建