我有以下数据集:
conf variable value
1 C1 T1 0.6578166
2 C2 T1 0.6729989
3 C3 T1 0.6629129
4 C4 T1 0.6602419
5 C5 T1 0.6951054
6 C1 T2 0.6764889
7 C2 T2 0.7008580
8 C3 T2 0.6921985
9 C4 T2 0.6868215
10 C5 T2 0.7104877
11 C1 T3 0.6834098
12 C2 T3 0.7120974
13 C3 T3 0.7045760
14 C4 T3 0.6986261
15 C5 T3 0.7180474
16 C1 T4 0.6875082
17 C2 T4 0.7185950
18 C3 T4 0.7122448
19 C4 T4 0.7051376
20 C5 T4 0.7229975
21 C1 T5 0.6902167
22 C2 T5 0.7228775
23 C3 T5 0.7167748
24 C4 T5 0.7093825
25 C5 T5 0.7261777
....
我想将这些数据绘制为带有误差线的线图。为此,我使用了此
网站中定义的
summarySE
函数。我希望该图是一个线图,其中 x 轴代表 variable
,y 轴代表 value
,并按 conf
分组。考虑到从 T1
到当前变量点的值,应随每个变量一起显示误差线。例如,T6
中误差线的考虑值应该是从T1
到T6
的值。
基于此,我将
summarySE
函数称为:
m2 <- summarySE(m, measurevar = "value", groupvars = c("variable","conf") , na.rm=TRUE)
我得到的结果是:
variable conf N value sd se ci
1 T1 C1 1 0.6578166 NA NA NA
2 T1 C2 1 0.6729989 NA NA NA
3 T1 C3 1 0.6951054 NA NA NA
4 T1 C4 1 0.6629129 NA NA NA
5 T1 C5 1 0.6602419 NA NA NA
6 T2 C1 1 0.6764889 NA NA NA
......
我还尝试了以下方法:
m2 <- summarySE(m, measurevar = "value", groupvars = c("conf") , na.rm=TRUE)
我得到了以下结果:
conf N value sd se ci
1 C1 60 0.7038542 0.009609437 0.001240573 0.002482381
2 C2 60 0.7395447 0.012869318 0.001661422 0.003324497
3 C3 60 0.7418830 0.010397609 0.001342326 0.002685987
4 C4 60 0.7365407 0.014438260 0.001863971 0.003729798
5 C5 60 0.7301954 0.014389284 0.001857649 0.003717146
但这并不能绘制每个变量的误差线。
绘制情节:
coverage_plot <- ggplot(m2, aes(x=m$variable, y=m$value, group=conf, color=conf)) +
geom_errorbar(aes(ymin=value-ci, ymax=value+ci), width=0.1, position=pd) +
geom_line(position=pd) + geom_point(position=pd) +
scale_x_discrete(labels = c(1:60))
当我运行代码时,我还收到以下错误:
Error: Aesthetics must be either length 1 or the same as the data (5): ymin, ymax, x, y, group, colour
有人可以帮我解决这个问题吗?
您可以采用封装设计来构建平均图,例如
superb
(带有误差线的汇总图)。以下是一些变化:
library(superb)
# basic plot; bars are used by default
superb(value ~ conf, dta)
#line plot
superb(value ~ conf, dta, plotStyle = "line")
#line plot with some additional directive for customizing it
library(ggplot2)
superb(value ~ conf, dta, plotStyle = "line") +
theme_bw() + ylab("Variable")
尝试像这样删除
$
中的aes
coverage_plot <- ggplot(m2, aes(x=variable, y=value, group=conf, color=conf)) +
geom_errorbar(aes(ymin=value-ci, ymax=value+ci), width=0.1, position=pd) +
geom_line(position=pd) + geom_point(position=pd) +
scale_x_discrete(labels = c(1:60))
您的数据中也没有名为变量的列。确保您写的是正确的名字