0% found this document useful (0 votes)
14 views9 pages

Practical 7 Visulization

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)
14 views9 pages

Practical 7 Visulization

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
You are on page 1/ 9

Experiment No.7.

Apply the basic visualization techniques in R to understand data

A bar chart represents data in rectangular bars with length of the bar proportional to the
value of the variable. R uses the function barplot() to create bar charts. R can draw both
vertical and Horizontal bars in the bar chart. In bar chart each of the bars can be given
different colors.
Syntax
The basic syntax to create a bar-chart in R is −
barplot (H, xlab, ylab, main, names.arg, col)
Following is the description of the parameters used −

 H is a vector or matrix containing numeric values used in bar chart.


 xlab is the label for x axis.
 ylab is the label for y axis.
 main is the title of the bar chart.
 names.arg is a vector of names appearing under each bar.
 col is used to give colors to the bars in the graph.

A simple bar chart is created using just the input vector and the name of each bar.
The below script will create and save the bar chart in the current R working directory.
# Create the data for the chart
H <- c(7,12,28,3,41)

# Give the chart file a name


png(file = "barchart.png")

# Plot the bar chart


barplot(H)

# Save the file


dev.off()
When we execute above code, it produces following result −
# Create the data for the chart
H <- c(7,12,28,3,41)
M <- c("Mar","Apr","May","Jun","Jul")

# Give the chart file a name


png(file = "barchart_months_revenue.png")

# Plot the bar chart


barplot(H,names.arg=M,xlab="Month",ylab="Revenue",col="blue",main="Revenuechart",border="r
ed")

# Save the file


dev.off()

When we execute above code, it produces following result −


Histogram
A histogram represents the frequencies of values of a variable bucketed into ranges.
Histogram is similar to bar chat but the difference is it groups the values into continuous
ranges. Each bar in histogram represents the height of the number of values present in
that range.
R creates histogram using hist() function. This function takes a vector as an input and
uses some more parameters to plot histograms.
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.

Example
A simple histogram is created using input vector, label, col and border parameters.
The script given below will create and save the histogram in the current R working
directory.
# Create data for the graph.
v <- c(9,13,21,8,36,22,12,41,31,33,19)

# Give the chart file a name.


png(file = "histogram.png")

# Create the histogram.


hist(v,xlab = "Weight",col = "yellow”, border = "blue")

# Save the file.


dev.off()
When we execute the above code, it produces the following result −

When we execute the above code, it produces the following result −


Range of X and Y values
To specify the range of values allowed in X axis and Y axis, we can use the xlim and ylim
parameters.
The width of each of the bar can be decided by using breaks.

# Create data for the graph.


v <- c(9,13,21,8,36,22,12,41,31,33,19)

# Give the chart file a name.


png(file = "histogram_lim_breaks.png")

# Create the histogram.


hist(v,xlab = "Weight",col = "green",border = "red", xlim = c(0,40), ylim = c(0,5),
breaks = 5)

# Save the file.


dev.off()
When we execute the above code, it produces the following result −

Pie Chart
# Create data for the graph.

x <- c(21, 62, 10, 53)

labels <- c("London", "New York", "Singapore", "Mumbai")

# Give the chart file a name.

png(file = "city.png")

# Plot the chart.

pie(x,labels)

# Save the file.

dev.off()
When we execute the above code, it produces the following result −

# Create data for the graph.


x <- c(21, 62, 10, 53)
labels <- c("London", "New York", "Singapore", "Mumbai")

# Give the chart file a name.


png(file = "city_title_colours.jpg")

# Plot the chart with title and rainbow color pallet.


pie(x, labels, main = "City pie chart", col = rainbow(length(x)))

# Save the file.


dev.off()
The below script will create and save the pie chart in the current R working directory.

When we execute the above code, it produces the following result −


# Create data for the graph.

x <- c(21, 62, 10,53)

labels <- c("London","New York","Singapore","Mumbai")

piepercent<- round(100*x/sum(x), 1)

# Give the chart file a name.

png(file = "city_percentage_legends.jpg")

# Plot the chart.

pie(x, labels = piepercent, main = "City pie chart",col = rainbow(length(x)))

legend("topright", c("London","New York","Singapore","Mumbai"), cex = 0.8, fill = rainbow(length(x)))

# Save the file.

dev.off()

We can add slice percentage and a chart legend by creating additional chart variables.
When we execute the above code, it produces the following result −

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy