R functions to Visualise Data

Reading the .csv file into R

# reading the data
mtcars <- read.csv("mtcars.csv")

Simple Pie Chart (Demo)

numCars = c(10, 8, 14)
cyls = c("4-cyl", "6-cyl", "8-cyl")
pie(numCars, labels = cyls, main="Pie Chart of 4, 6, 8 cylinder cars")

Simple Pie Chart (Demo)

Create a pie chart showing the proportion of cars from the mtcars data set that have different transmission types.

# 1) Create the Contingency Table for which you want a Pie Chart
tab1 <- table(mtcars$am)

# 2) Calculate percentages %
tab2 = prop.table(tab1)

percent <- round(tab2*100,1)


# 3a) Create labels for each pie in the chart
pielabels <- paste(percent, "%", sep="")

# 3b) Generate the Pie Chart
pie(tab2, 
    col = c("lightblue","red"), 
    labels = pielabels,
    main = '% of cars by Transmission (am)', 
    cex = 1.1)

# 3c) Legend for the pie chart
legend("topright", 
       c("1","0"), 
       cex=0.8, 
       fill=rainbow(length(tab2)))

Pie Chart

Simple Bar Chart (Demo)

# Simple Bar Plot
counts <- table(mtcars$gear)
barplot(counts, main="Car Distribution",
   xlab="Number of Gears")

Simple Bar Chart

Display a barplot showing the percentage of of cars having different cylinders

# 1) Generate the Contingency Table with Proportions
tab1 <- table(mtcars$cyl)
tab2 <- prop.table(tab1)
tab3 <- round(tab2*100, 2)

# 2) Generate the Bar Plot
bp <- barplot(tab3,
              xlab = "cyl", ylab = "Percent (%)",
              main = "% Cars by number of cylinders (cyl=4,6,8)",
              col = c("yellow"), 
              beside = TRUE,
              ylim = c(0, 60))

# 3) (Optional) Display the percentages on the Bar Plot
text(bp, 0, tab3, pos=3)

Barplot

List the column names in the dataframeDisplay a Grouped Barplot showing the % of cars having different Cylinders & Transmissions

Grouped Barplot

# 1) 
tab1 <- table(mtcars$am, mtcars$cyl)
tab2 <- prop.table(tab1)
tab3 <- round(tab2*100, 2)

# 2) Grouped bar-plot
bp <- barplot(tab3,
        xlab = "cyl", ylab = "Percent (%)",
        main = "% Cars by number of cylinders (cyl=4,6,8) and transmission (am=0,1)",
        col = c("lightblue","orange"), 
        beside = TRUE,
        ylim = c(0, 50),
        legend = rownames(tab3))

# 3) (Optional) Display percentage on the bars
text(bp, 0, tab3, pos = 3)

Grouped Barplot

Create a Simple Boxplot of mpg in mtcars Dataset

# Boxplot of MPG by Car Cylinders
boxplot(mtcars$mpg, main="Distribution of Miles Per Gallon",
        ylab="Miles Per Gallon")

Simple Boxplot

Create a Simple Boxplot of mpg in mtcars Dataset by Number of Cylinders

# Boxplot of MPG by Car Cylinders
boxplot(mpg~cyl,data=mtcars, main="Car Milage Data",
   xlab="Number of Cylinders", ylab="Miles Per Gallon")

Grouped Boxplot

Create a Scatter Plot between wt & mpg using mtcars Dataset

# Simple Scatterplot
attach(mtcars)
plot(wt, mpg, main="Scatterplot Example",
   xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)

Scatter Plot

Create a Scatter Plot between wt & mpg with regression line using mtcars Dataset

# Simple Scatterplot
attach(mtcars)
plot(wt, mpg, main="Scatterplot Example",
   xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)
# Add fit lines
abline(lm(mpg~wt), col="red") # regression line (y~x)

Scatter Plot with Regression Line

Basic Scatterplot Matrix

pairs(~mpg+disp+drat+wt,data=mtcars,
   main="Simple Scatterplot Matrix")

Scatterplot Matrix