Approved: ASR Pro
Today’s article was created to help you if you get ggplot2 error streaks.
This mini-workshop describes how to create an error bar chart using R software and the ggplot2 package. There are several types of error bars that can be created using the following functions:
- geom_errorbar ()
- geom_linerange ()
- geom_pointrange ()
- geom_crossbar ()
- geom_errorbarh ()
Prepare The Data
Teeth growth data will most likely be used. It describes the effect of vitamin C on tooth growth in guinea pigs. Three doses of vitamin C (0.5, 1 and 2 mg) are used, each with two dosage regimens [orange juice (OJ) plus ascorbic acid (VC)]:
## Len Supp dose## at least one 4.2 VC 0.5## only two 11.5 VC 0.5## 7.3 Vc 0.5## 4 5.8 HP 0.5## 5 6. 6 HP 0.5## 6 10.0 vc 0.5
- len: length of the tooth
- Dose: Dose in milligrams (0.5, 1, 2)
- supp: optional type or (vc JO)
In the product below, we plot the average length of the teeth in each group. The cutter deflection stroke is used to plot your own error bands on the graph.
First of all, the auxiliary work below is used to calculate some standard and average deviations for each variable of interest in each group of people:
# +++++++++++++++++++++++++++# Function to calculate total standard deviation and mean # for both groups# +++++++++++++++++++++++++++++++# data: fre m data# varname: the type of column name that contains your variable # to sum up# groupnames: a vector of column names to use as # Grouping variablesdata_summary
## supp dose len sd## pure OJ 0.5 13.23 4.459709## 2 OJ 1 22.70 3.910953## 3 OJ 2 2 6.06 2.655058## 4 VK 0.5 7.98 2.746634## 5 CV 1 16.77 2.515309## seven VK three 26.14 4.797731
Histogram With Error Bars
The geom_errorbar () function can be used to generate nightclub errors:
library (ggplot2)# Standard histogramp
Please note that you have chosen to use only the highest error bars
# Keep top error bars only ggplot (df2, aes (x = dose, y = len, fill = supp)) + geom_bar (stat = "identity", color = "black", position = position_dodge ()) + geom_errorbar (aes (ymin = len, ymax = len + sd), width = .2, position = position_dodge (.9))
Error Bars Line Diagram
# Standard line schemap
You can also use the geom_pointrange () or geom_linerange () functions instead of most of them with geom_errorbar ()
# Use geom_pointrangeggplot (df2, aes (x = dose, y = len, group = supp, color = supp)) +geom_pointrange (aes (ymin = len-sd, ymax = len + sd))# Use geom_line () + geom_pointrange ()ggplot (df2, aes (x = dose, y = len, group = supp, color = supp)) + geom_line () + geom_pointrange (aes (ymin = len-sd, ymax = len + sd))
Average +/- can be added as a horizontal line, a nice error bar, or a range of points:
This analysis was done with R-Software (version 3.2.4) and ggplot2 (version 2.1.0)
Did you like this article? I would be very happy if you could help spread the information by sending it to a friend or by sharing it on Twitter, Facebook or Linked In.
Show me a little love for the control buttons below … Thanks, and don’t forget to share and comment below!
Do you like this article? you will be very grateful if you want to post it by emailing it to a friend, or perhaps sharing it on Twitter, Facebook, or perhaps Linked In.
Show me and love a little, as well as the underwear as below … Thank you and then please don’t forget to share also to comment on the underwear!
Recommended To You!
This section contains the best resources on data technology, science and self-development to help you in this process.
Speed up your computer's performance now with this simple download.