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

One thought on “Add more to a histogram

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s