Add more to a histogram

A basic histogram

A histogram is a standard way to present the distribution of a sample of numbers. It is easy to make a histogram using R with the hist() command. For example:

set.seed(123)
x = norm(n = 50, mean = 10, sd = 1)
hist(x, col = "skyblue")

Produces a histogram resembling this:

Rplot hist
Basic Histogram

Add a rug

A rug plot can be added to more or less any graphic. The rug() command can add the rug to any side of the plot:

  • side = 1 is the bottom axis
  • side = 2 is the left axis

You can alter the colour and width of the rug lines using regular graphical parameters:

rug(x, side = 1, col = "blue")

Adds the rug like so:

Rplot hist rug
A Rug plot added to a histogram

Add a strip chart

A strip chart can also be added to any chart via the stripchart() command. However, you also need to specify add = TRUE to the command. Giving a bit of jitter helps to separate out points that are coincident:

stripchart(x,
             method = "jitter",
             pch = 23,
             bg = "pink",
             add = TRUE)

The final plot looks like so:

Rplot hist rug strip
A histogram with added rug and strip plot

There are many additional options for the stripchart() command.


See more tips and tricks at DataAnalytics.org.uk

Advertisements

The Essential R Reference

R is a language with its own vocabulary and grammar. To make R work for you, you communicate with the computer using the language of R and tell it what to do. You accomplish this by typing commands directly into the program. This means that you need to know some of the words of the language and how to put them together to make a “sentence” that R understands. This book aims to help with this task by providing a “dictionary” of words that R understands.

391419_Cover

Community Ecology: Analytical Methods Using R and Excel

Interactions between species are of fundamental importance to all living systems and the framework we have for studying these interactions is community ecology. This is important to our understanding of the planet’s biological diversity and how species interactions relate to the functioning of ecosystems at all scales. Species do not live in isolation and the study of community ecology is of practical application in a wide range of conservation issues.

Comm-Ecol-Cover

Statistics for Ecologists Using R and Excel

This is a book about the scientific process and how you apply it to data in ecology. You will learn how to plan for data collection, how to assemble data, how to analyze data and finally how to present the results. The book uses Microsoft Excel and the powerful Open Source R program to carry out data handling as well as producing graphs.

S4E2e-Cover