R Strip Charts

 R Strip Chart

A strip chart is a type of chart that displays numerical data along a single strip.A strip chart can be used to visualize dozens of time series at once.

Dataset to Create Strip Chart

In R, first we need to load the dataset of which we want to create the strip chart of.

In this tutorial, we will be using the built-in dataset named airquality to create a strip chart.

Let's see the first six rows of the dataset we will be using,
# use head() to load first six rows of airquality dataset 
head(airquality)
 Ozone Solar.R Wind Temp Month Day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3
4    18     313 11.5   62     5   4
5    NA      NA 14.3   56     5   5
6    28      NA 14.9   66     5   6

Create Strip Chart in R

In R, we use the stripchart() function to create a strip chart. 
For example,
# strip chart for ozone reading of airquality dataset 
stripchart(airquality$Ozone)

In the above example, we have used the stripchart() function and the $ operator to create a strip chart of the Ozone reading of the airquality dataset.

We can see that the data is mostly cluttered below 50 with one falling outside 150.

We can pass in additional parameters to control the way our plot looks. You can read about them in the help section ?stripchart.

Add Title, Label, New Color to a Strip Chart in R

We can add titles, provide labels for the axes, and change the color of the strip chart in R. For example,
# add title, label, new color to strip chart 
stripchart(airquality$Ozone, main="Mean ozone in parts per billion at Roosevelt Island", xlab="Parts Per Billion", ylab="Ozone", col="orange")


In the above figure, we can see that we have added a title, and a label to the x-axis and y-axis, and changed the color of the strip.

Here,
main - adds the title "Mean ozone in parts per billion at Roosevelt Island"
xlab - adds the label "Parts Per Billion" for x-axis
ylab - add the label "Ozone" for y-axis
col = "Orange" - changes the color of strip to orange

Additionally, with the argument vertical=TRUE we can plot it vertically and with pch we can specify the plotting character (square by default). Some values of pch are 0 for square, 1 for circle, 2 for triangle etc. You can see the full list in the help section ?points.

Jitter Plots
stripchart(airquality$Ozone,
main="Mean ozone in parts per billion at Roosevelt Island",
xlab="Parts Per Billion",
ylab="Ozone",
method="jitter",
col="orange",
pch=1
)


Multiple Strip Charts

We can draw multiple strip charts in a single plot, by passing in a list of numeric vectors.

Let us consider the Temp field of airquality dataset. Let us also generate normal distribution with the same mean and standard deviation and plot them side by side for comparison.

# prepare the data
temp <- airquality$Temp
# gererate normal distribution with same mean and sd
tempNorm <- rnorm(200,mean=mean(temp, na.rm=TRUE), sd = sd(temp, na.rm=TRUE))
# make a list
x <- list("temp"=temp, "norm"=tempNorm)
stripchart(x,
main="Multiple stripchart for comparision",
xlab="Degree Fahrenheit",
ylab="Temperature",
method="jitter",
col=c("orange","red"),
pch=16
)


Strip Chart from Formula

The function stripchart() can also take in formulas of the form y~x where, y is a numeric vector which is grouped according to the value of x.

For example, in our dataset airquality, the Temp can be our numeric vector. Month can be our grouping variable, so that we get the strip chart for each month separately.

In our dataset, month is in the form of number (1=January, 2-Febuary and so on).

stripchart(Temp~Month,
data=airquality,
main="Different strip chart for each month",
xlab="Months",
ylab="Temperature",
col="brown3",
group.names=c("May","June","July","August","September"),
vertical=TRUE,
pch=16
)



Comments

Popular posts from this blog

Programming in R - Dr Binu V P

Introduction

R Data Types