Plotting Examples – Line Graph in R

R is extremely versatile when it comes to plotting data but it can be troublesome to use R for visualizations – particularly when you are not yet as used to R. In the following I will show you how to set up a line graph with three lines representing different mean frequencies of three groups during three stages of a process. I frequently use line plots and as a colleague struggled to set one up in R I thought that including an example may be of interest for some of you.

Creating a line graph is pretty straight forward and rather easy. However, you have a lot of options that can and sometimes have to specify – you can find a very nice and VERY handy overview of the graphical parameters you can customize here. We are going to set this graph up in three steps: first, we plot one vector, then we plot the other two vectors, and , finally, we will define and plot the axes. Have a look at the code below to see how it can be done.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#####################################################
### R Script "Visualizations with R: Line Graph"
#####################################################
###                   START
#####################################################
# Remove all lists from the current workspace
rm(list=ls(all=T))
# create vectors with some data points
male <- c(1.6829871, 0.7339867, 0.2307310)
female <- c(0.9524596, 0.5481039, 0.4289967)
combined <- c(1.2639248, 0.6427771, 0.3125549)
# round vectors so they look nicer in the plot
male <- round(male, 2)
female <- round(female, 2)
combined <- round(combined, 2)
# setting up line graph
plot(female, # plot the vector called "female"
  type = "o", # use plot type "o" (overplotted points and lines)
  lwd = 2, # use double line width (4 for 4 times the normal line width)
  lty = 1, # use line type 1 (striaght line)
  pch = 19, #  use point character 19 (filled circle)
  ylim = c(0, 3), # the y-axis should be drawn from 0 to 3
  ylab = "Relative Frequency", # the y-axis label should read "Relative Frequency"
  xlab = "Stages",  # the x-axis label should read "Stages"
  axes = F, # don't draw axes yet!
  cex = 1) # all writing should be of normal size (.5 for half size)
# add aline for the data points in vector male (pch = 0: use empty squares as point characters)
lines(male, type = "o", lwd = 2,  lty = 1, pch = 0,  cex = 1)
# add aline for the data points in vector combined
# (lty = 3: use a dotted line instead of a straight line and
# pch = 4: use x marks as point characters)
lines(combined, type = "o", lwd = 2,  lty = 3, pch = 4,  cex = 1)
# add x-axes with specified labels at specified intervals
axis(1, at = 0:4, lab = c("", "Stage 1", "Stage 2", "Stage 3", ""))
# add y-axes with specified labels at specified intervals
axis(2, at = seq(0, 3, .5), las = 1, lab = seq(0, 3, .5))
# create a legend
# define vector with linetypes
linetype <- c(1, 1, 3)
# define vector with point charaters
plotchar <- c(19, 0, 4)
# set up legend
legend("topright", inset = .05, c("female", "male", "both genders combined"),
  horiz = F,  pch = plotchar, lty = linetype)
# create a box around the plot
box()
# create a grid in the plot
grid()
###############################################################

Below is what the code produces.
linegraphexp

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.