Thursday, 6 August 2015

R: Create bar plot

You can use barplot function to create bar plots.  For example, following example creates bar plot for heights of students.

Height
Number Of Students
<130   
5
130-135
2
136-145   
4
>145
8

> heights <- table(c("<130", "<130", "<130", "<130", "<130", "130-135", "130-135", "136-145", "136-145", "136-145", "136-145", ">145", ">145", ">145", ">145", ">145",">145",">145", ">145" ))
> 
> barplot(heights)

Above statements, draws barplot like following.


You can draw same bar plot using ggplot2 also.
> x <- c("<130", "130-135", "136-145", ">145")
> y <- c(5, 2, 4, 8)
> qplot(x, y, geom="bar", stat="identity")

Above snippet draws bar plot like following.






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment