In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "ggplot2 how to add error lines to the grouping bar chart". The content is detailed, the steps are clear, and the details are handled properly. I hope this "ggplot2 how to add error lines to the grouping bar chart" article can help you solve your doubts.
How to add error lines to the grouped bar chart and adjust the position reasonably? The detailed adjustment is mainly based on geom_errorbar and its parameter position.
1. Case data:
> dat type Sample Num SD1 A sample1 90 1.02 B sample1 34 2.03 C sample1 56 1.24 D sample1 99 2.05 E sample1 15 2.46 A sample2 50 1.07 B sample2 20 1.08 C sample2 24 1.09 D sample2 70 1.810 E sample2 14 1.5
2. Add geom_errorbar to the bar chart (do not control the position)
The settings for geom_errorbar are as follows: width is the width of the upper and lower end of the error line, and size is the thickness.
Geom_errorbar (aes (ymin= (Num-SD), ymax= (Num+SD)), width=0.2,size=0.02)
All the drawing codes are as follows:
P = ggplot (dat, aes (x = type,y = Num,fill = Sample) + geom_bar (stat = "identity", width= 0.6 position = "dodge") + geom_errorbar (aes (ymin= (Num-SD), ymax= (Num+SD)), width=0.2,size=0.02) + scale_fill_manual (values = c ("red", "blue")) + labs (x = "", y = "") Title = "test") + geom_text (aes (label = dat$Num), position=position_dodge (width = 0. 5), size = 5 guide_legend vjust =-0. 25) + guides (fill = guide_legend (reverse = F) + theme (plot.title = element_text (size = 25 reverse = "bold", vjust = 0. 5, hjust = 0. 5), legend.title = element_blank () Legend.text = element_text (size= 18, face = "bold"), legend.position = 'right', legend.key.size=unit (0.8)) print (p)
3. The control position position= "dodge" is consistent with the position parameters of the bar chart.
The drawing code is as follows:
P = ggplot (dat, aes (x = type,y = Num,fill = Sample) + geom_bar (stat = "identity", width= 0.6 position = "dodge") + geom_errorbar (aes (ymin= (Num-SD), ymax= (Num+SD), width=0.2,size=0.02,position = "dodge") + scale_fill_manual (values = c ("red", "blue")) + labs (x = "", y = "") Title = "test") + geom_text (aes (label = dat$Num), position=position_dodge (width = 0. 5), size = 5 guide_legend vjust =-0. 25) + guides (fill = guide_legend (reverse = F) + theme (plot.title = element_text (size = 25 reverse = "bold", vjust = 0. 5, hjust = 0. 5), legend.title = element_blank () Legend.text = element_text (size= 18, face = "bold"), legend.position = 'right', legend.key.size=unit (0.8)) print (p)
4. Control position=position_dodge (width=0.6). The overall interval of the error line is the same as the overall width of the column in the group width.
The drawing code is as follows:
P = ggplot (dat, aes (x = type,y = Num,fill = Sample) + geom_bar (stat = "identity", width= 0.6 position = "dodge") + geom_errorbar (aes (ymin= (Num-SD), ymax= (Num+SD), width=0.2,size=0.02,position = position_dodge (width= 0.6)) + scale_fill_manual (values = c ("red", "blue")) + labs (x = "", y = "") Title = "test") + geom_text (aes (label = dat$Num), position=position_dodge (width = 0. 5), size = 5 guide_legend vjust =-0. 25) + guides (fill = guide_legend (reverse = F) + theme (plot.title = element_text (size = 25 reverse = "bold", vjust = 0. 5, hjust = 0. 5), legend.title = element_blank () Legend.text = element_text (size= 18, face = "bold"), legend.position = 'right', legend.key.size=unit (0.8)) print (p) This article "how to add error lines to the grouped bar chart by ggplot2" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.