【发布时间】:2018-04-19 04:43:24
【问题描述】:
我正在尝试使用 R 中的 ggplot2 在每个条形图的末尾构建一个带有误差线和值标签的极坐标条形图。我遇到了一个问题,即误差线和值标签都堆叠在一起而不是单独的酒吧。有谁知道如何解决这一问题?
这是我使用的代码和数据:
structure(list(Feature_Set = c("All Features", "Depression Only",
"Depression + schiz", "Depression + schiz + AD", "Depression + schiz + AD + Cog",
"Depression + schiz + AD + Cog + BMI", "Cog_BMI_WHR", "cog_and_AD",
"AD", "Depressive Symptoms", "All Features", "Depression Only",
"Depression + schiz", "Depression + schiz + AD", "Depression + schiz + AD + Cog",
"Depression + schiz + AD + Cog + BMI", "Cog_BMI_WHR", "cog_and_AD",
"AD", "Depressive Symptoms", "All Features", "Depression Only",
"Depression + schiz", "Depression + schiz + AD", "Depression + schiz + AD + Cog",
"Depression + schiz + AD + Cog + BMI", "Cog_BMI_WHR", "cog_and_AD",
"AD", "Depressive Symptoms", "All Features", "Depression Only",
"Depression + schiz", "Depression + schiz + AD", "Depression + schiz + AD + Cog",
"Depression + schiz + AD + Cog + BMI", "Cog_BMI_WHR", "cog_and_AD",
"AD", "Depressive Symptoms"), Trajectory = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Resilient", "chronic", "emergent",
"depressed improved"), class = "factor"), value = c(65.51, 61.42,
62, 64.26, 64.99, 65.72, 60.26, 61.6, 59.98, 59.92, 85.13, 69.06,
72.2, 77.18, 80.61, 83.6, 71.85, 69.72, 66.71, 65.74, 79.5, 66.79,
70.22, 72.52, 74.87, 77.28, 69.72, 68.17, 63.15, 65.64, 77.39,
67.97, 69.18, 70.51, 73.08, 75.33, 67.19, 67.82, 68, 65.12),
SD = c(3.23, 2.75, 4.01, 3.42, 3.88, 3.23, 3.31, 4.15, 3.34,
3.98, 1.57, 2.72, 3.51, 2.53, 2.36, 2.86, 2.51, 3.58, 2.88,
1.8, 2.09, 2.44, 2.75, 2.86, 1.98, 1.96, 2.15, 1.88, 2.82,
3.87, 1.78, 2.99, 2.71, 3.28, 2.96, 1.53, 2.92, 3.1, 2.76,
2.47)), row.names = c(NA, -40L), class = "data.frame", .Names = c("Feature_Set",
"Trajectory", "value", "SD"))
数据框(以下数据用于演示目的)。
Feature_Set class . value SD
1 All Features Resilient 65.51 3.23
2 Depression Only Resilient 61.42 2.75
3 Depression + schiz Resilient 62.00 4.01
4 Depression + schiz + AD Resilient 64.26 3.42
5 Depression + schiz + AD + Cog Resilient 64.99 3.88
6 Depression + schiz + AD + Cog + BMI Resilient 65.72 3.23
7 Cog_BMI_WHR Resilient 60.26 3.31
8 cog_and_AD Resilient 61.60 4.15
9 AD Resilient 59.98 3.34
10 Depressive Symptoms Resilient 59.92 3.98
11 All Features chronic 85.13 1.57
12 Depression Only chronic 69.06 2.72
13 Depression + schiz chronic 72.20 3.51
14 Depression + schiz + AD chronic 77.18 2.53
15 Depression + schiz + AD + Cog chronic 80.61 2.36
16 Depression + schiz + AD + Cog + BMI chronic 83.60 2.86
17 Cog_BMI_WHR chronic 71.85 2.51
18 cog_and_AD chronic 69.72 3.58
19 AD chronic 66.71 2.88
20 Depressive Symptoms chronic 65.74 1.80
21 All Features emergent 79.50 2.09
22 Depression Only emergent 66.79 2.44
23 Depression + schiz emergent 70.22 2.75
24 Depression + schiz + AD emergent 72.52 2.86
25 Depression + schiz + AD + Cog emergent 74.87 1.98
26 Depression + schiz + AD + Cog + BMI emergent 77.28 1.96
27 Cog_BMI_WHR emergent 69.72 2.15
28 cog_and_AD emergent 68.17 1.88
29 AD emergent 63.15 2.82
30 Depressive Symptoms emergent 65.64 3.87
31 All Features depressed improved 77.39 1.78
32 Depression Only depressed improved 67.97 2.99
33 Depression + schiz depressed improved 69.18 2.71
34 Depression + schiz + AD depressed improved 70.51 3.28
35 Depression + schiz + AD + Cog depressed improved 73.08 2.96
36 Depression + schiz + AD + Cog + BMI depressed improved 75.33 1.53
37 Cog_BMI_WHR depressed improved 67.19 2.92
38 cog_and_AD depressed improved 67.82 3.10
39 AD depressed improved 68.00 2.76
40 Depressive Symptoms depressed improved 65.12 2.47
代码:
ggplot(data,aes(x=Feature_Set,y=value,fill=Trajectory))+
geom_bar(stat="identity",position="dodge")+
coord_polar() +
scale_y_continuous(breaks = 0:nlevels(data$Trajectory)) +
geom_text(aes(y = value +20,label = value))+
geom_errorbar(aes(ymin=value-SD, ymax=value+SD), width=.2, position="identity") +
xlab("Feature Set")+ylab("Predictive Accuracy")
结果:
根据接受的答案,我更新了代码作为其他有类似问题的示例:
ggplot(data,aes(x=Feature_Set,y=value,fill=Trajectory))+
geom_bar(stat="identity",position="dodge")+
coord_polar() +
scale_y_continuous(breaks = 0:nlevels(data$Trajectory)) +
geom_text(position = position_dodge(.9), aes(y = value +10,label = value))+
geom_errorbar(aes(ymin=value-SD, ymax=value+SD), position=position_dodge(.9)) +
#geom_point(position=position_dodge(.9), aes(y=value, colour=Trajectory)) +
xlab("Feature Set")+ylab("Predictive Accuracy")
【问题讨论】:
-
请您粘贴
dput(data)的结果。它将更容易重新创建数据框;您当前的示例在我们需要编辑的列中有很多空格。 -
您在
geom_bar中使用position = "dodge",但在geom_errorbar中使用position = "identity"。在geom_errorbar中设置position = "dodge"可能会解决这个问题。 -
@neilfws,感谢您的建议。我添加了那个
-
@bVa,我试过了,但不幸的是它没有改变任何东西