0% found this document useful (0 votes)
16 views20 pages

R Data Visualization: Charts & Graphs

Uploaded by

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

R Data Visualization: Charts & Graphs

Uploaded by

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

UNIT- V

Charts and Graphs: Introduction, Pie Chart: Chart Legend, Bar Chart,
Box Plot, Histogram, Line Graph: Multiple Lines in Line Graph,
Scatter Plot.

Regression: Linear Regression Analysis, Multiple Linear regression

Charts and Graphs: Introduction


 R language is mostly used for statistics and data analytics purposes to
represent the data graphically in the software.
 To represent those data graphically, charts and graphs are used in R.
R – graphs
 There are hundreds of charts and graphs present in R

 1. Pie Diagram or Pie Chart

 A pie-chart is a representation of values as slices of a circle with


different colors.
 The slices are labeled and the numbers corresponding to each slice is
also represented in the chart.
 In R the pie chart is created using the pie() function which takes
positive numbers as a vector input.
 The additional parameters are used to control labels, color, title etc.

Syntax

The basic syntax for creating a pie-chart using the R is −

pie(x, labels, radius, main, col, clockwise)

Following is the description of the parameters used −


 x is a vector containing the numeric values used in the pie
chart.
 labels is used to give description to the slices.
 radius indicates the radius of the circle of the pie chart.(value
between −1 and +1).
 main indicates the title of the chart.
 col indicates the color palette.

Note:

 clockwise is a logical value indicating if the slices are drawn clockwise


or anti clockwise.
 dev. off shuts down the specified (by default the current) device. If
the current device is shut down and any other devices are open, the
next open device is made current.
 [Link]() is a function used to close a graphical device that was
opened previously, such as when creating a plot.
 When you generate a plot in R, the graphical output is typically
directed to a device, like the R console, a window, or an image file. If
you've opened a graphical device to save a plot to a file (e.g., PNG,
PDF, JPEG), you need to call [Link]() to close the device and finalize
the output.
 A pie chart is a circular graphical view of data.

E.G

# Create a vector of pies


x <- c(10,20,30,40)

# Display the pie chart


pie(x)
By default, the plotting of the first pie starts from the x-axis and
move counterclockwise.

Note: The size of each pie is determined by comparing the value with all the
other values, by using this formula:

The value divided by the sum of all values: x/sum(x)

Labels and Header

Use the label parameter to add a label to the pie chart, and use the main parameter to add a
header:

Example
# Create a vector of pies
x <- c(10,20,30,40)

# Create a vector of labels


mylabel <- c("Apples", "Bananas", "Cherries", "Dates")
# Display the pie chart with labels
pie(x, label = mylabel, main = "Fruits")

Colors

You can add a color to each pie with the col parameter:

Example
# Create a vector of colors
colors <- c("blue", "yellow", "green", "black")

# Display the pie chart with colors


pie(x, label = mylabel, main = "Fruits", col = colors)
Histogram

 A histogram is a graphical display of data using bars of


different heights.
 Histogram is used to summarize discrete or continuous data
that are measured on an interval scale.

Syntax

The basic syntax for creating a histogram using R is –

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

Following is the description of the parameters used −

 v is a vector containing numeric values used in


histogram.
 main indicates title of the chart.
 col is used to set color of the bars.
 border is used to set border color of each bar.
 xlab is used to give description of x-axis.
 xlim is used to specify the range of values on the x-
axis.
 ylim is used to specify the range of values on the y-
axis.
 breaks is used to mention the width of each bar.

Create Histogram in R

 In R, we use the hist() function to create Histograms. For


example,

 temperatures <- c(67 ,72 ,74 ,62 ,76 ,66 ,65 ,59 ,61 ,69 )
 # histogram of temperatures vector
 result <- hist(temperatures)
 print(result)

 Output

 In the above example, we have used the hist() function to


create a histogram of the temperatures vector.
 The histogram we have created above is plain and simple,
we can add so many things to the Histogram.
Add Title and Label to a Histogram in R

To add a title and a label to our Histogram in R, we pass


the main and the xlab parameter respectively inside
the hist() function. For example,

temperatures <- c(67 ,72 ,74 ,62 ,76 ,66 ,65 ,59 ,61 ,69 )

# histogram of temperatures vector


result <- hist(temperatures,
main = "Histogram of Temperature",
xlab = "Temperature in degrees Fahrenheit"
)

print(result)

Output

Add Title and Label to Histogram

In the above figure, we can see that we have added a title and a
label to the Histogram of the temperatures vector.
hist(temperatures,
main = "Maximum Temperatures in a Week",
xlab = "Temperature in degrees Fahrenheit")

Here,
main - adds the title "Maximum Temperatures in a Week"
xlab - adds the label "Temperature in degrees Fahrenheit"

Change Bar Color of Histogram in R


In R, we pass the col parameter inside hist() to change the color
of bars. For example,
temperatures <-
c(67 ,72 ,74 ,62 ,76 ,66 ,65 ,59 ,61 ,69 )

# histogram of temperatures vector


result <- hist(temperatures,
main = "Histogram of Temperature",
xlab = "Temperature in degrees Fahrenheit",
col = "red")

print(result)

Output
Change Histogram Color
In the above example, we have used the col parameter inside barplot() to
change the color of bars.

result <- hist(temperatures,


...
col = "red"
)

Here, col = "red" changes the color of bars to red.

Range of Axes in R
To provide a range of the axes in R, we pass the xlab and
the ylab parameter inside hist(). For example,

temperatures <- c(67 ,72 ,74 ,62 ,76 ,66 ,65 ,59 ,61 ,69 )

# histogram of temperatures vector


result <- hist(temperatures,
main = "Histogram of Temperature",
xlab = "Temperature in degrees Fahrenheit",
col = "red",
xlim = c(50,100),
ylim = c(0, 5))

print(result)

Output
Histogram With Axes Range
In the above example, we have used the xlim and the ylim parameter
inside hist() to provide a range of x-axis and y-axis respectively.

result <- hist(temperatures,


...
xlim = c(50,100),
ylim = c(0, 5))
)

Here,

 x-axis ranges from 50 to 100


 y-axis ranges from 0 to 5

Note:

It's important to note that parameters like col, main, xlab, and ylab in
the hist() function are optional, meaning they are not required for creating a
histogram.

If not explicitly specified, the default values will be applied. However, using
these optional parameters can provide valuable customization options for
the histogram, allowing you to enhance its appearance and improve data
interpretation.
R Bar Plot
 Bar Plots is one of the most efficient ways of representing datas. It can
be used to summarize large data in visual form.
 Bar graphs have the ability to represent data that shows changes
over time, which helps us to visualize trends.
 In other words, it is the pictorial representation of the dataset.
These data sets contain the numerical values of variables that
represent the length or height.
 A bar chart uses rectangular bars to visualize data. Bar charts can
be displayed horizontally or vertically.
 R uses the function barplot() to create bar charts
Syntax:

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

Parameters:

 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
under each bar in bar chart.
 col: This parameter is used to give colors to the bars in the
graph.

Create Bar Plot in R


 In R, we use the barplot() function to create bar plots. For
example,

temperatures <- c(22, 27, 26, 24, 23, 26, 28)

# bar plot of temperatures vector


result <- barplot(temperatures)

print(result)

Output

Create Bar Plot


 In the above example, we have used the barplot() function to
create a bar plot of the temperatures vector.
 The bar plot we have created above is plain and simple,
Add Title to a Bar Plot in R
 To add a title to our bar plot in R, we pass the main parameter
inside the barplot() function. For example,

temperatures <- c(22, 27, 26, 24, 23, 26, 28)

result <- barplot(temperatures,


main = "Maximum Temperatures in a Week")
print(result)
Output

Add Title to Bar Plot


 In the above figure, we can see that we have added a title to
the bar plot of the temperatures vector.

barplot(temperatures, main = "Maximum


Temperatures in a Week")
 Here, the main parameter adds the title "Maximum
Temperatures in a Week" to our bar plot.

Provide Labels to Axes in R


In R, we can also provide labels for the x-axis and y-axis. For
example,

temperatures <- c(22, 27, 26, 24, 23, 26, 28)


result <- barplot(temperatures,
main = "Maximum Temperatures in a Week",
xlab = "Degree Celsius",
ylab = "Day")
print(result)

Output

Add Label to Axes


In the above example, we have provided labels for the x-axis and
y-axis respectively.

barplot(temperatures,
...
xlab = "Degree Celsius",
ylab = "Day")

 Here, we have provided additional xlab and ylab parameters


to barplot()
 xlab - provides the "Degree Celsius" label for the x-axis
 ylab - provides the "Day" label for the y-axis

Provide Names for Each Bar of Bar Plot in R


 We pass the [Link] parameter inside barplot() to provide
names for each bar in R. For example,
temperatures <- c(22, 27, 26, 24, 23, 26, 28)

result <- barplot(temperatures,


main = "Maximum Temperatures in a Week",
xlab = "Degree Celsius",
ylab = "Day",
[Link] = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
"Sat")
)

print(result)

Output

Provide Name to Each Bar


 In the above example, we have used the [Link] parameter
to provide names to each bar of the bar plot. Notice the code,

barplot(temperatures,
...
[Link] = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
)
 Here, we have assigned "Sun" to the first bar, "Mon" to the
second bar and so on.

Change Bar Color in R


In R, we pass the col parameter inside barplot() to change the
color of bars. For example,

temperatures <- c(22, 27, 26, 24, 23, 26, 28)

result <- barplot(temperatures,


main = "Maximum Temperatures in a Week",
xlab = "Degree Celsius",
ylab = "Day",
[Link] = c("Sun", "Mon", "Tue", "Wed",
"Thu", "Fri", "Sat"),
col = "blue"
)

print(result)
Output
Change Color of Bar Plot
In the above example, we have used the col parameter inside barplot() to change
the color of bars.

result <- barplot(temperatures,


...
col = "blue"
)
Here, col = "blue" changes the color of bars to blue.

Bar Texture in R
To change the texture of bars in R, we pass the density parameter
inside barplot(). For example,

temperatures <- c(22, 27, 26, 24, 23, 26, 28)

result <- barplot(temperatures,


main = "Maximum Temperatures in a Week",
xlab = "Degree Celsius",
ylab = "Day",
[Link] = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
"Sat"),
col = "blue",
density = 20
)

print(result)
Output

Change Bar Texture


In the above example, we have used the density parameter inside barplot() to
change the texture of bars.

result <- barplot(temperatures,


...
col = "blue"
density = 20
)
Here, density = 20 provides the texture of density 20 to all the bars which
are of color blue.

Make Bar Plot Horizontal in R


In R, to make our bar chart horizontal, we pass the horiz parameter inside barplot().

For example,

temperatures <- c(22, 27, 26, 24, 23, 26, 28)


result <- barplot(temperatures,
main = "Maximum Temperatures in a Week",
xlab = "Degree Celsius",
ylab = "Day",
[Link] = c("Sun", "Mon", "Tue", "Wed", "Thu",
"Fri", "Sat"),
col = "blue",
density = 20,
horiz = TRUE
)

print(result)

Output

Horizontal Bar Plot


Here, horiz = TRUE passed inside barplot() changes the orientation of our chart to
horizontal.

What are the Differences Between Bar Chart and Histogram


Bar graph Histogram

The bar graph is the A histogram is the graphical


graphical representation representation of quantitative
of categorical data. data.
There is equal space There is no space between the
between each pair of consecutive bars.
consecutive bars.
The height of the bars The area of rectangular bars
shows the frequency, and shows the frequency of the
the width of the bars are data and the width of the bars
same. need not to be same.

You might also like