0% found this document useful (0 votes)
86 views

R-Program Lab Manual

1. The document discusses various probability distributions that can be modeled in R including the binomial, Poisson, normal, and uniform distributions. 2. It provides information on functions in R like dbinom(), pbinom(), qbinom(), and rbinom() that can be used to calculate probabilities, generate random values, and plot binomial distributions. 3. Similarly, it describes functions such as dpois(), ppois(), qpois(), and rpois() for the Poisson distribution, and dnorm(), pnorm(), qnorm(), and rnorm() for the normal distribution. 4. These functions allow calculating probability densities, cumulative distribution functions, quantiles, and generating random values from each distribution.

Uploaded by

JAJULAHARIBABU
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

R-Program Lab Manual

1. The document discusses various probability distributions that can be modeled in R including the binomial, Poisson, normal, and uniform distributions. 2. It provides information on functions in R like dbinom(), pbinom(), qbinom(), and rbinom() that can be used to calculate probabilities, generate random values, and plot binomial distributions. 3. Similarly, it describes functions such as dpois(), ppois(), qpois(), and rpois() for the Poisson distribution, and dnorm(), pnorm(), qnorm(), and rnorm() for the normal distribution. 4. These functions allow calculating probability densities, cumulative distribution functions, quantiles, and generating random values from each distribution.

Uploaded by

JAJULAHARIBABU
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 57

Program-1

Binomial Distribution in R
Programming:
The binomial distribution is also known as discrete probability distribution, which
is used to find the probability of success of an event. The event has only two possible
outcomes in a series of experiments. The tossing of the coin is the best example of the
binomial distribution. When a coin is tossed, it gives either a head or a tail. The
probability of finding exactly three heads in repeatedly tossing the coin ten times is
approximate during the binomial distribution.

R allows us to create binomial distribution by providing the following function:

These function can have the following parameters:

S.No Parameter Description

1. x It is a vector of numbers.

2. p It is a vector of probabilities.

3. n It is a vector of observations.

4. size It is the number of trials.

5. prob It is the probability of the success of each trial.

1. dbinom(): Direct Look-Up, Points


The dbinom() function of R calculates the probability density distribution at each
point. In simple words, it calculates the density function of the particular binomial
distribution.

Example

1. # Creating a sample of 100 numbers which are incremented by 1.5.


2. x <- seq(0,100,by = 1)
3. # Creating the binomial distribution.
4. y <- dbinom(x,50,0.5)
5. # Giving a name to the chart file.
6. png(file = "dbinom.png")
7. # Plotting the graph.
8. plot(x,y)
9. # Saving the file.
10. dev.off()

Output:
2. pbinom():Direct Look-Up, Intervals

The dbinom() function of R calculates the cumulative probability(a single value


representing the probability) of an event. In simple words, it calculates the cumulative
distribution function of the particular binomial distribution.

Example

1. # Probability of getting 20 or fewer heads from 48 tosses of a coin.


2. x <- pbinom(20,48,0.5)
3. #Showing output
4. print(x)

Output:

3. qbinom(): Inverse Look-Up

The qbinom() function of R takes the probability value and generates a number whose
cumulative value matches with the probability value. In simple words, it calculates the
inverse cumulative distribution function of the binomial distribution.

Let's find the number of heads that have a probability of 0.45 when a coin is tossed 51
times.

Example

1. # Finding number of heads with the help of qbinom() function


2. x <- qbinom(0.45,48,0.5)
3. #Showing output
4. print(x)

Output:
4.rbinom()

The rbinom() function of R is used to generate required number of random values for
given probability from a given sample.

Let's see an example in which we find nine random values from a sample of 160 with
a probability of 0.5.

Example

1. # Finding random values


2. x <- rbinom(9,160,0.5)
3. #Showing output
4. print(x)

Output:

2. Poisson distribution

The Poisson distribution represents the probability of a provided number of cases


happening in a set period of space or time if these cases happen with an identified
constant mean rate (free of the period since the ultimate event)

Many probability distributions can be easily implemented in R language with the help
of R’s inbuilt functions.

There are four Poisson functions available in R:


1. dpois
2. ppois
3. qpois
4. rpois

Consider a Random Variable X with Poisson distribution given as The mean (u)
of this distribution is given by . The variance of such a distribution is

So if there are ‘n’ which happened out of which the only k were successful when the
probability of success is very less then the probability of success becomes

1. dpois()

This function is used for illustration of Poisson density in an R plot. The function
dpois() calculates the probability of a random variable that is available within a
certain range.

Syntax:

dpois(k, \lambda, log)

where,

K: number of successful events happened in an interval

\lambda: mean per interval

log: If TRUE then the function returns probability in form of log

Example:

dpois(2, 3)

dpois(6, 6)

Output:

[1] 0.2240418
[1] 0.1606231

2. ppois()
This function is used for the illustration of cumulative probability function in an R
plot. The function ppois() calculates the probability of a random variable that will be
equal to or less than a number.
Syntax:
ppois(q, \lambda, lower.tail, log)
where,
K: number of successful events happened in an interval
\lambda: mean per interval
lower.tail: If TRUE then left tail is considered otherwise if the FALSE right tail is
considered
log: If TRUE then the function returns probability in form of log

Example:
ppois(2, 3)

ppois(6, 6)
Output:
[1] 0.4231901
[1] 0.6063028

3. rpois()
The function rpois() is used for generating random numbers from a given Poisson’s
distribution.
Syntax:
rpois(q, \lambda)
where
q: number of random numbers needed
\lambda: mean per interval
Example:
rpois(2, 3)
rpois(6, 6)
Output:
[1] 2 3
[1] 6 7 6 10 9 4

4. qpois()
The function qpois() is used for generating quantile of a given Poisson’s
distribution.
In probability, quantiles are marked points that divide the graph of a probability
distribution into intervals (continuous ) which have equal probabilities.
Syntax:
qpois(q, \lambda, lower.tail, log)
Where,
K: number of successful events happened in an interval
\lambda: mean per interval
lower.tail: If TRUE then left tail is considered otherwise if the FALSE right tail is
considered
log: If TRUE then the function returns probability in form of log

Example:
y <- c(.01, .05, .1, .2)
qpois(y, 2)
qpois(y, 6)

Output:
[1] 0 0 0 1
[1] 1 2 3 4
3. R Normal Distribution

In random collections of data from independent sources, it is commonly seen that the
distribution of data is normal. It means that if we plot a graph with the value of the
variable in the horizontal axis and counting the values in the vertical axis, then we get
a bell shape curve. The curve center represents the mean of the data set. In the graph,
fifty percent of the value is located to the left of the mean. And the other fifty percent
to the right of the graph. This is referred to as the normal distribution.

R allows us to generate normal distribution by providing the following functions:


These function can have the following parameters:

S.No Parameter Description

1. x It is a vector of numbers.

2. p It is a vector of probabilities.

3. n It is a vector of observations.

4. mean It is the mean value of the sample data whose default value is

5. sd It is the standard deviation whose default value is 1.

1. dnorm():Density

The dnorm() function of R calculates the height of the probability distribution at each
point for a given mean and standard deviation. The probability density of the normal
distribution is:

Example

1. # Creating a sequence of numbers between -1 and 20 incrementing by 0.2.


2. x <- seq(-1, 20, by = .2)
3. # Choosing the mean as 2.0 and standard deviation as 0.5.
4. y <- dnorm(x, mean = 2.0, sd = 0.5)
5. # Giving a name to the chart file.
6. png(file = "dnorm.png")
7. #Plotting the graph
8. plot(x,y)
9. # Saving the file.
10. dev.off()

Output:
2 .pnorm():Direct Look-Up

The dnorm() function is also known as "Cumulative Distribution Function". This


function calculates the probability of a normally distributed random numbers, which
is less than the value of a given number. The cumulative distribution is as follows:

f(x)=P(X≤x)

Example
1. # Creating a sequence of numbers between -1 and 20 incrementing by 0.2.
2. x <- seq(-1, 20, by = .1)
3. # Choosing the mean as 2.0 and standard deviation as 0.5.
4. y <- pnorm(x, mean = 2.0, sd = 0.5)
5. # Giving a name to the chart file.
6. png(file = "pnorm.png")
7. #Plotting the graph
8. plot(x,y)
9. # Saving the file.
10. dev.off()

Output:
3.qnorm():Inverse Look-Up

The qnorm() function takes the probability value as an input and calculates a number
whose cumulative value matches with the probability value. The cumulative
distribution function and the inverse cumulative distribution function are related by

p=f(x)
x=f-1 (p)

Example

1. # Creating a sequence of numbers between -1 and 20 incrementing by 0.2.


2. x <- seq(0, 1, by = .01)
3. # Choosing the mean as 2.0 and standard deviation as 0.5.
4. y <- qnorm(x, mean = 2.0, sd = 0.5)
5. # Giving a name to the chart file.
6. png(file = "qnorm.png")
7. #Plotting the graph
8. plot(y,x)
9. # Saving the file.
10. dev.off()

Output:
4. rnorm():Random variates

The rnorm() function is used for generating normally distributed random numbers.
This function generates random numbers by taking the sample size as an input. Let's
see an example in which we draw a histogram for showing the distribution of the
generated numbers.

Example

1. # Creating a sequence of numbers between -1 and 20 incrementing by 0.2.


2. x <- rnorm(1500, mean=80, sd=15 )
3. # Giving a name to the chart file.
4. png(file = "rnorm.png")
5. #Creating histogram
6. hist(x,probability =TRUE,col="red",border="black")
7. # Saving the file.
8. dev.off()

Output:

4.Uniform Distribution
In simple terms, Uniform Distribution is a probability based distribution wherein it
happens to have equal chances of outcome to occur as a result. That is, it has equals
chances for the probability to have the group values.

For example: Consider a scenario wherein we deal with the question that determines
the probability of males or females who would opt for blue color for their car.

In such a scenario, with respect to the Uniform distribution of data, both males and
females would have equal probability outcomes.

So equal chances of expecting either group of the probability values.

Within the Uniform Distribution, R programming provides us with the below


functions to understand the variants in it.

1. dunif() function

2. runif() function

3. qunif() function

4. punif() function

1. dunif() function

R dunif() function enables us calculate the uniform probability density function for
our passed set of values.

In this example, we have created a sequence of values between 0 to 50 using seq()


function. Further, we use dunif() function to get the uniform density probability value.

Before that, have a look at the below syntax!

dunif(data, min, max)

data: The values on which density function is built.

min: Specifies the minimum value to be set for the uniform density function.

max: Depicts the maximum value for the uniform density function.

Example:
#Removed all the existing objects

rm(list = ls())

info = seq(0, 50, by = 1)

dta = dunif(info, min = 10, max = 25)

plot(dta,type="o")

Output:

2. R punif() function

R punif() function helps us estimate the uniform cumulative distribution function for
the set of values.

In the below example, we use the same set of values as above! Further, punif()
function calculates the cumulative frequency distribution for the set of values passed
to it.

Example:

Removed all the existing objects


rm(list = ls())

info = seq(0, 50, by = 1)

dta = punif(info, min = 10, max = 25)

plot(dta,type="o")

Output:

3. R qunif() function

R qunif() function helps us to get the uniform quantile distribution probability values
for the data values.

In this example, we have used qunif() function to get the quantile distribution values
from the data values passed.
Example:

#Removed all the existing objects

rm(list = ls())

info = seq(0, 1, by = 0.04)

dta = qunif(info, min = 10, max = 25)

plot(dta,type="o")

Output:

4. R runif() function

Now, R runif() function helps us get random numbers as usual but with a little twist.
Using runif() function, we get a set of random numbers that are uniformly distributed
i.e. follow an uniform distribution.
Example:

#Removed all the existing objects

rm(list = ls())

info = 20000

dta = runif(info, min = 10, max = 25)

hist(dta,breaks = 40,

main = "runif function",

xlim = c(0, 100))

Output:
5. Exponential Distribution
The exponential distribution in R Language is the probability distribution of the time
between events in a Poisson point process, i.e., a process in which events occur
continuously and independently at a constant average rate. It is a particular case of the
gamma distribution.

In R, there are 4 built-in functions to generate exponential distribution:

dexp()

dexp(x_dexp, rate)

pexp()

pexp(x_pexp, rate )

qexp()

qexp(x_qexp, rate)

rexp()

rexp(N, rate )

Where,

x: represents x-values for exp function .

rate: represents the shapex.

N: Specify sample size

Functions To Generate Exponential Distribution

1. dexp() Function

dexp() function returns the corresponding values of the exponential density for an
input vector of quantiles.

Syntax:

dexp(x_dexp, rate)
Example:

# R program to illustrate

# exponential distribution

# Specify x-values

x_dexp <- seq(1, 10, by = 0.1)

# Apply dexp() function

y_dexp <- dexp(x_dexp, rate = 5)

# Plot dexp values

plot(y_dexp)

Ouput:
2. pexp() Function

pexp() function returns the corresponding values of the exponential cumulative


distribution function for an input vector of quantiles.

Syntax:

pexp(x_pexp, rate )

Example:

# R program to illustrate

# exponential distribution

# Specify x-values

x_pexp <- seq(1, 10, by = 0.2)

# Apply pexp() function

y_pexp <- pexp(x_pexp, rate = 1)


# Plot values

plot(y_pexp)

Output :

3. qexp() Function

qexp() function gives the possibility, we can use the qexp function to return the
corresponding values of the quantile function.
Syntax:

qexp(x_qexp, rate)

Example:

# R program to illustrate

# exponential distribution

# Specify x-values

x_qexp <- seq(0, 1, by = 0.2)

# Apply qexp() function

y_qexp <- qexp(x_qexp, rate = 1)

# Plot values

plot(y_qexp)

Output:
4. rexp() Function

rexp() function is used to simulate a set of random numbers drawn from the
exponential distribution.

Syntax:

rexp(N, rate )

Example:

# R program to illustrate

# exponential distribution

# Set seed for reproducibility

set.seed(500)

# Specify size

N <- 100

# Draw exp distributed values

y_rexp <- rexp(N, rate = 1)


# Plot exp density

hist(y_rexp, breaks = 50, main = "")

Output :

6.T-Test in R

In statistics, the T-test is one of the most common test which is used to determine
whether the mean of the two groups is equal to each other. The assumption for the test
is that both groups are sampled from a normal distribution with equal fluctuation. The
null hypothesis is that the two means are the same, and the alternative is that they are
not identical. It is known that under the null hypothesis, we can compute a t-statistic
that will follow a t-distribution with n1 + n2 - 2 degrees of freedom.

In R, there are various types of T-test like one sample and Welch T-test. R provides
a t.test() function, which provides a variety of T-tests.

There are the following syntaxes of t.test() function for different T-test

Independent 2-group T-test

1. t.test(y~x)

here, y is numeric, and x is a binary factor.

Independent 2-group T-test

1. t.test(y1,y2)

Here, y1 and y2 are numeric.

Paired T-test

1. t.test(y1,y2,paired=TRUE)

Here, y1 & y2 are numeric.

One sample T-test

t.test(y,mu=3)

Here, Ho: mu=3

How to perform T-tests in R


In the T-test, for specifying equal variances and a pooled variance estimate, we set
var.equal=True. We can also use alternative="less" or alternative="greater" for
specifying one-tailed test.

Let's see how one-sample, paired sample, and independent samples T-test is
performed.

One-Sample T-test
One-Sample T-test is a T-test which compares the mean of a vector against a theoretical
mean. There is a following formula which is used to compute the T-test :

T-Test in R
Here,

M is the mean.
? is the theoretical mean.
s is the standard deviation.
n is the number of observations.

In R, we use the following syntax of t.test() function for performing a one-sample T-


test in R.

t.test(x, ?=0)

Here,

1. x is the name of our variable of interest.


2. ? is described by the null hypothesis, which is set equal to the mean.

Example

Let's see an example of One-Sample T-test in which we test whether the volume of a
shipment of wood was less than usual(?0=0).

1. set.seed(0)
2. ship_vol <- c(rnorm(70, mean = 35000, sd = 2000))
3. t.test(ship_vol, mu = 35000)
Output:

2. Paired-Sample T-test

To perform a paired-sample test, we need two vectors data y1 and y2. Then, we will
run the code using the syntax t.test (y1, y2, paired = TRUE).

Example:

1. set.seed(2800)
2. pre.treatment <- c(rnorm(2000, mean = 130, sd = 5))
3. post.treatment <- c(rnorm(2000, mean = 144, sd = 4))
4. t.test(pre_Treatment, post_Treatment, paired = TRUE)

Output:
Independent-Sample T-test

Depending on the structure of our data and the equality of their variance, the
independent-sample T-test can take one of the three forms, which are as follows:

1. Independent-Samples T-test where y1 and y2 are numeric.


2. Independent-Samples T-test where y1 is numeric and y2 is binary.
3. Independent-Samples T-test with equal variances not assumed.

There is the following general form of t.test() function for the independent-sample t-
test:

1. t.test(y1,y2, paired=FALSE)
By default, R assumes that the versions of y1 and y2 are unequal, thus defaulting to
Welch's test. For toggling this, we set the flag var.equal=TRUE.

Example 1: Independent-Sample T-test where y1 and y2 are numeric

1. set.seed(0)
2. Spenders.Cleve <- rnorm(50, mean = 300, sd = 70)
3. Spenders.NY <- rnorm(50, mean = 350, sd = 70)
4. t.test(Spenders.Cleve, Spenders.NY, var.equal = TRUE)

Output:
7. F test in R

Fisher’s F test calculates the ratio between the larger variance and the smaller
variance. We use the F test when we want to check where means of three or more
groups are different or not. F-test is used to assess whether the variances of two
populations (A and B) are equal. The method is simple; it consists of taking the ratio
between the larger variance and the smaller variance. var.test() function in R
Programming performs an F-test between 2 normal populations with the hypothesis
that variances of the 2 populations are equal.

Formula for Fisher’s F-Test

F = Larger Sample Variance / Smaller Sample Variance

Implementation in R

To test the equality of variances between the two sample use var.test(x, y)

To compare two variance use var.test(x, y, alternative = "two.sided")

Syntax:

var.test(x, y, alternative = “two.sided”)

Parameters:

x, y: numeric vectors

alternative: a character string specifying the alternative hypothesis.

Example 1:

Let we have two samples x, y. The R function var.test() can be used to compare two
variances as follow:

# Taking two samples

x <- rnorm(249, mean = 20)

y <- rnorm(79, mean = 30)

# var test in R

var.test(x, y, alternative = "two.sided")

Output:

F test to compare two variances


data: x and y
F = 0.88707, num df = 248, denom df = 78, p-value = 0.4901
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.6071405 1.2521004
sample estimates:
ratio of variances
0.8870677

It returns the following:

the value of the F test statistic.

the degrees of the freedom of the F distribution of the test statistic.

the p-value of the test 0.4901

a confidence interval for the ratio of the population variances.

the ratio of the sample variances 0.8870677

The p-value of F-test is p = 0.4901 which is greater than the alpha level 0.05. In
conclusion, there is no difference between the two sample.

8. Chi-square test

The Chi-Square Test is used to analyze the frequency table (i.e., contingency table),
which is formed by two categorical variables. The chi-square test evaluates whether
there is a significant relationship between the categories of the two variables.

The Chi-Square Test is a statistical method which is used to determine whether two
categorical variables have a significant correlation between them. These variables
should be from the same population and should be categorical like- Yes/No,
Red/Green, Male/Female, etc.

R provides chisq.test() function to perform chi-square test. This function takes data as
an input, which is in the table form, containing the count value of the variables in the
observation.

In R, there is the following syntax of chisq.test() function:


chisq.test(data)

Let's see an example in which we will take the Cars93 data present in the "Mass"
library. This data represents the sales of different models of cars in the year 1993.

Data:

1. library("MASS")
2. print(str(Cars93))

Output:
Example:

1. # Loading the Mass library.


2. library("MASS")
3. # Creating a data frame from the main data set.
4. car_data<- data.frame(Cars93$AirBags, Cars93$Type)
5. # Creating a table with the needed variables.
6. car_data = table(Cars93$AirBags, Cars93$Type)
7. print(car_data)
8. # Performing the Chi-Square test.
9. print(chisq.test(car_data))
Output:

9. Correlation Coefficient
R Language provides two methods to calculate the pearson correlation coefficient. By using the
functions cor() or cor.test() it can be calculated. It can be noted that cor() computes the correlation
coefficient whereas cor.test() computes the test for association or correlation between paired samples. It
returns both the correlation coefficient and the significance level(or p-value) of the correlation.

Syntax: cor(x, y, method = “pearson”)


cor.test(x, y, method = “pearson”)

Parameters:

x, y: numeric vectors with the same length


method: correlation method

# R program to illustrate
# pearson Correlation Testing
# Using cor()

# Taking two numeric


# Vectors with same length
x = c(1, 2, 3, 4, 5, 6, 7)
y = c(1, 3, 6, 2, 7, 4, 5)

# Calculating
# Correlation coefficient
# Using cor() method
result = cor(x, y, method = "pearson")

# Print the result


cat("Pearson correlation coefficient is:", result)

Output:

Pearson correlation coefficient is: 0.5357143

Example 2: Using cor.test() method

# R program to illustrate
# pearson Correlation Testing
# Using cor.test()

# Taking two numeric


# Vectors with same length
x = c(1, 2, 3, 4, 5, 6, 7)
y = c(1, 3, 6, 2, 7, 4, 5)

# Calculating
# Correlation coefficient
# Using cor.test() method
result = cor.test(x, y, method = "pearson")

# Print the result


print(result)

Output:

Pearson's product-moment correlation


data: x and y
t = 1.4186, df = 5, p-value = 0.2152
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.3643187 0.9183058
sample estimates:
cor
0.5357143

10. Regression Analysis in R


Programming
Implementation of Logistic Regression in R programming
Example:
# Generate random IQ values with mean = 30 and sd =2
IQ <- rnorm(40, 30, 2)

# Sorting IQ level in ascending order


IQ <- sort(IQ)

# Generate vector with pass and fail values of 40 students


result <- c(0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 1, 1, 0, 0, 1, 0,
0, 0, 1, 0, 0, 1, 1, 0, 1, 1,
1, 1, 1, 0, 1, 1, 1, 1, 0, 1)

# Data Frame
df <- as.data.frame(cbind(IQ, result))

# Print data frame


print(df)

# output to be present as PNG file


png(file="LogisticRegressionGFG.png")

# Plotting IQ on x-axis and result on y-axis


plot(IQ, result, xlab = "IQ Level",
ylab = "Probability of Passing")

# Create a logistic model


g = glm(result~IQ, family=binomial, df)

# Create a curve based on prediction using the regression model


curve(predict(g, data.frame(IQ=x), type="resp"), add=TRUE)

# This Draws a set of points


# Based on fit to the regression model
points(IQ, fitted(g), pch=30)

# Summary of the regression model


summary(g)

# saving the file


dev.off()

Output:

IQ result
1 25.46872 0
2 26.72004 0
3 27.16163 0
4 27.55291 1
5 27.72577 0
6 28.00731 0
7 28.18095 0
8 28.28053 0
9 28.29086 0
10 28.34474 1
11 28.35581 1
12 28.40969 0
13 28.72583 0
14 28.81105 0
15 28.87337 1
16 29.00383 1
17 29.01762 0
18 29.03629 0
19 29.18109 1
20 29.39251 0
21 29.40852 0
22 29.78844 0
23 29.80456 1
24 29.81815 0
25 29.86478 0
26 29.91535 1
27 30.04204 1
28 30.09565 0
29 30.28495 1
30 30.39359 1
31 30.78886 1
32 30.79307 1
33 30.98601 1
34 31.14602 0
35 31.48225 1
36 31.74983 1
37 31.94705 1
38 31.94772 1
39 33.63058 0
40 35.35096 1

11. Fitting of straight line in R

abline() function in R Language is used to add one or more straight lines to a graph. The
abline() function can be used to add vertical, horizontal or regression lines to plot.

Syntax:
abline(a=NULL, b=NULL, h=NULL, v=NULL, …)

Parameters:
a, b: It specifies the intercept and the slope of the line
h: specifies y-value for horizontal line(s)
v: specifies x-value(s) for vertical line(s)

Returns: a straight line in the plot


Example 1: To add a vertical line to the plot

# add line to square plot


# first example : Add one line
plot(cars)
abline(v = 16, col = "darkgreen")

# second example : add 2 lines


# addline to square plot
# change line colors, sizes and types
plot(cars)
abline(v = c(16, 22), col = c("darkgreen", "blue"),
lty = c(1, 2), lwd = c(1, 3))

# third example
set.seed(1200); mydata<-rnorm(180)
hist(mydata, col="darkgreen")

# lwd=line width, lty =linetype


abline(v = mean(mydata), col = "blue", lwd = 4, lty = 4)

Output:
Here, in above example straight line is added using abline() to different graphical
plots

12. R Histogram

For creating a histogram, R provides hist() function, which takes a vector as an input
and uses more parameters to add more functionality. There is the following syntax of
hist() function:

hist(v,main,xlab,ylab,xlim,ylim,breaks,col,border)

Example
1. # Creating data for the graph.
2. v <- c(12,24,16,38,21,13,55,17,39,10,60)
3.
4. # Giving a name to the chart file.
5. png(file = "histogram_chart.png")
6.
7. # Creating the histogram.
8. hist(v,xlab = "Weight",ylab="Frequency",col = "green",border = "red")
9.
10. # Saving the file.
11. dev.off()

Output:
13.R Bar Charts

In R, we can create a bar chart to visualize the data in an efficient manner. For this
purpose, R provides the barplot() function, which has the following syntax:

barplot(h,x,y,main, names.arg,col)

Example

# Creating the data for Bar chart

H<- c(12,35,54,3,41)

# Giving the chart file a name

png(file = "bar_chart.png")

# Plotting the bar chart

barplot(H)

# Saving the file

dev.off()

Output:
14.R Pie Charts
The Pie charts are created with the help of pie () function, which takes positive
numbers as vector input. Additional parameters are used to control labels, colors,
titles, etc.

There is the following syntax of the pie() function:

pie(X, Labels, Radius, Main, Col, Clockwise)

Here,

97.2K
iPhone 13 Pro & 13 Pro Max: ONE MONTH REVIEW!

1. X is a vector that contains the numeric values used in the pie chart.
2. Labels are used to give the description to the slices.
3. Radius describes the radius of the pie chart.
4. Main describes the title of the chart.
5. Col defines the color palette.
6. Clockwise is a logical value that indicates the clockwise or anti-clockwise
direction in which slices are drawn.

[1] 0 0 0 1

[1] 1 2 3 4

1. # Creating data for the graph.


2. x <- c(20, 65, 15, 50)
3. labels <- c("India", "America", "Shri Lanka", "Nepal")
4. # Giving the chart file a name.
5. png(file = "Country.jpg")
6. # Plotting the chart.
7. pie(x,labels)
8. # Saving the file.
9. dev.off()

Output:
15. Scatter plots in R.

There is the following syntax for creating scatterplot in R:


plot(x, y, main, xlab, ylab, xlim, ylim, axes)

Example

#Fetching two columns from mtcars

data <-mtcars[,c('wt','mpg')]

# Giving a name to the chart file.

png(file = "scatterplot.png")

# Plotting the chart for cars with weight between 2.5 to 5 and mileage between 15 and
30.

plot(x = data$wt,y = data$mpg, xlab = "Weight", ylab = "Milage", xlim = c(2.5,5),


ylim = c(15,30), main = "Weight v/sMilage")

# Saving the file.

dev.off()

Output:
16. Plot box in R.
R provides a boxplot() function to create a boxplot. There is the following syntax of
boxplot() function:

boxplot(x, data, notch, varwidth, names, main)


Example
1. # Giving a name to the chart file.
2. png(file = "boxplot.png")
3. # Plotting the chart.
4. boxplot(mpg ~ cyl, data = mtcars, xlab = "Quantity of Cylinders",
5. ylab = "Miles Per Gallon", main = "R Boxplot Example")
6.
7. # Save the file.
8. dev.off()

Output:

You might also like