如何在垂直方面的条形图上方添加平均分隔字母?
How to add mean separation letters above bars in a vertical facet?
我正在尝试制作一个包含三个垂直条形图的图形。我可以在 ggplot2 中使用 Facets 成功地做到这一点,但想知道如何在每个图的每个条上方添加平均分隔字母。数据在方面 (1-3) 中按收获分隔。请在下面找到示例数据,然后是方面的代码,然后是我为成功添加分隔符的收获之一创建的最终代码。谢谢:
Rootstock Harvest N Length.cm. sd se ci Lower Upper
1 A 1 8 76.51075 36.00585368 12.72999165 30.10164697 63.78075835 89.24074165
2 A 2 8 160.9014 59.41736724 21.00721165 49.67416212 139.8941884 181.9086116
3 A 3 8 325.2713625 117.812772 41.653105 98.49394223 283.6182575 366.9244675
4 B 1 8 71.09765 38.1718855 13.49579954 31.91249489 57.60185046 84.59344954
5 B 2 8 142.1246375 67.33487775 23.80647433 56.29336655 118.3181632 165.9311118
6 B 3 8 336.24105 115.3372241 40.77786664 96.42433239 295.4631834 377.0189166
7 C 1 8 46.3798 25.53625659 9.028430101 21.34884477 37.3513699 55.4082301
8 C 2 8 118.4134875 65.35360032 23.10598698 54.63697717 95.30750052 141.5194745
9 C 3 8 281.5792375 117.049729 41.38332856 97.85602232 240.1959089 322.9625661
10 D 1 7 86.62565714 37.50832694 14.17681503 34.6894167 72.44884212 100.8024722
11 D 2 6 120.86055 48.8806253 19.95543172 51.2970703 100.9051183 140.8159817
12 D 3 8 274.203525 86.86426453 30.71115524 72.62034249 243.4923698 304.9146802
13 E 1 8 57.2146875 26.41120188 9.337769974 22.08031734 47.87691753 66.55245747
14 E 2 8 120.2972875 45.39353788 16.04903923 37.94994738 104.2482483 136.3463267
15 E 3 8 333.678325 103.5042683 36.594285 86.53173379 297.08404 370.27261
16 F 1 8 81.166225 37.18034784 13.14523804 31.08354867 68.02098696 94.31146304
17 F 2 8 161.96095 71.60747702 25.31706629 59.86534893 136.6438837 187.2780163
18 F 3 7 331.6084286 195.9165314 74.04948854 181.1925711 257.55894 405.6579171
19 G 1 8 68.743 44.9696775 15.89918195 37.59559123 52.84381805 84.64218195
20 G 2 8 142.6372625 80.95226642 28.62094827 67.67778838 114.0163142 171.2582108
21 G 3 8 362.7983625 143.4288665 50.70976205 119.9095331 312.0886004 413.5081246
22 H 1 8 55.2445625 31.35474448 11.08557622 26.21322238 44.15898628 66.33013872
23 H 2 8 125.48165 97.10938809 34.33335342 81.18548013 91.14829658 159.8150034
24 H 3 8 402.004975 176.9811861 62.57229841 147.9599743 339.4326766 464.5772734
25 I 1 7 63.47242857 30.17893187 11.40656408 27.91085683 52.06586449 74.87899265
26 I 2 8 167.4317875 76.30008692 26.97615443 63.78846899 140.4556331 194.4079419
27 I 3 8 357.2169125 108.6952357 38.42956911 90.8714911 318.7873434 395.6464816
28 J 1 8 65.6122875 29.5111406 10.43376382 24.67193096 55.17852368 76.04605132
29 J 2 8 126.4079875 66.06781069 23.35849848 55.23407198 103.049489 149.766486
30 J 3 8 502.1878625 140.3556285 49.62320833 117.3402419 452.5646542 551.8110708
31 K 1 8 57.3303625 27.86618278 9.852183406 23.29671181 47.47817909 67.18254591
32 K 2 8 106.203025 52.69142229 18.629231 44.05113142 87.573794 124.832256
33 K 3 7 348.9141 166.4786335 62.92300897 153.9670564 285.991091 411.837109
34 L 1 8 54.4373375 32.89032536 11.62848605 27.49700012 42.80885145 66.06582355
35 L 2 8 173.801975 114.7929316 40.58543019 95.9692925 133.2165448 214.3874052
36 L 3 8 427.5621 141.5160672 50.03348538 118.3103929 377.5286146 477.5955854
37 N 1 8 53.19965 18.75340803 6.630330993 15.67824146 46.56931901 59.82998099
38 N 2 8 102.83935 49.66950295 17.56082118 41.52474363 85.27852882 120.4001712
39 N 3 8 361.586525 123.0931912 43.52001511 102.9084831 318.0665099 405.1065401
40 M 1 8 90.71195 45.58629605 16.11718953 38.11109723 74.59476047 106.8291395
41 M 2 8 201.219475 90.14563026 31.87129323 75.36363289 169.3481818 233.0907682
42 M 3 8 521.6052875 144.9878341 51.26094033 121.2128627 470.3443472 572.8662278
43 O 1 8 72.572875 37.76520057 13.35201471 31.57249778 59.22086029 85.92488971
44 O 2 8 181.783975 97.67988452 34.53505436 81.66242708 147.2489206 216.3190294
45 O 3 8 415.6914625 179.8261041 63.57812883 150.3383853 352.1133337 479.2695913
46 P 1 8 58.817375 22.82069356 8.068333583 19.07857726 50.74904142 66.88570858
47 P 2 8 131.3006875 67.37113752 23.8192941 56.32368048 107.4813934 155.1199816
48 P 3 8 286.8072625 49.36617664 17.45357913 41.27115649 269.3536834 304.2608416
49 Q 1 8 76.4929625 50.22446933 17.75703142 41.98870713 58.73593108 94.24999392
50 Q 2 8 174.730975 111.6447534 39.47238111 93.33734965 135.2585939 214.2033561
51 Q 3 8 363.4492125 169.6025304 59.96354967 141.7912638 303.4856628 423.4127622
52 R 1 8 72.9998875 24.48056866 8.655188055 20.46626758 64.34469945 81.65507555
53 R 2 8 172.1736375 97.8407617 34.59193304 81.79692377 137.5817045 206.7655705
54 R 3 8 453.68845 177.718465 62.83296587 148.5763549 390.8554841 516.5214159
带有 Facet 的代码:
library(ggplot2)
AllHarvest<-summarySE(CombinedwinrhizoSigmaPlot, measurevar="Length.cm.", groupvars=c("Rootstock","Harvest"))
AllHarvest$Lower <- AllHarvest$Length.cm. - AllHarvest$se
AllHarvest$Upper <- AllHarvest$Length.cm. + AllHarvest$se
AllHplot<-ggplot(AllHarvest,aes(x=Rootstock, y=Length.cm., fill=Rootstock)) + ylim(0,600) + geom_bar(colour = "black", stat="identity", width = 0.7, fill = "#4DAF4A") + geom_errorbar(aes(ymax=Upper, ymin=Lower, width = 0.2), size=0.5, color = "black")
AllHplot + facet_grid(Harvest ~ .)
Harvest #3 的代码,包括柱上方的平均分隔字母:
Harvest3 <- AllHarvest[which(Harvest =="3"),]
Harvest3summary <- summarySE(Harvest3, measurevar="Length.cm.", groupvars=c("Rootstock"))
offset.v = -8 # offsets for mean letters
offset.h = 0.4
Harvest3summary$Lower <- Harvest3summary$Length.cm. - Harvest3summary$se
Harvest3summary$Upper <- Harvest3summary$Length.cm. + Harvest3summary$se
Harvest3plot <- ggplot(Harvest3summary,aes(x=Rootstock, y=Length.cm., fill=Rootstock)) + ylim(0,600) +
geom_bar(colour = "black", stat="identity", width = 0.7, fill = "#4DAF4A") + geom_errorbar(aes(ymax=Upper, ymin=Lower, width = 0.2), size=0.5, color = "black") +
geom_text(aes(label=c("CDE", "CDE", "DE", "DE","CDE","E","BCDE","ABCDE","ABCDE","AB","CDE", "ABC", "ABCDE","A","ABCD","CDE","BCDE","ABC"), size = 8, hjust=offset.h, vjust=offset.v)) +
labs(x = "", y = expression(Total~Root~Length~(cm))) + theme(legend.position = "none") +
## ggtitle("Main title") +
theme(
plot.title = element_text(size = rel(1.5),
face = "bold", vjust = 1.5),
axis.title = element_text(face = "bold"),
axis.title.y = element_text(vjust= 1.8, size=14),
axis.text.y=element_text(size=12),
axis.title.x = element_text(vjust= -0.5),
axis.text.x = element_text(angle=60, hjust=1.1,vjust=1.05, size=14, colour="black"))
panel.border = element_rect(colour="black")
环顾四周后,我通过反复试验能够回答我自己的问题。我正在为那些将来可能想使用它的人添加代码——这对于需要平均分隔符的期刊数字特别有用。请注意 - 使用 geom_text
时,您将分隔字母作为一个长字符串添加到构面中的所有图形中,因此请确保它们的顺序正确:
AllHplot<-ggplot(AllHarvest,aes(x=Rootstock, y=Length.cm., fill=Rootstock)) + coord_cartesian(ylim=c(0,650)) + geom_blank()+ theme_bw() + geom_bar(colour = "black", stat="identity", width = 0.7, fill = "black") + geom_errorbar(aes(ymax=Upper, ymin=Lower, width = 0.2), size=0.5, color = "black")+
labs(x = "", y = expression(paste(Total~Root~Length~(cm)))) + scale_y_continuous(expand = c(0,0)) +
theme(
axis.title = element_text(face = "bold"),
axis.title.y = element_text(face="bold", vjust= 1.8, size=14),
axis.text.y=element_text(size=12, colour="black"),
axis.title.x = element_text(vjust= -0.5),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
axis.text.x = element_text(angle=60, hjust=1.1, vjust=1.05, size=14, colour="black"),
strip.background = element_blank(),
strip.text = element_blank())+ geom_text(aes(label=c("CDE", "CDE", "DE", "DE","CDE","E","BCDE","ABCDE","ABCDE","AB","CDE", "ABC", "ABCDE","A","ABCD","CDE","BCDE","ABC", "ABC", "ABCDEF", "CDEF", "BCDEF","BCDEF","ABC","BCDEF","EF","ABC","CDEF","DEF", "ABCD", "F","A","AB","BCDEF","ABCDE","ABC", "ABC", "ABC", "D", "AB","BCD","AB","ABCD","CD","ABCD","ABCD","BCD", "CD", "BCD","A","ABC","ABCD","ABC","ABC"), hjust=offset.h, vjust=offset.v)) + facet_grid(Harvest ~ .,as.table=F)
我正在尝试制作一个包含三个垂直条形图的图形。我可以在 ggplot2 中使用 Facets 成功地做到这一点,但想知道如何在每个图的每个条上方添加平均分隔字母。数据在方面 (1-3) 中按收获分隔。请在下面找到示例数据,然后是方面的代码,然后是我为成功添加分隔符的收获之一创建的最终代码。谢谢:
Rootstock Harvest N Length.cm. sd se ci Lower Upper
1 A 1 8 76.51075 36.00585368 12.72999165 30.10164697 63.78075835 89.24074165
2 A 2 8 160.9014 59.41736724 21.00721165 49.67416212 139.8941884 181.9086116
3 A 3 8 325.2713625 117.812772 41.653105 98.49394223 283.6182575 366.9244675
4 B 1 8 71.09765 38.1718855 13.49579954 31.91249489 57.60185046 84.59344954
5 B 2 8 142.1246375 67.33487775 23.80647433 56.29336655 118.3181632 165.9311118
6 B 3 8 336.24105 115.3372241 40.77786664 96.42433239 295.4631834 377.0189166
7 C 1 8 46.3798 25.53625659 9.028430101 21.34884477 37.3513699 55.4082301
8 C 2 8 118.4134875 65.35360032 23.10598698 54.63697717 95.30750052 141.5194745
9 C 3 8 281.5792375 117.049729 41.38332856 97.85602232 240.1959089 322.9625661
10 D 1 7 86.62565714 37.50832694 14.17681503 34.6894167 72.44884212 100.8024722
11 D 2 6 120.86055 48.8806253 19.95543172 51.2970703 100.9051183 140.8159817
12 D 3 8 274.203525 86.86426453 30.71115524 72.62034249 243.4923698 304.9146802
13 E 1 8 57.2146875 26.41120188 9.337769974 22.08031734 47.87691753 66.55245747
14 E 2 8 120.2972875 45.39353788 16.04903923 37.94994738 104.2482483 136.3463267
15 E 3 8 333.678325 103.5042683 36.594285 86.53173379 297.08404 370.27261
16 F 1 8 81.166225 37.18034784 13.14523804 31.08354867 68.02098696 94.31146304
17 F 2 8 161.96095 71.60747702 25.31706629 59.86534893 136.6438837 187.2780163
18 F 3 7 331.6084286 195.9165314 74.04948854 181.1925711 257.55894 405.6579171
19 G 1 8 68.743 44.9696775 15.89918195 37.59559123 52.84381805 84.64218195
20 G 2 8 142.6372625 80.95226642 28.62094827 67.67778838 114.0163142 171.2582108
21 G 3 8 362.7983625 143.4288665 50.70976205 119.9095331 312.0886004 413.5081246
22 H 1 8 55.2445625 31.35474448 11.08557622 26.21322238 44.15898628 66.33013872
23 H 2 8 125.48165 97.10938809 34.33335342 81.18548013 91.14829658 159.8150034
24 H 3 8 402.004975 176.9811861 62.57229841 147.9599743 339.4326766 464.5772734
25 I 1 7 63.47242857 30.17893187 11.40656408 27.91085683 52.06586449 74.87899265
26 I 2 8 167.4317875 76.30008692 26.97615443 63.78846899 140.4556331 194.4079419
27 I 3 8 357.2169125 108.6952357 38.42956911 90.8714911 318.7873434 395.6464816
28 J 1 8 65.6122875 29.5111406 10.43376382 24.67193096 55.17852368 76.04605132
29 J 2 8 126.4079875 66.06781069 23.35849848 55.23407198 103.049489 149.766486
30 J 3 8 502.1878625 140.3556285 49.62320833 117.3402419 452.5646542 551.8110708
31 K 1 8 57.3303625 27.86618278 9.852183406 23.29671181 47.47817909 67.18254591
32 K 2 8 106.203025 52.69142229 18.629231 44.05113142 87.573794 124.832256
33 K 3 7 348.9141 166.4786335 62.92300897 153.9670564 285.991091 411.837109
34 L 1 8 54.4373375 32.89032536 11.62848605 27.49700012 42.80885145 66.06582355
35 L 2 8 173.801975 114.7929316 40.58543019 95.9692925 133.2165448 214.3874052
36 L 3 8 427.5621 141.5160672 50.03348538 118.3103929 377.5286146 477.5955854
37 N 1 8 53.19965 18.75340803 6.630330993 15.67824146 46.56931901 59.82998099
38 N 2 8 102.83935 49.66950295 17.56082118 41.52474363 85.27852882 120.4001712
39 N 3 8 361.586525 123.0931912 43.52001511 102.9084831 318.0665099 405.1065401
40 M 1 8 90.71195 45.58629605 16.11718953 38.11109723 74.59476047 106.8291395
41 M 2 8 201.219475 90.14563026 31.87129323 75.36363289 169.3481818 233.0907682
42 M 3 8 521.6052875 144.9878341 51.26094033 121.2128627 470.3443472 572.8662278
43 O 1 8 72.572875 37.76520057 13.35201471 31.57249778 59.22086029 85.92488971
44 O 2 8 181.783975 97.67988452 34.53505436 81.66242708 147.2489206 216.3190294
45 O 3 8 415.6914625 179.8261041 63.57812883 150.3383853 352.1133337 479.2695913
46 P 1 8 58.817375 22.82069356 8.068333583 19.07857726 50.74904142 66.88570858
47 P 2 8 131.3006875 67.37113752 23.8192941 56.32368048 107.4813934 155.1199816
48 P 3 8 286.8072625 49.36617664 17.45357913 41.27115649 269.3536834 304.2608416
49 Q 1 8 76.4929625 50.22446933 17.75703142 41.98870713 58.73593108 94.24999392
50 Q 2 8 174.730975 111.6447534 39.47238111 93.33734965 135.2585939 214.2033561
51 Q 3 8 363.4492125 169.6025304 59.96354967 141.7912638 303.4856628 423.4127622
52 R 1 8 72.9998875 24.48056866 8.655188055 20.46626758 64.34469945 81.65507555
53 R 2 8 172.1736375 97.8407617 34.59193304 81.79692377 137.5817045 206.7655705
54 R 3 8 453.68845 177.718465 62.83296587 148.5763549 390.8554841 516.5214159
带有 Facet 的代码:
library(ggplot2)
AllHarvest<-summarySE(CombinedwinrhizoSigmaPlot, measurevar="Length.cm.", groupvars=c("Rootstock","Harvest"))
AllHarvest$Lower <- AllHarvest$Length.cm. - AllHarvest$se
AllHarvest$Upper <- AllHarvest$Length.cm. + AllHarvest$se
AllHplot<-ggplot(AllHarvest,aes(x=Rootstock, y=Length.cm., fill=Rootstock)) + ylim(0,600) + geom_bar(colour = "black", stat="identity", width = 0.7, fill = "#4DAF4A") + geom_errorbar(aes(ymax=Upper, ymin=Lower, width = 0.2), size=0.5, color = "black")
AllHplot + facet_grid(Harvest ~ .)
Harvest #3 的代码,包括柱上方的平均分隔字母:
Harvest3 <- AllHarvest[which(Harvest =="3"),]
Harvest3summary <- summarySE(Harvest3, measurevar="Length.cm.", groupvars=c("Rootstock"))
offset.v = -8 # offsets for mean letters
offset.h = 0.4
Harvest3summary$Lower <- Harvest3summary$Length.cm. - Harvest3summary$se
Harvest3summary$Upper <- Harvest3summary$Length.cm. + Harvest3summary$se
Harvest3plot <- ggplot(Harvest3summary,aes(x=Rootstock, y=Length.cm., fill=Rootstock)) + ylim(0,600) +
geom_bar(colour = "black", stat="identity", width = 0.7, fill = "#4DAF4A") + geom_errorbar(aes(ymax=Upper, ymin=Lower, width = 0.2), size=0.5, color = "black") +
geom_text(aes(label=c("CDE", "CDE", "DE", "DE","CDE","E","BCDE","ABCDE","ABCDE","AB","CDE", "ABC", "ABCDE","A","ABCD","CDE","BCDE","ABC"), size = 8, hjust=offset.h, vjust=offset.v)) +
labs(x = "", y = expression(Total~Root~Length~(cm))) + theme(legend.position = "none") +
## ggtitle("Main title") +
theme(
plot.title = element_text(size = rel(1.5),
face = "bold", vjust = 1.5),
axis.title = element_text(face = "bold"),
axis.title.y = element_text(vjust= 1.8, size=14),
axis.text.y=element_text(size=12),
axis.title.x = element_text(vjust= -0.5),
axis.text.x = element_text(angle=60, hjust=1.1,vjust=1.05, size=14, colour="black"))
panel.border = element_rect(colour="black")
环顾四周后,我通过反复试验能够回答我自己的问题。我正在为那些将来可能想使用它的人添加代码——这对于需要平均分隔符的期刊数字特别有用。请注意 - 使用 geom_text
时,您将分隔字母作为一个长字符串添加到构面中的所有图形中,因此请确保它们的顺序正确:
AllHplot<-ggplot(AllHarvest,aes(x=Rootstock, y=Length.cm., fill=Rootstock)) + coord_cartesian(ylim=c(0,650)) + geom_blank()+ theme_bw() + geom_bar(colour = "black", stat="identity", width = 0.7, fill = "black") + geom_errorbar(aes(ymax=Upper, ymin=Lower, width = 0.2), size=0.5, color = "black")+
labs(x = "", y = expression(paste(Total~Root~Length~(cm)))) + scale_y_continuous(expand = c(0,0)) +
theme(
axis.title = element_text(face = "bold"),
axis.title.y = element_text(face="bold", vjust= 1.8, size=14),
axis.text.y=element_text(size=12, colour="black"),
axis.title.x = element_text(vjust= -0.5),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
axis.text.x = element_text(angle=60, hjust=1.1, vjust=1.05, size=14, colour="black"),
strip.background = element_blank(),
strip.text = element_blank())+ geom_text(aes(label=c("CDE", "CDE", "DE", "DE","CDE","E","BCDE","ABCDE","ABCDE","AB","CDE", "ABC", "ABCDE","A","ABCD","CDE","BCDE","ABC", "ABC", "ABCDEF", "CDEF", "BCDEF","BCDEF","ABC","BCDEF","EF","ABC","CDEF","DEF", "ABCD", "F","A","AB","BCDEF","ABCDE","ABC", "ABC", "ABC", "D", "AB","BCD","AB","ABCD","CD","ABCD","ABCD","BCD", "CD", "BCD","A","ABC","ABCD","ABC","ABC"), hjust=offset.h, vjust=offset.v)) + facet_grid(Harvest ~ .,as.table=F)