R-Sessions 12: Basic Graphics


Introduction

Producing graphics can be a way to get familiar with your data or to strongly present your results. Fortunately, this can be done both easy as well as in a very powerful way in R-Project. R-Project comes with some standard graphical functions and a package for Trellis-graphics. Here, we will see some of the basics of the standard graphics functionality of R-Project.

R-Project creates graphics and presents them in a ‘graphics device’. This can be a window on the screen, but just as easily a file in a specified format (such as .bmp or .pdf). There are two types of functions that create graphics in R-Project. One of those sets up such a graphics device by calculating and drawing the axes, plot title, margins and so on. Then the data is plotted into the device. The other type of graphics function cannot create a graphics device and only adds data to a plot. This paragraph shows only the first type of plotting-functions.

Basic Plotting

The most basic plot-function in R-Project is called ‘plot()’. It is a function that sets up the graphics-device and is able to create some different types of graphic representations of your data.

For instance, let’s say we want to visualize a set of five values: 3,4,5,6 and 5. In the syntax below, these values are first assigned to the variable ‘y’. Then, we call the plot()-function and tell it to plot the data assigned to y.

y <- c(3,4,5,6,5)
plot (y)
plot (y, type=”l”, main=”Example line-plot”, xlab=”Predictor Value”, ylab=”Outcome Value”)

Basic plot 01

Although the syntax of the first plot-command is very simple, R-Project actually performs quite a bit of work for us. For example: a new window opens, the plotting area is set alongside margins, minimal and maximum values for the axes are calculated based on the data and drawn succeedingly, basic labels are added to the aces and finally: the data is represented. Obviously this plot is not ready for publication, but fortunately all the ‘choices’ R-Project made for us are only the defaults, so we can easily specify exactly what we want.

The next plot already looks a bit better. This is because some extra specifications are added to the second plot-command in the syntax above. By specifying “type=”l” we tell the plot-function that we want the data-points to be connected using a line. The main=” ” specification creates a header for the plot, while the xlab=” ” and ylab=” ” specify the labels for the x-axis and y-axis respectively.

Basic plot 02

x <- c(1,3,4,7,9)
plot (x,y, type=”b”, main=”Example plot”, xlab=”Predictor Value”, ylab=”Outcome Value”)

What if the values that we want to represent are related to predictor values (values on the x-axis) that are not evenly spread, such as in the graphics above? In that case, we have to specify the values on the x-axis to the plot()-function. The syntax above shows how this is done. First, we assign some values to the variable we call x. Then, we replicate the plot()-syntax from above and add the x-variable to it, before the y-variable. Additionally the type=”l” from above is changed into type=”b” (b stands for both), which results in plotting both a line as well as points. The plot this results in, is shown below.

Basic plot 03

Other types of plots

Statistics does not exist solely out of line- and points-graphics. The syntax below shows how the represent the data stored in the y-variable can be represented using a barplot, pie-chart, histogram and a boxplot. These types of graphics are only shown, not described exhaustingly. All of these functions have many parameters that can be used to create exactly what you want.

barplot(y, main=”Barplot”, names.arg=c(“a”,”b”,”c”,”d”,”e”))
pie(y, main=”Pie-chart”, labels=c(“a”,”b”,”c”,”d”,”e”))
hist(y, main=”Histogram”)
boxplot(y, main=”Boxplot”)

Basic plot 04

The syntax above results almost exactly in the graph shown above. The only difference is, that normally R-Project would create four separate graphs when the syntax above is provided. For an explanation of how to place more graphs on one graphics device, see elsewhere in this manual.

All the graphics functions shown here have the main=” ” argument specified. The barplot() function has the additional names.arg – argument specified, which here provides five letters (“a” to “e”) as labels for the bars. On the pie-chart this is done as well, but with the label-argument.

- – — — —– ——–

- – — — —– ——–
R-Sessions is a collection of manual chapters for R-Project, which are maintained on Curving Normality. All posts are linked to the chapters from the R-Project manual on this site. The manual is free to use, for it is paid by the advertisements, but please refer to it in your work inspired by it. Feedback and topic requests are highly appreciated.
——– —– — — – -

Latest Comments

  1. Index of the R-Sessions | Curving Normality
    [...] Basic Graphics [...]

Leave a Reply