0% found this document useful (0 votes)
10 views3 pages

Program 10

Uploaded by

kbhavana0602
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)
10 views3 pages

Program 10

Uploaded by

kbhavana0602
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

10. Plot various graphs using graphics in R(Histogram, Bar plots).

Histogram

Histogram is a graphical representation used to create a graph with bars representing the
frequency of grouped data in vector. Histogram is same as bar chart but only difference
between them is histogram represents frequency of grouped data rather than data itself.

Syntax: hist(x, col, border, main, xlab, ylab)


where:
 x is data vector
 col specifies the color of the bars to be filled
 border specifies the color of border of bars
 main specifies the title name of histogram
 xlab specifies the x-axis label
 ylab specifies the y-axis label

Example:

x <- c(21, 23, 56, 90, 20, 7, 94, 12,57, 76, 69, 45, 34, 32, 49, 55, 57)

hist(x, main = "Histogram of Vector x",xlab = "Values",col.lab = "darkgreen",

col.main = "red",col=”purple”,border=”yellow”)

The ‘breaks’ argument essentially alters the width of the histogram bars. It is seen that as
we increase the value of the break, the bars grow thinner.

Example:

hist(mtcars$mpg, col = "green")

hist(mtcars$mpg, col = "green", breaks = 25)

hist(mtcars$mpg, col = "green", breaks = 50)

Barplots

A bar chart uses rectangular bars to visualize data. Bar charts can be displayed horizontally or
vertically. The height or length of the bars are proportional to the values they represent.
Example:

x <- c("A", "B", "C", "D")

y <- c(2, 4, 6, 8)

barplot(y, names.arg = x)

 The x variable represents values in the x-axis (A,B,C,D)


 The y variable represents values in the y-axis (2,4,6,8)
 Then we use the barplot() function to create a bar chart of the values
 names.arg defines the names of each observation in the x-axis

Bar Color

Use the col parameter to change the color of the bars:

barplot(y, names.arg = x, col = "red")

Density / Bar Texture

To change the bar texture, use the density parameter:

barplot(y, names.arg = x, density = 200)

Bar Width

Use the width parameter to change the width of the bars:

barplot(y, names.arg = x, width = c(1,2,3,4))

Horizontal Bars

If you want the bars to be displayed horizontally instead of vertically, use horiz=TRUE

barplot(y, names.arg = x, horiz = TRUE)

Bar Plot Example


marks<- c(7, 15, 23, 12)

sub<-c("EM1","EM2","PPS","ED")

# plotting vector

barplot(marks,names.arg=sub, xlab = "subject",

ylab = "marks", col = "pink",

col.axis = "red",

col.lab = "green")

Example:

barplot(table(mtcars$carb), col="pink")

You might also like