Shading and coloring your graph

You can shade and color your graphs using the polygon() command. To use the polygon() command, you must specify the horizontal and vertical axis limits, but you must also include the x and y variables as the middle arguments.

Let's create a quadratic curve and shade under it with a light green selected from the Hexadecimal Color Chart:

x <- 1:100
y <- 3*x^2 + 2*x + 7
plot(x, y)
lines(x, y)

polygon(cbind(c(min(x), x, max(x)), c(min(y), y, min(y))), col="#00CC66") 

Here is the graph:

Shading and coloring your graph

Using this approach, the polygon() command shades under the curve, between the minimum and maximum values of the x variable and below the y variable. The syntax involving cbind() is an elegant way of including the relevant limits.

The following example is more complex. It uses the rnorm() command to simulate values from a normal distribution, with a given mean and standard deviation. By default, random values with a mean of 0 and a standard deviation of 1 are produced. For example, to select a sample of 30 values from a normal distribution with a mean of 12 and standard deviation of 4, use the following syntax:

sample <- rnorm(30, mean=12, sd=4)

OK. Let's proceed with the example, this time choosing a nice light brown color from the Hexadecimal Color Chart. For this example, we will select a random sample of 25 from a normal distribution with a mean of two and standard deviation of three and shade under those points:

x <- 1:25
y <- rnorm(25, mean=2, sd=3)
plot(x, y, pch = 16, cex=0.8)
lines(x, y)

polygon(cbind(c(min(x), x, max(x)),c(min(y), y, min(y))), col="#FF9933") 

We get the following graph, with light brown coloring below the curve:

Shading and coloring your graph

Now we shade above the curve with another attractive color from the Hexadecimal Color Chart:

plot(x, y, pch = 16, cex = 0.8)

lines(x, y)
polygon(cbind(c(min(x), x, max(x)),c(max(y), y, max(y))), col="#CC66FF")

To shade above the curve, we changed the third argument of the group of y values from minimum y to maximum y. Let's see the graph, with coloring above the curve, which is as follows:

Shading and coloring your graph

Now let's see how to shade between the curve and the x axis.

plot(x, y)

lines(x, y)

polygon(cbind(c(min(x), x, max(x)), c(0, y, 0)), col="#339966") 

Here is our graph.

Shading and coloring your graph

To shade between the curve and the x axis, we used zeroes for the first and third vertical axis values. Obviously, you could shade both above and below the curve:

plot(x, y, pch = 16, cex = 0.8)

lines(x, y)

polygon(cbind(c(min(x), x, max(x)),c(max(y), y, max(y))), col="#66CCCC")
polygon(cbind(c(min(x), x, max(x)),c(min(y), y, min(y))), col="#339999") 

We get this graph:

Shading and coloring your graph

Using polygon() to shade under a normal curve

We will use the dnorm() command to create a standard normal curve and we will use polygon() to shade under the normal curve. The syntax is as follows:

dnorm(x, mean = 0, sd = 1)

This creates a set of probabilities from a normal probability distribution with a mean of 0 and a standard deviation of 1. Thus, the following syntax creates a normal distribution graph from -5 to 5:

x <- seq(-5, 5, length=1000)
y <- dnorm(x)

plot(x, y, type="l", lwd=2, col="blue", xlab="Z Value", ylab="Probability", main="Testing Polygon with a Normal Curve")  

We get the following graph:

Using polygon() to shade under a normal curve

We have plotted the curve using 1000 points in order to give a smooth appearance, but we must create an appropriate set of x and y values for polygon(). Let's shade under the entire curve with a pale lemon yellow color from the Hexadecimal Color Chart:

plot(x, y, type="l", lwd=2, col="blue", xlab="Z Value", ylab="Probability", main="Testing Polygon with a Normal Curve")  

Finally, we invoke polygon() as follows:

polygon(c(-5, x, 5), c(0, y, 0), col="#FFFF66")

This command will produce the following graph:

Using polygon() to shade under a normal curve

Let's start again and shade between two Z values.

plot(x, y, type="l", lwd=2, col="blue", xlab="Z Value", ylab="Probability", main="Testing Polygon with a Normal Curve")  

Next, we create the horizontal axis values. Let's suppose that we wish to shade from the point -3 to -1.5 on the Z Value axis, again using a large number of points. We recalculate the probabilities for this set of x values.

x <- seq(-3, -1.5, length=100)
y <- dnorm (x)

Finally, we invoke polygon():

polygon(c(-5, x, -1.5), c(0, y, 0), col="#669966")

The output graph is as follows:

Using polygon() to shade under a normal curve

You can use polygon() to create many interesting graphs, not only for shading between curves and axes. To develop your skill in using the polygon() command, you must read the standard texts and helpful websites and study the worked examples carefully. Additionally, you can enter ?polygon() on the command line and you will be taken to a web page that explains the polygon() command.

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

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