0% found this document useful (0 votes)
3 views39 pages

UNIT 5 R

This document provides an overview of advanced graphics in R, detailing high-level and low-level plotting commands. It explains various functions for creating plots, customizing their appearance, and adding elements like points, lines, text, and legends. Additionally, it covers graphical parameters that can be set locally or globally to enhance the visual representation of data.

Uploaded by

realpablo818
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views39 pages

UNIT 5 R

This document provides an overview of advanced graphics in R, detailing high-level and low-level plotting commands. It explains various functions for creating plots, customizing their appearance, and adding elements like points, lines, text, and legends. Additionally, it covers graphical parameters that can be set locally or globally to enhance the visual representation of data.

Uploaded by

realpablo818
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT 5

UNIT 5
Advanced Graphics in R
Plots in R:
In R language plots can be created y using different
commands. These commands can be basically classified into
high level and low level commands.
High Level plot commands in R:
Commands which create a new graph or plots are called
as high level plot commands. Here are some high level plot

Plot NC
commands.
Function Description
Scatter/ line plot
NM
Hist histogram
Barplot barplot
Box plot boxplot
Qnorm Normal-quantile
SR

1. plot()
 In R language, basic plots can be created by using the
function plot().
 The basic syntax of the function is as follows:
Plot(x,y)
 Here, x represents the values for x-axis and y represents
values for y-axis.
1. Along with x and y we can pass many graphical
Vasudha GS, SRNMNC, ShimogaPage 1
UNIT 5

parameters These parameters invoke simple visual


enhancements, like coloring the points and adding axis
labels, and can also control technical aspects of the
graphics device. Some of the most commonly used
graphical parameters are listed here:

1. type Tells R how to plot the supplied coordinates (for


example, as stand-alone points or joined by lines or both
dots and lines).

NC
2. main, xlab, ylab Options to include plot title, the horizontal
axis label, and the vertical axis label, respectively.

3. col Color (or colors) to use for plotting points and lines.
NM
4. pch Stands for point character . This selects which
character to use for plotting individual points.

5. cex Stands for character expansion . This controls the size


of plotted point characters.
SR

6. lty Stands for line type . This specifies the type of line to
use to connect the points (for example, solid, dotted, or
dashed).

7. lwd Stands for line width . This controls the thickness of


plotted lines.

8. xlim, ylim This provides limits for the horizontal range and
vertical range (respectively) of the plotting region.

Vasudha GS, SRNMNC, ShimogaPage 2


UNIT 5

Plot Types:
 By default, the plot function will plot individual points.
 To control the plot type, you can specify a single character
valued option for the argument type.
 The default value for type is "p", which can be interpreted
as “points only.”
 type="l" means “lines only”.
 Option "b" is used for both points and lines
 "o" for over plotting the points with lines (this eliminates

NC
the gaps between points and lines visible for type="b").
 The option type="n" results in no points or lines plotted,
creating an empty plot, which can be useful for
NM
complicated plots that must be constructed in steps.
Color:
 While creating plots, we can specify colors . Specifying
colors makes the graph clearer.
 Color can be added to the plots using the parameter col in
SR

the function plot(). This parameter can take any name of


the color.
 Color can also be specified as integer values between 1-8.
 1- Black, 2-red, 3-green, 4-blue, 5-cyan, 6-pink, 7-yellow, 8-
grey.
Example:
x<-(1:5)
y<-(6:10)
Vasudha GS, SRNMNC, ShimogaPage 3
UNIT 5

plot(x,y,main="example",xlab="x",ylab="y",col=2)

NC
plot(x,y, main=”example”,xlab=”x”,ylab=”y”, col=”red”)
NM
SR

Line and Point Appearances


 To alter the appearance of the plotted points you would
use pch, and to alter the lines you would use lty.
 The pch parameter controls the character used to plot
individual data points.
 You can specify a single character to use for each point,
or you can specify a value between 1 and 25 (inclusive).
 The symbols corresponding to each integer are shown in

Vasudha GS, SRNMNC, ShimogaPage 4


UNIT 5

the following graph:

NC
NM
 The lty parameter, which affects the type of line drawn,
SR

can
Take the values 1 through 6.
 The following graph shows different types of lines for
each integer between 1-6.

Vasudha GS, SRNMNC, ShimogaPage 5


UNIT 5

NC
Low level commands:
Low-level plotting functions allow you to add elements, like points, or
lines, to an existing plot. Here are the most common low-level plotting
functions:
NM
Points Adds points to existing graph
Lines Adds line to existing plot
Text Adds text to existing plot
Legend Adds legends to existing graph
SR

1. point
points() function in R Language is used to add a group of points of
specified shapes, size and color to an existing plot.
Syntax:
points(x, y, cex, pch, col)
Here, x, y: Vector of coordinates,
cex: size of points
pch: shape of points
col: color of points
Vasudha GS, SRNMNC, ShimogaPage 6
UNIT 5

Example:
# R program to add points to a plot
# Creating coordinate vectors
x <- c(1.3, 3.5, 1.4, -3.1, 5.7, 2.4, 3.3, 2.5, 2.3, 1.9, 1.8, 2.3)
y <- c(2.5, 5.8, 2.1, -3, 12, 5, 6.2, 4.8, 4.2, 3.5, 3.7, 5.2)
plot(x, y, cex = 1, pch = 3, xlab ="x", ylab ="y", col ="black")
# Creating coordinates to be added
x2 <- c(4.2, 1.2, -2.4, -0.3, -1.3, 2.5)
y2 <- c(2.4, 4.3, 1.4, 2.3, -2, 4.5)

NC
# Calling points() function
points(x2, y2, cex = 2, pch = 6, col ="blue")
NM
Output:
SR

Vasudha GS, SRNMNC, ShimogaPage 7


UNIT 5

Here, point with ‘+’ symbol are added with plot() function and points
with triangle shape are added using points() function.

2. lines
lines() function in R Programming Language is used to add lines of
different types, colors and width to an existing plot.
Syntax:
lines(x, y, col, lwd, lty)
here, x, y: Vector of coordinates,
col: Color of line

NC
lwd: Width of line
lty: Type of line
Example program:
# R program to add lines into plots
NM
# Creating coordinate vectors
x <- c(1.3, 3.5, 1.4, -3.1, 5.7,
2.4, 3.3, 2.5, 2.3, 1.9, 1.8, 2.3)
y <- c(2.5, 5.8, 2.1, -3, 12, 5,
6.2, 4.8, 4.2, 3.5, 3.7, 5.2)
# Plotting the graph
SR

plot(x, y, cex = 1, pch = 3, xlab ="x",


ylab ="y", col ="black")
# Creating coordinate vectors
x2 <- c(4.3, 1.2, -2.5, -0.4)
y2 <- c(3.5, 4.6, 2.5, 3.2)
# Plotting a line
lines(x2, y2, col = "red",
lwd = 2, lty = 1)

Vasudha GS, SRNMNC, ShimogaPage 8


UNIT 5

Output:

The

NC
Abline()
R function abline() can be
add vertical, horizontal or regression lines to a graph.
Syntax of the function is as follows:
used to
NM
abline(a=, b=, h=, v=, ...)
Here, a, b : single values specifying the intercept and the slope of the line
h : the y-value(s) for horizontal line(s)
v : the x-value(s) for vertical line(s)
Example Program:
SR

x=(1:10)
plot(x)
abline(h=4)

Vasudha GS, SRNMNC, ShimogaPage 9


UNIT 5

Output:

NC
3. text()
Text is defined as a way to describe data related to graphical
NM
representation. They work as labels to any pictorial or graphical
representation. In R language text to the plot can be added using text()
and mtext() functions.
Syntax:
text(x, y, labels)
SR

x and y: numeric values specifying the coordinates of the text to plot


labels: the text to be written
Example program:
# R program to add text to plot
# Plotting the graph
plot(1:5, 1:5,
main = "text() Function examples")
# Calling text() function
text(2, 4, "sales growth")

Vasudha GS, SRNMNC, ShimogaPage 10


UNIT 5

Output:

NC
Mtext()
NM
mtext() function in R Programming Language is used to add text to the
margins of the plot.
Synatx:
mtext(text, side)
Here, text: text to be written
SR

side: An integer specifying the side of the plot, such as: bottom, left, top,
and right.
Example Program:
# Create a scatter plot
x<-c(10,20,30,40,50)
y<-c(50,100,150,200,250)
plot(x,y, main = "Scatter Plot", xlab = "Speed", ylab = "Distance")

# Add text to different margins

Vasudha GS, SRNMNC, ShimogaPage 11


UNIT 5

mtext("Left Margin", side = 2, line = 2, col = "blue", cex = 1.2)


mtext("Right Margin", side = 4, line = 0, col = "green", cex = 1.2)
mtext("Top Margin", side = 3, line = 0, col = "red", cex = 1.2)
mtext("Bottom Margin", side = 1, line = 2, col = "purple", cex = 1.2)

Output:

NC
NM
SR

4. Legends
Legends are useful to add more information to the plots and enhance
the user readability. It involves the creation of titles, indexes, placement
of plot boxes in order to create a better understanding of the graphs
plotted. The in-built R function legend() can be used to add legend to

Vasudha GS, SRNMNC, ShimogaPage 12


UNIT 5

plot.
Syntax:
legend(x, y, legend, fill, col, bg, lty, cex, title, [Link], bg)
 Here, x and y: These are co-ordinates to be used to position the
legend
 legend: Text of the legend
 fill: Colors to use for filling the boxes of legend text
 col: Colors of lines
 bg: It defines background color for the legend box.
 title: Legend title (optional)

NC [Link]: An integer specifying the font style of the legend


(optional)
Example program:
# declaring the data to plot
NM
x<-1:10
y=x^1/2
z= x^2
# plotting x and y coordinate
# line
SR

plot(x, y, col="blue")
# adding another line on the
# coordinates involving y and z
lines(z, y ,col="red")
# Adding a legend to the graph
# defining the lines
legend(2, 4, legend=c("Equation 1", "Equation 2"), fill = c("blue","red") )

Vasudha GS, SRNMNC, ShimogaPage 13


UNIT 5

Output:

NC
NM
Graphics parameters:
In R programming language we can customize the appearance of
plots. There are a variety of graphical parameters that can be used to do
so. Some of the most commonly used graphical parameters include:
xlim and ylim: These parameters set the limits of the x and y axes,
SR

respectively. They take a vector of two values: the minimum and


maximum values of the axis.
 xlab and ylab: These parameters set the labels of the x and y axes,
respectively. They take a string value.
 main: This parameter sets the main title of the plot. It takes a
string value.
 col: This parameter sets the color of points or lines in a plot. It
takes a string value specifying a color name or a code in the
format “#RRGGBB”, where RR, GG, and BB are the red, green, and

Vasudha GS, SRNMNC, ShimogaPage 14


UNIT 5

blue components of the color, respectively.


 lwd: This parameter sets the width of lines in a plot. It takes
numeric value.
 sub: This parameter sets the sub-title/label of the plot.
 pch: This parameter sets the plotting character for points in a plot.
It takes an integer value between 0 and 25.
 lty: This parameter can be used to change the line types of the plot
 font: This parameter sets the font style and font size in the plot.
We can make the text bold, italic, bold italic, etc.
 cex: It is a short form for character expansion. This parameter sets

NCthe size of elements in the plot, such as points or text. cex takes a
numeric value with, 1 being the default size.
Local and global parameters:
 Local graphical parameters in R have specific to a particular plot
NM
and set within the plot function .
 For example, the parameters like main, cex, xlab, ylab etc
specified inside plotting function.
 These parameters affect only the appearance of current graph and
do not affect other plots in the session.
SR

 Global Parameters are used to control the overal appearance of


plots and affect all subsequent plots in the R session.
 These parameters are set using functions like par().
 Some of the common example for global parameters are:
1. mar-- this sets global margin for plots, adjusting the space for
titles, labels and axes.
Par(mar=c(5,4,3,2)
2. cex—This can be used as both local and global parameter. When
used with par() function it applies the text size for titles , axis and

Vasudha GS, SRNMNC, ShimogaPage 15


UNIT 5

labels and other text elements in the subsequent plots.


3. bg-- This sets the global background color for all subsequent
plots.
Par(bg=”lightgrey”)
4. family—This globally canges the font family for the text in
subsequent plots.
Par(family=”arial”)
5. usr—Sets the global user co-ordiantes, cnaging the axis limits
for subsequent plots.
Par(usr=c(0,10,0,20)

NC
6. lend—this sets line end style, specifying how the ends of lines
should be drawn. It can take one of these values. “Round”
or “square”
par(lend = "square")
, “butt”
NM
7. col –this can be used as local parameter and global parameter.
When used with par() function , it will set color for all the
subsequent plots.
[Link] and lwd-- these can be used as local parameter and global
parameter. When used with par() function , they will set line type
SR

and line width for all the subsequent plots.


10. tcl—when used with par function, Globally adjusts the length of
tick marks on axes for subsequent plots.
Par(tcl=0.5)
Device drivers:
In R, graphic devices refer to the various output destinations where
graphical plots can be displayed or saved. Different devices
provide different ways of viewing or storing the plots. Here are
some commonly used graphic devices in R:

Vasudha GS, SRNMNC, ShimogaPage 16


UNIT 5

1. Screen Device:
 When we call the function plot(), it considers console
screen as the destination for the plot,
 Plot(x,y) –this statement creates and displays a plot on the
screen on active window.
 We can display multiple plots on the screen at the same time
, without affecting each other, we can create multiple
devices or active windows by using the function [Link]().
 When we create devices using [Link](), each created
windows will be assigned integers in the order of their

NC creation.
 [Link]() creates a new window and sets it as a active window.
Example:
x1<-1:5
NM
y1 <-6:10
x2<-1:5
y2 <-6:10
plot(x1,y1)
[Link]()
SR

plot(x2,y2)
[Link]()
 Here, the first plot will be created on the active window. Then we
are creating a new active device by using [Link](), and the
second plot will be displayed on the newly created active
window/device.
 When we create a device by using [Link](), it should be closed by
using [Link]() after its purpose is served.
 When we have multiple active devices/ windows , we can switch

Vasudha GS, SRNMNC, ShimogaPage 17


UNIT 5

from one window to another using the function [Link]().


 This [Link]() function takes an integer as a parameter that
indicates the device/ window number.
Example:
[Link]()
[Link]()
[Link]()
 Now we have, there devices/ windows identified by numbers 1,2,3.
To switch between these devices we can use [Link]().
 For example, [Link](2)—makes the second window as the active

NC
window. To shift to first window, we can write [Link](1).
Multiple plots In One device.
 It is also possible to display multiple plots on a single device/ or
window.
NM
 This is done by dividing the window area into rows and columns.
 The function par() takes a parameter “mfrow” for number of
rows and columns into which window should be divided.
Example:
# Set up a 2x2 layout (2 rows, 2 columns)
SR

par(mfrow = c(2, 2))


# Plot in the first panel
plot(1:10, main = "Plot 1")
# Plot in the second panel
plot(1:10, main = "Plot 2", col = "red")
# Plot in the third panel
plot(1:10, main = "Plot 3", pch = 3)
# Plot in the fourth panel
plot(1:10, main = "Plot 4", type = "l", lty = 2)

Vasudha GS, SRNMNC, ShimogaPage 18


UNIT 5

# Reset to single-panel layout


par(mfrow = c(1, 1))

Output:

NC
NM
 The layout() function in R provides a flexible way to arrange
multiple plots in a customized layout. It allows you to specify the
SR

number of rows and columns, and you can define a matrix that
describes the placement of individual plots within those rows and
columns.
Example program:
# Define the layout structure (2 rows, 2 columns)
layout_matrix <- matrix(c(1, 2, 3, 4), nrow = 2, byrow = TRUE)

# Set up the layout


layout(layout_matrix)

Vasudha GS, SRNMNC, ShimogaPage 19


UNIT 5

# Plot in the first panel


plot(1:10, main = "Plot 1")
# Plot in the second panel
plot(1:10, main = "Plot 2", col = "red")
# Plot in the third panel
plot(1:10, main = "Plot 3", pch = 3)
# Plot in the fourth panel
plot(1:10, main = "Plot 4", type = "l", lty = 2)
# Reset to single-panel layout
layout(1)

NC
Output:
NM
SR

Vasudha GS, SRNMNC, ShimogaPage 20


UNIT 5

Here, layout matrix specifies a 2x2 layout where each element


represents a panel in the layout. The layout (layout_matrix)
function sets up the layout based on the specified matrix.
Subsequent plot commands create plots in the designated panels
according to the layout. Finally, layout (1) resets the layout to a
single-panel layout.

Plotting Regions and Margins


For any single plot created using base R graphics, there are three regions

NC
that make up the image.
 The plot region is all you’ve dealt with so far. This is where your
actual
plot appears and where you’ll usually be drawing your points, lines,
NM
text, and so on.
 The plot region uses the user coordinate system , which reflects
the value and scale of the horizontal and vertical axes.
 The figure region is the area that contains the space for your axes,
their labels, and any titles. These spaces are also referred to as the
SR

figure margins .
 The outer region, also referred to as the outer margins , is
additional space around the figure region that is not included by
default but can be specified if it’s needed.
Default Spacing:
 We can check the default margin setting of the figure region by
using the function par() with oma and mar.
 Oma display the default values of outer margin region and mar
shows margin settings.
par()$oma
Vasudha GS, SRNMNC, ShimogaPage 21
UNIT 5

[1] 0 0 0 0 // default values of outer margin area


R> par()$mar
[1] 5.1 4.1 4.1 2.1 // default values of margin settings (left,
top, bottom and right)
Customizing Margins:
 In R, you can customize margins in various plotting functions to
adjust the space around the plot area. The par() function is
commonly used to set graphical parameters, including margins
Example Program:
# Sample data

NC
x <- 1:10
y <- x^2
# Create a scatter plot with default margins
plot(x, y, main = "Default Margins")
# Customize margins using par()
NM
par(mar = c(5, 4, 4, 2) + 0.1) # Setting margins: bottom, left, top, right
# Create a scatter plot with customized margins
plot(x, y, main = "Customized Margins")
# Reset margins to default values
par(mar = c(5, 4, 4, 2) + 0.1) # Resetting to default values
In the par() function, the mar argument sets the margins. The order of
SR

values in the vector corresponds to the bottom, left, top, and right
margins, respectively. These Values can be adjusted according to our
requirement.
Alternatively, we can use the oma (outer margin) parameter in the par()
function to set the outer margins, which include the main title and axis
labels.

Clipping:
 Controlling clipping allows you to draw in or add elements to the

Vasudha GS, SRNMNC, ShimogaPage 22


UNIT 5

margin regions with reference to the user coordinates of the plot


itself.
 The xpd parameter in R is often used to control whether plotting is
allowed outside the plotting region.
 By default, xpd is set to TRUE, meaning that plotting is allowed
outside the plotting region.
 Setting xpd to FALSE restricts plotting to the inside of the plotting
region.
Example:
# Sample data

NC
x <- 1:10
y <- x^2

# Create a scatter plot with default settings


NM
plot(x, y, main = "Default Plotting")

# Set xpd to FALSE to restrict plotting to the inside of the plotting region
par(xpd = FALSE)
SR

# Define a clipping region (e.g., x between 3 and 7, y between 5 and 50)


clip(x1 = 3, x2 = 7, y1 = 5, y2 = 50)

# Try to plot outside the clipping region


points(8, 80, col = "red", pch = 16, cex = 2, lwd = 2, main = "Clipping with
xpd = FALSE")
# Reset xpd to TRUE and clipping to the entire plotting region
par(xpd = TRUE)
clip()

Vasudha GS, SRNMNC, ShimogaPage 23


UNIT 5

# Create a scatter plot to show that xpd is back to default


points(8, 80, col = "blue", pch = 16, cex = 2, lwd = 2)
# Reset xpd to TRUE (not necessary in this case as it's already the
default)
par(xpd = TRUE)

In this example, when xpd is set to FALSE, attempting to plot outside the
specified clipping region will not produce any output. Resetting xpd to
TRUE allows plotting outside the plotting region again.

NC
Adding Box around plot:
 The box() function is often used to add a box around the
plot area. You can customize the appearance of the box
using the lwd (line width) and col (color) parameters.
NM
# Create a scatter plot
plot(1:10, 1:10, xlab = "X-axis", ylab = "Y-axis", main = "Box
Example")
# Add a box around the plot area
SR

box(lwd = 2, col = "red")

This example adds a red box with a line width of 2 around the
plot area.
Output:

Vasudha GS, SRNMNC, ShimogaPage 24


UNIT 5

NC
 Along with above specified arguments, box can take
another parameter, bty which indicates the type of box.
 The bty argument is supplied a single character: "o"
NM
(default), "l", "7", "c", "u", "]", or "n". the resulting box
boundaries will follow the appearance of the
corresponding uppercase letter.

Customizing Axes:
SR

 The axis function allows you to control the addition and


appearance of an axis on any of the four sides of the plot
region in greater detail.
 The first argument it takes is side, provided with a single
integer: 1 (bottom), 2 (left), 3 (top), or 4 (right).
 These numbers are consistent with the positions of the
relevant margin-spacing values when you’re setting graphical
parameter vectors like mar.

Vasudha GS, SRNMNC, ShimogaPage 25


UNIT 5

 The axis() function in R is used to add axis labels and tick


marks to a plot. It is a versatile function that allows you to
customize the appearance of the axes in various ways.
 Here is the basic syntax of the axis() function:

axis(side, at = NULL, labels = TRUE, tick = TRUE, line = NA, pos


= NA, outer = FALSE, font = NA, col = NA, lty = "solid", lwd = 1,
...)

Here,NC
NM
 side: Specifies which side of the plot the axis should be
drawn on. Possible values are 1 (bottom), 2 (left), 3 (top),
4 (right).
 at: A numeric vector specifying the positions of tick marks.
 labels: Logical value indicating whether tick labels should
SR

be drawn.
 tick: Logical value indicating whether tick marks should be
drawn.
 line: The line type of the axis line. If NA, no axis line is
drawn.
 pos: The position of the axis line relative to the plotting
region.
 outer: Logical value indicating whether the axis should be

Vasudha GS, SRNMNC, ShimogaPage 26


UNIT 5

drawn outside the plot.


 font: An integer specifying the font style of tick labels.
 col: The color of tick marks and labels.
 lty: The line type of the tick marks.
 lwd: The line width of the tick marks.
Customizing Font:
Customizing fonts in R involves adjusting the font family, font
face, and font size to achieve the desired appearance of text
elements in your plots. You can customize fonts in various text

NC
elements such as plot titles, axis labels, tick labels, legends,
and annotations. Below are examples demonstrating how to
customize fonts in R plots using the par() function and other
plotting functions.
NM
1. Customizing Fonts in the Entire Plot:
You can use the par() function to set graphical parameters for
the entire plot, including font-related parameters.
Example:
SR

# Sample data
x <- 1:10
y <- x^2
# Set graphical parameters for the entire plot
par(family = "serif", [Link] = 2, [Link] = 1.5)
# Create a scatter plot
plot(x, y, main = "Custom Font")

Vasudha GS, SRNMNC, ShimogaPage 27


UNIT 5

here, family = "serif" sets the font family to a serif font.


[Link] = 2 sets the title to bold.
[Link] = 1.5 increases the size of the main title.
2. Customizing Fonts in Specific Text Elements:
You can use parameters within individual plotting functions to
customize fonts for specific text elements.
Example progam:
# Sample data
x <- 1:10

NC
y <- x^2
# Create a scatter plot with customized font
plot(x, y, main = "Custom Font", xlab = "X-axis Label", ylab = "Y-
axis Label", [Link] = "blue", [Link] = 1.2)
NM
Here, xlab and ylab parameters allow you to set the labels
for the x and y-axes.
[Link] = "blue" sets the color of the axis labels to blue.
SR

[Link] = 1.2 increases the size of the axis labels.


3. Using Math Expressions and Greek Symbols:
You can include mathematical expressions and Greek
symbols in text elements using the expression() function.
Example Program:
# Sample data
x <- 1:10
y <- x^2

Vasudha GS, SRNMNC, ShimogaPage 28


UNIT 5

# Create a scatter plot with mathematical expressions and


Greek symbols
plot(x, y, main = expression(paste("y = ", x^2)), [Link] = "red",
[Link] = 1.2)
 Here, expression(paste("y = ", x^2)) allows you to include
the mathematical expression in the main title.
 [Link] = "red" sets the color of the main title to red.
 [Link] = 1.2 increases the size of the main title.

NC
Histograms:
A histogram contains a rectangular area to display the
statistical information which is proportional to the frequency of
NM
a variable and its width in successive numerical intervals. A
graphical representation that manages a group of data points
into different specified ranges. It has a special feature that
shows no gaps between the bars and is similar to a vertical bar
graph.
SR

Syntax:
hist(v, main, xlab, xlim, ylim, breaks, col, border)
Here,
 v: This parameter contains numerical values used in
histogram.
 main: This parameter main is the title of the chart.
 col: This parameter is used to set color of the bars.
 xlab: This parameter is the label for horizontal axis.
Vasudha GS, SRNMNC, ShimogaPage 29
UNIT 5

 border: This parameter is used to set border color of each


bar.
 xlim: This parameter is used for plotting values of x-axis.
 ylim: This parameter is used for plotting values of y-axis.
 breaks: This parameter is used as width of each bar.
Example program:
# Create data for the graph.
v <- c(19, 23, 11, 5, 16, 21, 32,
14, 19, 27, 39)

NC
# Create the histogram.
hist(v, xlab = "[Link] Articles ",
col = "green", border = "black")
NM
Output:
SR

Vasudha GS, SRNMNC, ShimogaPage 30


UNIT 5

Pie chart:
A pie chart is a circular statistical graphic, which is divided into
slices to illustrate numerical proportions. It depicts a special
chart that uses “pie slices”, where each sector shows the
relative sizes of data. A circular chart cuts in the form of radii
into segments describing relative frequencies or magnitude
also known as a circle graph.
R Programming Language uses the function pie() to create pie

NC
charts. It takes positive numbers as a vector input.

Syntax:
NM
pie(x, labels, radius, main, col, clockwise)
Here,
 x: This parameter is a vector that contains the numeric
values which are used in the pie chart.
 labels: This parameter gives the description to the slices
SR

in pie chart.
 radius: This parameter is used to indicate the radius of the
circle of the pie chart.(value between -1 and +1).
 main: This parameter is represents title of the pie chart.
 clockwise: This parameter contains the logical value
which indicates whether the slices are drawn clockwise or
in anti clockwise direction.
 col: This parameter give colors to the pie in the graph.
Vasudha GS, SRNMNC, ShimogaPage 31
UNIT 5

Example program:
# Create data for the graph.
geeks<- c(23, 56, 20, 63)
labels <- c("Mumbai", "Pune", "Chennai", "Bangalore")
# Plot the chart.
pie(geeks, labels)

Output:

NC
NM
SR

Bar Chart:
Bar charts are a popular and effective way to visually represent
categorical data in a structured manner. R stands out as a
powerful programming language for data analysis and
visualization.
R uses the barplot() function to create bar charts. Here, both
vertical and Horizontal bars can be drawn.

Vasudha GS, SRNMNC, ShimogaPage 32


UNIT 5

barplot(H, xlab, ylab, main, [Link], col)

Here,
 H: This parameter is a vector or matrix containing numeric
values which are used in bar chart.
 xlab: This parameter is the label for x axis in bar chart.
 ylab: This parameter is the label for y axis in bar chart.
 main: This parameter is the title of the bar chart.
 [Link]: This parameter is a vector of names appearing

NCunder each bar in bar chart.


col: This parameter is used to give colors to the bars in the
graph.
Example Program:
NM
# Create the data for the chart
A <- c(17, 32, 8, 53, 1)
# Plot the bar chart
barplot(A, xlab = "X-axis", ylab = "Y-axis", main ="Bar-Chart")
SR

Vasudha GS, SRNMNC, ShimogaPage 33


UNIT 5

Output:

NC
NM
Creating a Horizontal Bar Chart in R
To create a horizontal bar chart:
1. Take all parameters which are required to make a simple
bar chart.
SR

2. Now to make it horizontal new parameter is added.


barplot(A, horiz=TRUE )
Example:
# Create the data for the chart
A <- c(17, 32, 8, 53, 1)

# Plot the bar chart


barplot(A, horiz = TRUE, xlab = "X-axis",

Vasudha GS, SRNMNC, ShimogaPage 34


UNIT 5

ylab = "Y-axis", main ="Horizontal Bar Chart"


)
Output:

NC
Scatter plot In R:
NM
A scatter plot is a set of dotted points representing individual
data pieces on the horizontal and vertical axis. In a graph in
which the values of two variables are plotted along the X-axis
and Y-axis, the pattern of the resulting points reveals a
SR

correlation between them.


We can create a scatter plot in R Programming Language using
the plot() function.
Synrax:
plot(x, y, main, xlab, ylab, xlim, ylim, axes)
Here,
 x: This parameter sets the horizontal coordinates.
 y: This parameter sets the vertical coordinates.

Vasudha GS, SRNMNC, ShimogaPage 35


UNIT 5

 xlab: This parameter is the label for horizontal axis.


 ylab: This parameter is the label for vertical axis.
 main: This parameter main is the title of the chart.
 xlim: This parameter is used for plotting values of x.
 ylim: This parameter is used for plotting values of y.
 axes: This parameter indicates whether both axes should
be drawn on the plot.
Example program:
# Create example data

NC
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 1, 7, 3)
# Create scatter plot
plot(x, y, main = "Simple Scatter Plot", xlab = "X-axis", ylab = "Y-axis")
NM
output:
SR

Vasudha GS, SRNMNC, ShimogaPage 36


UNIT 5

Scatterplot Matrices
When we have two or more variables and we want to correlate
between one variable and others so we use a R scatterplot
matrix.
pairs() function is used to createR matrices of scatterplots.

Here,

NC
Syntax:


pairs(formula, data)

formula: This parameter represents the series of variables


used in pairs.
NM
 data: This parameter represents the data set from which
the variables will be taken.
Example program:
# Create example data with three variables
SR

data <- [Link](


x1 = c(1, 2, 3, 4, 5),
x2 = c(2, 4, 1, 7, 3),
x3 = c(5, 3, 2, 8, 6)
)
# Create scatter plot matrix
pairs(data, main = "Scatter Plot Matrix")

Vasudha GS, SRNMNC, ShimogaPage 37


UNIT 5

Output:

NC
NM
3D Scatterplots
SR

Here we will use R scatterplot3D package to create 3D


scatterplots, this package can plot R scatterplots in 3D using
scatterplot3d() methods.
Example:
[Link]("scatterplot3d")
# Install and load the scatterplot3d package
[Link]("scatterplot3d")
library(scatterplot3d)
Vasudha GS, SRNMNC, ShimogaPage 38
UNIT 5

# Create example data with three variables


data <- [Link](
x = c(1, 2, 3, 4, 5),
y = c(2, 4, 1, 7, 3),
z = c(5, 3, 2, 8, 6)
)
# Create 3D scatter plot
scatterplot3d(data$x, data$y, data$z, color = "blue", main = "3D

NC
Scatter Plot", xlab = "X-axis", ylab = "Y-axis", zlab = "Z-axis")
NM
SR

Vasudha GS, SRNMNC, ShimogaPage 39

You might also like