4

我想为明天到期的作业绘制一个带有置信区间的交互图。这是我的数据集:

Snail0

    water   lightness   total_migration
    control dark    9.5
    control dark    20
    control dark    22
    control dark    24
    control dark    10
    control dark    18
    control dark    19
    control dark    19.5
    control dark    23
    control dark    2
    control dark    10.5
    control dark    22.5
    control dark    26
    control dark    32
    control dark    2
    control dark    12.5
    control dark    13.5
    control dark    16.5
    control dark    19
    control dark    20
    control dark    21
    control dark    22
    control dark    24
    control dark    25
    control light   11
    control light   20.5
    control light   22.5
    control light   24
    control light   21
    control light   23
    control light   23.5
    control light   28
    control light   30
    control light   18.5
    control light   19.5
    control light   19.5
    control light   23.5
    control light   24
    control light   2
    control light   20
    control light   23
    control light   28.5
    control light   29
    control light   16
    control light   17.5
    control light   26.5
    control light   28.1
    control light   30.5
    crab    light   6
    crab    light   14
    crab    light   15
    crab    light   17
    crab    light   19
    crab    light   7
    crab    light   14
    crab    light   17
    crab    light   22
    crab    light   26
    crab    light   4.5
    crab    light   10
    crab    light   11
    crab    light   21
    crab    light   24
    crab    light   14
    crab    light   20
    crab    light   22.5
    crab    light   23
    crab    light   14
    crab    light   16
    crab    light   18.5
    crab    light   22
    crab    light   23
    crab    dark    14
    crab    dark    19
    crab    dark    20
    crab    dark    23
    crab    dark    25
    crab    dark    9
    crab    dark    18
    crab    dark    18
    crab    dark    20
    crab    dark    21
    crab    dark    15
    crab    dark    15
    crab    dark    15
    crab    dark    19
    crab    dark    23
    crab    dark    12
    crab    dark    14.5
    crab    dark    21
    crab    dark    21.5
    crab    dark    22
    crab    dark    14.5
    crab    dark    16
    crab    dark    19
    crab    dark    20
    crab    dark    22.5
    snail   dark    6
    snail   dark    15
    snail   dark    20
    snail   dark    22
    snail   dark    15
    snail   dark    15
    snail   dark    16
    snail   dark    17
    snail   dark    21
    snail   dark    15
    snail   dark    20
    snail   dark    15
    snail   dark    16
    snail   dark    19.5
    snail   dark    23
    snail   dark    24
    snail   dark    4
    snail   dark    18.5
    snail   dark    20
    snail   dark    24
    snail   dark    25
    snail   light   13
    snail   light   13
    snail   light   13
    snail   light   18
    snail   light   15
    snail   light   16
    snail   light   17
    snail   light   17
    snail   light   19
    snail   light   14
    snail   light   14
    snail   light   19
    snail   light   7
    snail   light   13
    snail   light   13
    snail   light   13
    snail   light   16
    snail   light   3
    snail   light   9
    snail   light   15
    snail   light   22
    snail   light   23

interaction.plot(Snail0$water,Snail0$lightness,Snail0$total_migration) 

我一直在尝试使用ToothGrowth 数据在此处底部完成的操作,但显然我做错了,因为我只收到错误消息。

Snail0$water <- factor(Snail0$water)


df<-with(Snail0, aggregate(total_migration, list(lightness=lightness, water=water), mean))


df$se<-with(Snail0, aggregate(total_migration, list(lightness=lightness, water=water), function(x) sd(x)/sqrt(10)))[,3]


gp<-ggplot(df, aes(x=water, y=total_migration, colour=lightness, group=lightness))

gp+geom_line(aes(linetype=lightness)) + geom_point(aes(shape=lightness)) + geom_errorbar(aes(ymax=x+se, ymin=x-se))

错误信息:

Error: Aesthetics must either be length one, or the same length as the dataProblems:lightness, water, lightness, lightness

显然,我不知道自己在做什么,所以有人可以帮我纠正我的代码吗?

4

1 回答 1

3

当您df使用第一个aggregate命令创建对象时,包含因变量的列不再称为total_migration但是x

因此,如果您在命令中替换total_migration为,您的代码将起作用:xggplot

gp <- ggplot(df, aes(x=water, y=x, colour=lightness, group=lightness))
gp + geom_line(aes(linetype=lightness)) + 
     geom_point(aes(shape=lightness)) + 
     geom_errorbar(aes(ymax=x+se, ymin=x-se))

在此处输入图像描述

于 2012-11-19T12:39:30.357 回答