我正在尝试使用 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")
最佳答案
这是部分解决方案:
- 我删除了
geom_errorbar()
中的width
参数 - 我更喜欢使用
position =position_dodge()
- 在
position_dodge()
中针对重叠文本的geom_text
尝试不同的width
值。
ggplot(data, aes(x = Feature_Set, y = value, fill = Trajectory)) +
geom_bar(stat = "identity", position = position_dodge()) +
coord_polar() +
scale_y_continuous(breaks = 0:nlevels(data$Trajectory)) +
geom_text(aes(y = value + 20, label = value), position = position_dodge(width = 0.8)) +
geom_errorbar(aes(ymin = value - SD, ymax = value + SD), position = position_dodge()) +
xlab("Feature Set") + ylab("Predictive Accuracy")
关于r - 极坐标条形图ggplot2 R中的误差线和标签位置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49912880/