Adding error bars

In most scientific data visualizations, error bars are required to show the level of confidence in the data. However, there is no predefined function in the base R library to draw error bars. In this recipe, we will learn how to draw error bars in scatter plots.

Getting ready

All you need for the next recipe is to type it at the R prompt as we will use some base library functions to define a new error bar function. You can also save the recipe's code as a script so that you can use it again later on.

How to do it...

Let's draw vertical error bars with 5 percent errors on our cars' scatter plot using the arrows() function:

plot(mpg~disp,data=mtcars)

arrows(x0=mtcars$disp,
y0=mtcars$mpg*0.95,
x1=mtcars$disp,
y1=mtcars$mpg*1.05,
angle=90,
code=3,
length=0.04,
lwd=0.4)
How to do it...

To add horizontal error bars (also 5 percent in both directions) to the same graph, run the following code after creating the preceding graph:

arrows(x0=mtcars$disp*0.95,
y0=mtcars$mpg,
x1=mtcars$disp*1.05,
y1=mtcars$mpg,
angle=90,
code=3,
length=0.04,
lwd=0.4)
How to do it...

How it works...

In the previous two examples, we used the arrows() function to draw horizontal and vertical error bars. The arrows() function is a base graphics function that draws different kinds of arrows. It provides various arguments to adjust the size, location, and shape of the arrows such that they can be used as error bars.

The first four arguments define the location of the start and end points of the arrows. The first two arguments, which are x0 and y0, are coordinates of the starting points and the next two arguments, which are x1 and y1, are coordinates of the endpoints of the arrows.

To draw vertical error bars, say, with a 5 percent error both ways, we set both x0 and x1 to the x location of the data points (in this case, mtcars$disp), and we set y0 and y1 to the y values of the data points' plus and minus the error margin (1.05*mtcars$mpg and 0.95*mtcars$mpg, respectively).

Similarly, to draw horizontal error bars, we have the same y coordinate for the start and end, but add and subtract the error margin from the x coordinates of the data points.

The angle argument is to set the angle between the shaft of the arrow and the edge of the arrowhead. The default value is 30 (which looks more like an arrow), but to use as an error bar, we set it to 90 (to flatten out the arrowhead in a way).

The code argument sets the type of arrow to be drawn. Setting it to 3 means drawing an arrowhead at both ends.

The length and lwd arguments set the length of the arrowheads and the line width of the arrow, respectively.

There's more...

The Hmisc package has the errbar function, which can be used to draw vertical error bars. The plotrix package has the plotCI function, which can be used to draw error bars or confidence intervals. If we do not wish to write our own error bars function using arrows(), it's easier to use one of these packages.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.46.181