SlideShare a Scribd company logo
www.r-squared.in/git-hub
dataCrunch Data Visualization With R: Introduction
dataCrunchCourse Material
Slide 2
All the material related to this course are available at our website
Slides can be viewed at SlideShare
Scripts can be downloaded from GitHub
Videos can be viewed on our Youtube Channel
dataCrunch
Slide 3
Plot Basics
dataCrunchplot()
Slide 4
The plot() function is the fundamental tool to build plots in the Graphics package. It is a
generic function and creates the appropriate plot based on the input received from the user. In
this section, we will explore the plot() function by inputting different types of data and
observing the corresponding plots created. We will use the mtcars data set throughout this
section.
The documentation for the plot() function and the mtcars data set can be viewed using the
help function.
1
2
3
4
help(plot)
help(mtcars)
dataCrunchmtcars
Slide 5
Let us take a quick look at the mtcars data set as we will be using it throughout this section:
> head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
> str(mtcars)
'data.frame': 32 obs. of 11 variables:
$ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
$ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
$ disp: num 160 160 108 258 360 ...
$ hp : num 110 110 93 110 175 105 245 62 95 123 ...
$ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
$ wt : num 2.62 2.88 2.32 3.21 3.44 ...
$ qsec: num 16.5 17 18.6 19.4 17 ...
$ vs : num 0 0 1 1 0 1 0 1 1 1 ...
$ am : num 1 1 1 0 0 0 0 0 0 0 ...
$ gear: num 4 4 4 3 3 3 3 4 4 4 ...
$ carb: num 4 4 1 1 2 1 4 2 2 4 ...
dataCrunchExplore plot()
Slide 6
Next, we will begin exploring the plot() function. The following data will be used as an input:
● Case 1: One continuous variable
● Case 2: One categorical variable
● Case 3: Two continuous variables
● Case 4: Two categorical variables
● Case 5: One continuous & one categorical variable
● Case 6: One categorical & one continuous variable
Case 5 and 6 might look similar but the difference lies in the variables being assigned to the X and Y
axis.
dataCrunchCase 1: One continuous variable
Slide 7
We will use the variable mpg (Miles Per Gallon) for this example.
# plot a single continuous variable
plot(mtcars$mpg)
The plot() function creates a
Scatter Plot when a single
continuous variable is used as the
input. We cannot infer anything
from the above plot as it
represents the data points of the
mpg variable in the XY
coordinate. Let us plot a
categorical variable and see what
happens.
dataCrunchCase 2: One categorical variable
Slide 8
Let us use the cyl (number of cylinders) variable for this data as we need a categorical variable.
But before that we need to convert it to type factor using as.factor
# check the data type of cyl
class(mtcars$cyl)
[1] "numeric"
# coerce to type factor
mtcars$cyl <- as.factor(mtcars$cyl)
# plot a single categorical variable
plot(mtcars$cyl)
The plot() function creates a bar
plot when the data is categorical in
nature.
dataCrunchCase 3: Two continuous variables
Slide 9
Till now we had used only one variable as the input but from this example, we will be using two
variables; one for the X axis and another for the Y axis. In this example, we will look at the
relationship between the displacement and mileage of the cars. The disp and mpg variables
are used and disp is plotted on X axis while mpg is plotted on the Y axis.
# plot two continuous variables
plot(mtcars$disp, mtcars$mpg)
A Scatter plot is created when we use
two continuous variables as the input
for the plot function but in this case,
we can interpret the plot as it
represents the relationship between
two variables.
dataCrunchCase 4: Two categorical variables
Slide 10
In this example, we will use two categorical variables am (transmission type) and cyl (number
of cylinders). We will convert am to type factor before creating the plot. Transmission type will
be plotted on X axis and number of cylinders on Y axis.
# coerce am to type factor
mtcars$am <- as.factor(mtcars$am)
# coerce cyl to type factor
mtcars$cyl <- as.factor(mtcars$cyl)
# plot two categorical variables
plot(mtcars$am, mtcars$cyl)
A stacked bar plot is created when
we use two categorical variables as
the input for the plot function. In the
next two examples, we will use both
continuous and categorical variables.
dataCrunchCase 5: Continuous/Categorical Variables
Slide 11
In this example, we will plot a categorical variable cyl on the X axis and a continuous variable
mpg on the Y axis.
# coerce cyl to type factor
mtcars$cyl <- as.factor(mtcars$cyl)
# categorical vs continuous variables
plot(mtcars$cyl, mtcars$mpg)
A box plot is created when we use a
categorical variable and continuous
variable as input for the plot function.
But in this case, the categorical
variable was plotted on the X axis
and the continuous variable on the Y
axis. What happens if we flip this?
dataCrunchCase 6: Categorical/Continuous Variables
Slide 12
In this example, the continuous variable is plotted on the X axis and the categorical variable on
the Y axis.
# coerce cyl to type factor
mtcars$cyl <- as.factor(mtcars$cyl)
# continuous vs categorical variables
plot(mtcars$mpg, mtcars$cyl)
A scatter plot is created but since the
Y axis variable is discrete, we can
observe lines of points for each level
of the discrete variable. We can
compare the range of the X axis
variable for each level of the Y axis
variable.
dataCrunch
Slide 13
Visit dataCrunch for
tutorials on:
→ R Programming
→ Business Analytics
→ Data Visualization
→ Web Applications
→ Package Development
→ Git & GitHub

More Related Content

PDF
Data Visualization With R
PDF
Data Visualization With R: Learn To Combine Multiple Graphs
PDF
Data Visualization With R: Learn To Modify Color Of Plots
PDF
R Data Visualization Tutorial: Bar Plots
PDF
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
PDF
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
PDF
R Data Visualization: Learn To Add Text Annotations To Plots
PDF
R Programming: Introduction to Matrices
Data Visualization With R
Data Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Modify Color Of Plots
R Data Visualization Tutorial: Bar Plots
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
R Data Visualization: Learn To Add Text Annotations To Plots
R Programming: Introduction to Matrices

What's hot (16)

PPTX
Matlab Visualizing Data
PPTX
Graph Plots in Matlab
PDF
PDF
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
PPT
R studio
PPTX
Programming Assignment Help
PDF
Doc 20180130-wa0004
PPTX
Computing and Data Analysis for Environmental Applications
PDF
Broom: Converting Statistical Models to Tidy Data Frames
PPTX
Variables in matlab
PPTX
CIV1900 Matlab - Plotting & Coursework
PDF
Matlab plotting
PDF
13 quadratic formtemplate
ODT
Assigment 3
PDF
Assignment3
PPT
Map reduce (from Google)
Matlab Visualizing Data
Graph Plots in Matlab
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
R studio
Programming Assignment Help
Doc 20180130-wa0004
Computing and Data Analysis for Environmental Applications
Broom: Converting Statistical Models to Tidy Data Frames
Variables in matlab
CIV1900 Matlab - Plotting & Coursework
Matlab plotting
13 quadratic formtemplate
Assigment 3
Assignment3
Map reduce (from Google)
Ad

Similar to Data Visualization With R: Introduction (20)

PPTX
Exploratory data analysis using r
PPTX
Lab 2 - Managing Data in R Basic Conecpt.pptx
PDF
Unit---4.pdf how to gst du paper in this day and age
PDF
Dplyr v2 . Exploratory data analysis.pdf
PDF
Dplyr v2 . Exploratory data analysis.pdf
DOCX
Data visualization with R and ggplot2.docx
PPTX
Exploratory Data Analysis
PDF
M4_DAR_part1. module part 4 analystics with r
PPTX
Data Exploration in R.pptx
PPTX
Artificial inteliggence and machine learning ppt
PDF
R training5
PPTX
Data manipulation and visualization in r 20190711 myanmarucsy
PDF
[系列活動] Data exploration with modern R
PDF
Data_Analytics_for_IoT_Solutions.pptx.pdf
PPT
Tools for research plotting
PPTX
Introduction to R
PPT
Tools for research plotting
PPT
PPT
Slides on introduction to R by ArinBasu MD
PPT
17641.ppt
Exploratory data analysis using r
Lab 2 - Managing Data in R Basic Conecpt.pptx
Unit---4.pdf how to gst du paper in this day and age
Dplyr v2 . Exploratory data analysis.pdf
Dplyr v2 . Exploratory data analysis.pdf
Data visualization with R and ggplot2.docx
Exploratory Data Analysis
M4_DAR_part1. module part 4 analystics with r
Data Exploration in R.pptx
Artificial inteliggence and machine learning ppt
R training5
Data manipulation and visualization in r 20190711 myanmarucsy
[系列活動] Data exploration with modern R
Data_Analytics_for_IoT_Solutions.pptx.pdf
Tools for research plotting
Introduction to R
Tools for research plotting
Slides on introduction to R by ArinBasu MD
17641.ppt
Ad

More from Rsquared Academy (20)

PDF
Handling Date & Time in R
PDF
Market Basket Analysis in R
PDF
Practical Introduction to Web scraping using R
PDF
Joining Data with dplyr
PDF
Explore Data using dplyr
PDF
Data Wrangling with dplyr
PDF
Writing Readable Code with Pipes
PDF
Introduction to tibbles
PDF
Read data from Excel spreadsheets into R
PDF
Read/Import data from flat/delimited files into R
PDF
Variables & Data Types in R
PDF
How to install & update R packages?
PDF
How to get help in R?
PDF
Introduction to R
PDF
RMySQL Tutorial For Beginners
PDF
R Markdown Tutorial For Beginners
PDF
R Programming: Introduction to Vectors
PPTX
R Programming: Variables & Data Types
PDF
R Programming: Mathematical Functions In R
PDF
R Programming: Learn To Manipulate Strings In R
Handling Date & Time in R
Market Basket Analysis in R
Practical Introduction to Web scraping using R
Joining Data with dplyr
Explore Data using dplyr
Data Wrangling with dplyr
Writing Readable Code with Pipes
Introduction to tibbles
Read data from Excel spreadsheets into R
Read/Import data from flat/delimited files into R
Variables & Data Types in R
How to install & update R packages?
How to get help in R?
Introduction to R
RMySQL Tutorial For Beginners
R Markdown Tutorial For Beginners
R Programming: Introduction to Vectors
R Programming: Variables & Data Types
R Programming: Mathematical Functions In R
R Programming: Learn To Manipulate Strings In R

Recently uploaded (20)

PPTX
Database Infoormation System (DBIS).pptx
PDF
Business Analytics and business intelligence.pdf
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
IB Computer Science - Internal Assessment.pptx
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPT
Quality review (1)_presentation of this 21
PPTX
Computer network topology notes for revision
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PPTX
Introduction to machine learning and Linear Models
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PDF
.pdf is not working space design for the following data for the following dat...
PDF
[EN] Industrial Machine Downtime Prediction
PDF
Clinical guidelines as a resource for EBP(1).pdf
PDF
annual-report-2024-2025 original latest.
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PDF
Introduction to the R Programming Language
Database Infoormation System (DBIS).pptx
Business Analytics and business intelligence.pdf
Introduction-to-Cloud-ComputingFinal.pptx
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
IB Computer Science - Internal Assessment.pptx
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Quality review (1)_presentation of this 21
Computer network topology notes for revision
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
Introduction to machine learning and Linear Models
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
.pdf is not working space design for the following data for the following dat...
[EN] Industrial Machine Downtime Prediction
Clinical guidelines as a resource for EBP(1).pdf
annual-report-2024-2025 original latest.
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Introduction to the R Programming Language

Data Visualization With R: Introduction

  • 2. dataCrunchCourse Material Slide 2 All the material related to this course are available at our website Slides can be viewed at SlideShare Scripts can be downloaded from GitHub Videos can be viewed on our Youtube Channel
  • 4. dataCrunchplot() Slide 4 The plot() function is the fundamental tool to build plots in the Graphics package. It is a generic function and creates the appropriate plot based on the input received from the user. In this section, we will explore the plot() function by inputting different types of data and observing the corresponding plots created. We will use the mtcars data set throughout this section. The documentation for the plot() function and the mtcars data set can be viewed using the help function. 1 2 3 4 help(plot) help(mtcars)
  • 5. dataCrunchmtcars Slide 5 Let us take a quick look at the mtcars data set as we will be using it throughout this section: > head(mtcars) mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 > str(mtcars) 'data.frame': 32 obs. of 11 variables: $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... $ cyl : num 6 6 4 6 8 6 8 4 4 6 ... $ disp: num 160 160 108 258 360 ... $ hp : num 110 110 93 110 175 105 245 62 95 123 ... $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ... $ wt : num 2.62 2.88 2.32 3.21 3.44 ... $ qsec: num 16.5 17 18.6 19.4 17 ... $ vs : num 0 0 1 1 0 1 0 1 1 1 ... $ am : num 1 1 1 0 0 0 0 0 0 0 ... $ gear: num 4 4 4 3 3 3 3 4 4 4 ... $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
  • 6. dataCrunchExplore plot() Slide 6 Next, we will begin exploring the plot() function. The following data will be used as an input: ● Case 1: One continuous variable ● Case 2: One categorical variable ● Case 3: Two continuous variables ● Case 4: Two categorical variables ● Case 5: One continuous & one categorical variable ● Case 6: One categorical & one continuous variable Case 5 and 6 might look similar but the difference lies in the variables being assigned to the X and Y axis.
  • 7. dataCrunchCase 1: One continuous variable Slide 7 We will use the variable mpg (Miles Per Gallon) for this example. # plot a single continuous variable plot(mtcars$mpg) The plot() function creates a Scatter Plot when a single continuous variable is used as the input. We cannot infer anything from the above plot as it represents the data points of the mpg variable in the XY coordinate. Let us plot a categorical variable and see what happens.
  • 8. dataCrunchCase 2: One categorical variable Slide 8 Let us use the cyl (number of cylinders) variable for this data as we need a categorical variable. But before that we need to convert it to type factor using as.factor # check the data type of cyl class(mtcars$cyl) [1] "numeric" # coerce to type factor mtcars$cyl <- as.factor(mtcars$cyl) # plot a single categorical variable plot(mtcars$cyl) The plot() function creates a bar plot when the data is categorical in nature.
  • 9. dataCrunchCase 3: Two continuous variables Slide 9 Till now we had used only one variable as the input but from this example, we will be using two variables; one for the X axis and another for the Y axis. In this example, we will look at the relationship between the displacement and mileage of the cars. The disp and mpg variables are used and disp is plotted on X axis while mpg is plotted on the Y axis. # plot two continuous variables plot(mtcars$disp, mtcars$mpg) A Scatter plot is created when we use two continuous variables as the input for the plot function but in this case, we can interpret the plot as it represents the relationship between two variables.
  • 10. dataCrunchCase 4: Two categorical variables Slide 10 In this example, we will use two categorical variables am (transmission type) and cyl (number of cylinders). We will convert am to type factor before creating the plot. Transmission type will be plotted on X axis and number of cylinders on Y axis. # coerce am to type factor mtcars$am <- as.factor(mtcars$am) # coerce cyl to type factor mtcars$cyl <- as.factor(mtcars$cyl) # plot two categorical variables plot(mtcars$am, mtcars$cyl) A stacked bar plot is created when we use two categorical variables as the input for the plot function. In the next two examples, we will use both continuous and categorical variables.
  • 11. dataCrunchCase 5: Continuous/Categorical Variables Slide 11 In this example, we will plot a categorical variable cyl on the X axis and a continuous variable mpg on the Y axis. # coerce cyl to type factor mtcars$cyl <- as.factor(mtcars$cyl) # categorical vs continuous variables plot(mtcars$cyl, mtcars$mpg) A box plot is created when we use a categorical variable and continuous variable as input for the plot function. But in this case, the categorical variable was plotted on the X axis and the continuous variable on the Y axis. What happens if we flip this?
  • 12. dataCrunchCase 6: Categorical/Continuous Variables Slide 12 In this example, the continuous variable is plotted on the X axis and the categorical variable on the Y axis. # coerce cyl to type factor mtcars$cyl <- as.factor(mtcars$cyl) # continuous vs categorical variables plot(mtcars$mpg, mtcars$cyl) A scatter plot is created but since the Y axis variable is discrete, we can observe lines of points for each level of the discrete variable. We can compare the range of the X axis variable for each level of the Y axis variable.
  • 13. dataCrunch Slide 13 Visit dataCrunch for tutorials on: → R Programming → Business Analytics → Data Visualization → Web Applications → Package Development → Git & GitHub