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, ...
Get R: Data Analysis and Visualization now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.