Plot Library
1) Useful Concepts
Packages are part of R programming and they are useful in collecting sets of R functions into a
single unit. It also contains compiled code and sample data. All of these are kept stored in a
directory called the "library" in the R environment
1. Packages and library: R packages are a collection of R functions, complied code and sample
data. They are stored under a directory called "library" in the R environment. By default, R
installs a set of packages during installation. More packages are added later, when they are
needed for some specific purpose
2. installed.packages(): is the command to check which packages are installed in R
3. install.packages(“package__name”): is the command to install a package in R. e.g. if we want to
install package “tidytext” we will write install.package(“tidytext”)
4. library(“dataset”): is the command to load data set in R e.g. if we want to load “iris” dataset in
R we will use the simple command of library(“datasets”)
5. head(dataset_name): in R head function returns the first records of the selected dataset e.g.
head(iris) will show the first records of the “iris data set loaded via command
library(“datasets”).
6. summary(dataset_name[$attribute]): in R summary() is a generic function that returns the
summary of the selected dataset or the selected variable e.g. summary(iris) will show the first
records of the “iris data set loaded via command library(“datasets”).
7. summary(dataset_name[$attribute]): in R summary function returns the summary of the
selected dataset or the selected variable e.g. summary(iris) will show the summary of the “iris
data set loaded via command library(“datasets”) and summary(iris$Sepal.Length) will show the
summary of the “iris data set loaded via command library(“datasets”)
8. plot(dataset_name[$attribute]): in R, plot() function created the graph of the selected dataset or
the selected variable e.g. plot(iris) will create graphs of the “iris data set loaded via command
library(“datasets”) and plot(iris$Species) will create graph of the “iris data set loaded via
command library(“datasets”)
9. plot(dataset_name[$attribute]): in R, plot() function created the graph of the selected dataset or
the selected variable e.g. plot(iris) will create graphs of the “iris data set loaded via command
library(“datasets”) and plot(iris$Species) will create graph of the “iris data set loaded via
command library(“datasets”)
Activity 1:
R program to install packages
install.packages("tidytext")
install.packages("ggplot2")
Output
The required packages will be installed
Activity 2:
R program to include libraries
library(tidytext)
library(ggplot2)
Output
The required libraries will be included/imported
Activity 3:
R program to load datasets (built-in) and view header
library(datasets)
#Loading iris datasets from the library and displaying the
header
head(iris)
Output
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
Activity 4:
R program to plot datasets
library(datasets)
#Loading iris datasets from the library and displaying the
header
head(iris)
#usint plot function to plot different graphs
plot(iris$Species) # Categorical variable
plot(iris$Petal.Length) # Quantitative variable
plot(iris$Species, iris$Petal.Width) # Cat x quant
plot(iris$Petal.Length, iris$Petal.Width) # Quant pair
plot(iris) # Entire data frame
Output:
Species:
Figure 5:Species of iris
Length of a petal:
Figure 6 :Length of petal
Species and width of a petal:
Figure 6 :Species and width of petal
Length and width of a petal:
Figure 7 :Length and width of petal
Entire Data Frame
Figure 6 :Entire data frame
Example with multiple options
Use the plot function with few options like changing the color to red, creating solid points, labeling the
main chart as well as the x-axis and the y-axis
Working Code:
plot(iris$Petal.Length, iris$Petal.Width,
col = “#cc0000”, # Hex code for datalab.cc red
pch = 19, # Use solid circles for points
main = “Iris: Petal Length vs. Petal Width”,
xlab = “Petal Length”,
ylab = “Petal Width”)