Graphics
Graphics
R Plotting
Multiple Points
You can plot as many points as you like, just make sure you have the same
number of points in both axis:
Example
plot(c(1, 2, 3, 4, 5), c(3, 7, 8, 9, 12))
For better organization, when you have many values, it is better to use
variables:
Example
x <- c(1, 2, 3, 4, 5)
y <- c(3, 7, 8, 9, 12)
plot(x, y)
Sequences of Points
If you want to draw dots in a sequence, on both the x-axis and the y-axis,
use the : operator:
Example
plot(1:10)
Draw a Line
The plot() function also takes a type parameter with the value l to draw a line
to connect all the points in the diagram:
Example
plot(1:10, type="l")
Plot Labels
The plot() function also accept other parameters, such as main, xlab and ylab
if you want to customize the graph with a main title and different labels for
the x and y-axis:
Example
plot(1:10, main="My Graph", xlab="The x-axis", ylab="The y axis")
Graph Appearance
There are many other parameters you can use to change the appearance of
the points.
Colors
Use col="color" to add a color to the points:
Example
plot(1:10, col="red")
Size
Use cex=number to change the size of the points (1 is default, while 0.5
means 50% smaller, and 2 means 100% larger):
Example
plot(1:10, cex=2)
Point Shape
Use pch with a value from 0 to 25 to change the point shape format:
Example
plot(1:10, pch=25, cex=2)
The values of the pch parameter ranges from 0 to 25, which means that we
can choose up to 26 different types of point shapes:
Line Graphs
A line graph has a line that connects all the points in a diagram.
To create a line, use the plot() function and add the type parameter with a
value of "l":
Example
plot(1:10, type="l")
Line Color
The line color is black by default. To change the color, use the col
parameter:
Example
plot(1:10, type="l", col="blue")
Line Width
To change the width of the line, use the lwd parameter (1 is default, while
0.5 means 50% smaller, and 2 means 100% larger):
Example
plot(1:10, type="l", lwd=2)
Line Styles
The line is solid by default. Use the lty parameter with a value from 0 to 6 to
specify the line format.
For example, lty=3 will display a dotted line instead of a solid line:
Example
plot(1:10, type="l", lwd=5, lty=3)
Scatter Plots
You learned from the Plot chapter that the plot() function is used to plot
numbers against each other.
A "scatter plot" is a type of plot used to display the relationship between two
numerical variables, and plots one dot for each observation.
It needs two vectors of same length, one for the x-axis (horizontal) and one
for the y-axis (vertical):
Example
x <- c(5,7,8,7,2,2,9,4,11,12,9,6)
y <- c(99,86,87,88,111,103,87,94,78,77,85,86)
plot(x, y)
The observation in the example above should show the result of 12 cars
passing by.
That might not be clear for someone who sees the graph for the first time, so
let's add a header and different labels to describe the scatter plot better:
Example
x <- c(5,7,8,7,2,2,9,4,11,12,9,6)
y <- c(99,86,87,88,111,103,87,94,78,77,85,86)
plot(x, y, main="Observation of Cars", xlab="Car age", ylab="Car speed")
To recap, the observation in the example above is the result of 12 cars passing by.
It seems that the newer the car, the faster it drives, but that could be a coincidence,
after all we only registered 12 cars.
Compare Plots
In the example above, there seems to be a relationship between the car speed
and age, but what if we plot the observations from another day as well? Will
the scatter plot tell us something else?
To compare the plot with another plot, use the points() function:
Example
Draw two plots on the same figure:
# day one, the age and speed of 12 cars:
x1 <- c(5,7,8,7,2,2,9,4,11,12,9,6)
y1 <- c(99,86,87,88,111,103,87,94,78,77,85,86)
Note: To be able to see the difference of the comparison, you must assign different
colors to the plots (by using the col parameter). Red represents the values of day 1,
while blue represents day 2. Note that we have also added the cex parameter to
increase the size of the dots.
Pie Charts
A pie chart is a circular graphical view of data.
Use the pie() function to draw pie charts:
Example
# Create a vector of pies
x <- c(10,20,30,40)
As you can see the pie chart draws one pie for each value in the vector (in this case
10, 20, 30, 40).
By default, the plotting of the first pie starts from the x-axis and
move counterclockwise.
Note: The size of each pie is determined by comparing the value with all the other
values, by using this formula:
The value divided by the sum of all values: x/sum(x)
Start Angle
You can change the start angle of the pie chart with the init.angle parameter.
The value of init.angle is defined with angle in degrees, where default angle
is 0.
Example
Start the first pie at 90 degrees:
# Create a vector of pies
x <- c(10,20,30,40)
# Display the pie chart and start the first pie at 90 degrees
pie(x, init.angle = 90)
Colors
You can add a color to each pie with the col parameter:
Example
# Create a vector of colors
colors <- c("blue", "yellow", "green", "black")
Legend
To add a list of explanation for each pie, use the legend() function:
Example
# Create a vector of labels
mylabel <- c("Apples", "Bananas", "Cherries", "Dates")
# y-axis values
y <- c(2, 4, 6, 8)
barplot(y, names.arg = x)
Example Explained
Bar Color
Use the col parameter to change the color of the bars:
Example
x <- c("A", "B", "C", "D")
y <- c(2, 4, 6, 8)
Example
x <- c("A", "B", "C", "D")
y <- c(2, 4, 6, 8)
Example
x <- c("A", "B", "C", "D")
y <- c(2, 4, 6, 8)
Example
x <- c("A", "B", "C", "D")
y <- c(2, 4, 6, 8)