geom_segment可变长度错误

时间:2016-12-10 18:32:29

标签: r ggplot2

在更新到2.2.0之前,以下代码在ggplot2中有效。现在我得到Error: Aesthetics must be either length 1 or the same as the data (30): x, y, xend, yend。该错误是由两个geom_segment调用引起的。

drug1 <- c(.7, -1.6, -.2, -1.2, -.1, 3.4, 3.7, .8, 0, 2)
drug2 <- c(1.9, .8, 1.1, .1, -.1, 4.4, 5.5, 1.6, 4.6, 3.4)
d <- data.frame(Drug=c(rep('Drug 1', 10), rep('Drug 2', 10),
                  rep('Difference', 10)),
                extra=c(drug1, drug2, drug2 - drug1))

ggplot(d, aes(x=Drug, y=extra)) + 
  geom_boxplot(col='lightyellow1', alpha=.3, width=.5) + 
  geom_dotplot(binaxis='y', stackdir='center', position='dodge') +
  stat_summary(fun.y=mean, geom="point", col='red', shape=18, size=5) +
  geom_segment(aes(x=rep('Drug 1', 30), xend=rep('Drug 2', 30), y=drug1, yend=drug2),
               col=gray(.8)) +
  geom_segment(aes(x='Drug 1', xend='Difference', y=drug1, yend=drug2 - drug1),
               col=gray(.8)) +
  xlab('') + ylab('Extra Hours of Sleep') + coord_flip()

更新:改进的代码:

drug1 <- c(.7, -1.6, -.2, -1.2, -.1, 3.4, 3.7, .8, 0, 2)
drug2 <- c(1.9, .8, 1.1, .1, -.1, 4.4, 5.5, 1.6, 4.6, 3.4)
d <- data.frame(Drug=c(rep('Drug 1', 10), rep('Drug 2', 10),
                  rep('Difference', 10)),
                extra=c(drug1, drug2, drug2 - drug1))
w <- data.frame(drug1, drug2, diff=drug2 - drug1)

ggplot(d, aes(x=Drug, y=extra)) +
  geom_boxplot(col='lightyellow1', alpha=.3, width=.5) + 
  geom_dotplot(binaxis='y', stackdir='center', position='dodge') +
  stat_summary(fun.y=mean, geom="point", col='red', shape=18, size=5) +
  geom_segment(data=w, aes(x='Drug 1', xend='Drug 2', y=drug1, yend=drug2),
               col=gray(.8)) +
  geom_segment(data=w, aes(x='Drug 1', xend='Difference', y=drug1, yend=drug2 - drug1),
               col=gray(.8)) +
  xlab('') + ylab('Extra Hours of Sleep') + coord_flip()

1 个答案:

答案 0 :(得分:1)

代码的更新版本会生成数据框d,如下所示:

drug1 <- c(.7, -1.6, -.2, -1.2, -.1, 3.4, 3.7, .8, 0, 2)
drug2 <- c(1.9, .8, 1.1, .1, -.1, 4.4, 5.5, 1.6, 4.6, 3.4)
d <- data.frame(Drug=c(rep('Drug 1', 10), rep('Drug 2', 10),
                  rep('Difference', 10)),
                extra=c(drug1, drug2, drug2 - drug1))

> d
         Drug extra
1      Drug 1   0.7
2      Drug 1  -1.6
3      Drug 1  -0.2
4      Drug 1  -1.2
5      Drug 1  -0.1
6      Drug 1   3.4
7      Drug 1   3.7
8      Drug 1   0.8
9      Drug 1   0.0
10     Drug 1   2.0
11     Drug 2   1.9
12     Drug 2   0.8
13     Drug 2   1.1
14     Drug 2   0.1
15     Drug 2  -0.1
16     Drug 2   4.4
17     Drug 2   5.5
18     Drug 2   1.6
19     Drug 2   4.6
20     Drug 2   3.4
21 Difference   1.2
22 Difference   2.4
23 Difference   1.3
24 Difference   1.3
25 Difference   0.0
26 Difference   1.0
27 Difference   1.8
28 Difference   0.8
29 Difference   4.6
30 Difference   1.4

这是一种创建数据框架的有问题的方法,原因有两个:

  1. 变量drug1drug2存在于全局环境和data.frame d中。这会产生混淆,掩盖和其他错误的可能性。

  2. Difference与产生差异的值相关联的唯一方法是行排序。例如,第1行和第11行中的值产生了第21行的差异。如果您稍后对数据集进行任何修改,则会产生问题。

  3. 我建议以这样的方式创建数据框:

    d2 <- data.frame(
      pair = 1:10,
      drug1 = c(.7, -1.6, -.2, -1.2, -.1, 3.4, 3.7, .8, 0, 2),
      drug2 = c(1.9, .8, 1.1, .1, -.1, 4.4, 5.5, 1.6, 4.6, 3.4)
    ) 
    
       pair drug1 drug2
    1     1   0.7   1.9
    2     2  -1.6   0.8
    3     3  -0.2   1.1
    4     4  -1.2   0.1
    5     5  -0.1  -0.1
    6     6   3.4   4.4
    7     7   3.7   5.5
    8     8   0.8   1.6
    9     9   0.0   4.6
    10   10   2.0   3.4
    

    有一个明确的pair变量可以链接这些值,drug1drug2之外不存在d2tidyr的额外副本。

    然后,您可以使用tidyr::gather(d2, drug, value, drug1, drug2) pair drug value 1 1 drug1 0.7 2 2 drug1 -1.6 3 3 drug1 -0.2 4 4 drug1 -1.2 5 5 drug1 -0.1 6 6 drug1 3.4 7 7 drug1 3.7 8 8 drug1 0.8 9 9 drug1 0.0 10 10 drug1 2.0 11 1 drug2 1.9 12 2 drug2 0.8 13 3 drug2 1.1 14 4 drug2 0.1 15 5 drug2 -0.1 16 6 drug2 4.4 17 7 drug2 5.5 18 8 drug2 1.6 19 9 drug2 4.6 20 10 drug2 3.4 转换为整齐/长格式(与ggplot和建模软件包一起使用):

    Agent(const std::string& _name, Room *const starting_room)
            : name(_name), current_room(starting_room) { }