Practical 7 Visulization
Practical 7 Visulization
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 −
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)
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)
Pie Chart
# Create data for the graph.
png(file = "city.png")
pie(x,labels)
dev.off()
When we execute the above code, it produces the following result −
piepercent<- round(100*x/sum(x), 1)
png(file = "city_percentage_legends.jpg")
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 −