R programming allows us to create numerous graphs and charts. It uses functions and parameters to define data, ranges, and different items. We will go through basics of creating followings;
- Line plot
- Scatterplot
- Barchart
- Pie chart
The table below gives a summary of the functions and parameters that we can utilize to create those different type of graphs and charts.

Line Plot
This is a kind of graph with multiple data points by connecting them with a line. The plot() function is used to create the line graphs and lines() function is combined with plot() function if there are two lines in the same graph.
We will use vectors to list the numerical values. The vectors simply help us to list the same type of items. We will give a name to a vector and it will be followed up with a c() function which will help to separate the numbers, names,… etc. with commas. We will learn about vectors in more detail in the coming post when we are going through data structure. However, we will be using it starting from creating graphs and charts.
Let’s go through some examples by using plot(), c() and lines() functions with parameters to draw line plots. Open your RStudio and start typing. After you type your functions and parameters, remember to highlight all and press the “Run” button to visualize your graph.
Example1 (only enter y axes data);
# Create the data for the graph with y vector
y <- c(7,15,23,3,45)
# Plot the line graph
plot(y, type = “o”, col = “blue”, lwd=2 , ylab = “Waterfall”, main = “Waterfall chart”)
Example2 (enter both x and y axes data);
# Create the data for the graph with x and y vectors
x <- c(1,2,3,4,5)
y <- c(7,15,23,3,45)
# Plot the line graph
plot(x,y, type = “o”, col = “blue”, lwd=2, xlab = “Month”, ylab = “Waterfall”, main = “Waterfall chart”)
Example2 (add another line plot in the same graph);
# Create the data for the graph
x <- c(1,2,3,4,5)
y1 <- c(7,15,23,3,45)
y2 <- c(3,11,25,6,38)
# Plot the line graph
plot(x,y1, type = “o”, col = “blue”, lwd=2, xlab = “Month”, ylab = “Waterfall”, main = “Waterfall chart”)
lines(x,y2, type = “o”, col = “red”, lwd=2)
Scatterplot
This type of plots represent horizontal (x axis) and vertical (y axis) variables with one point by representing the relationship between them. The function plot() will allow us to graph scatter data.
Example1 (entering x and y axis data);
# 8 days, ice cream sale vs temperature
temperature <- c(52,62,66,72,58,86,78,95)
sales <- c(150,325,405,600,200,750,650,900)
# Plot the line graph
plot(temperature,sales, col = “blue”, lwd=2, xlab = “Temperature,F”, ylab = “Sales,$”,
main = “Temperature vs Ice cream Sales”)
Example 2 (comparing two sets of data in same scatterplot);
# 8 days, ice cream sale vs temperature
temperature1 <- c(52,62,66,72,58,86,78,95)
sales1 <- c(150,325,405,600,200,750,650,900)
#next 5 days, ice cream sale vs temperature
temperature2 <- c(72,74,86,68,100)
sales2 <- c(550,600,730,450,1000)
# Plot the line graph
plot(temperature1,sales1, col = “blue”, xlab = “Temperature,F”, ylab = “Sales,$”,
main = “Temperature vs Ice cream Sales” , pch=5)
points(temperature2,sales2, col = “red”, pch=4)
Bar Chart
Bar charts represent the data with rectangular bars by arranging their proportions to the value of the data. It could be presented either vertically or horizontally. R uses barplot() function to plot.
Example 1 (data in each month);
# x-axis values
x <- c(“Jan”, “Feb”, “Mar”, “Apr”)
# y-axis values
y <- c(10, 15, 12, 30)
barplot(y, names.arg = x, col=”blue”)
Example 2 (data in each month – horizontal bar);
# x-axis values
x <- c(“Jan”, “Feb”, “Mar”, “Apr”)
# y-axis values
y <- c(10, 15, 12, 30)
barplot(y, names.arg = x, col=”blue”, border=”black”, horiz=TRUE)
Pie chart
Pie charts specify the values in slices within a circle. The function pie() will allow us to create pie charts.
Example 1 (only entering data without names);
# Create a vector of pie chart
values <- c(30,30,30,30)
# Display the pie chart
pie(values)
Example 2 (entering data and labels);
# Create a vector of pie chart
values <- c(30,30,30,30)
labels <- c(“Jan”, “Feb”, “Mar”, “Apr”)
# Display the pie chart
pie(values, labels)
Example 3 (adding colors);
# Create a vector of pie chart
values <- c(20,40,60,30)
labels <- c(“Jan”, “Feb”, “Mar”, “Apr”)
colors <- c( “yellow”, “blue”, “red”, “green”)
# Display the pie chart
pie(values, labels, col=colors )
Example 4 ( starting after initial 45 deg, presenting data as percentage by using formula,adding title & legend)
# Create a vector of pie chart with %
values <- c(20,40,60,30)
labels <- c(“Jan”, “Feb”, “Mar”, “Apr”)
colors <- c( “yellow”, “blue”, “red”, “green”)
piepercent<- round(100*values/sum(values),1)
# Display the pie chart
pie(values, labels=piepercent, col=colors, cex=1.2,init.angle = 45, main=”Monthly hour rate” )
#Adding legend
legend(“topright”, c(“Jan”, “Feb”, “Mar”, “Apr”), fill=colors)
Example 5 (presenting chart in 3D and separating slices )
#Need package “plotrix” from library
install.packages(“plotrix”)
library(plotrix)
# Create a vector of pie chart with %
values <- c(20,40,60,30)
labels <- c(“Jan”, “Feb”, “Mar”, “Apr”)
colors <- c( “yellow”, “blue”, “red”, “green”)
piepercent<- round(100*values/sum(values),1)
# Display the pie chart
pie3D(values, labels=piepercent, col=colors, cex=1.2, explode=0.1, main=”Monthly hour rate” )
#Adding legend
legend(“topright”, c(“Jan”, “Feb”, “Mar”, “Apr”), fill=colors)
Please practice more by using functions and parameters that I put together in the table and examples. In the next post, we will go though basics of R statistics.
