Difference between revisions of "R Histogram & Boxplot of Grades"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <bluebox> This is a simple recipe for generating a histogram and boxplot of student grades. For a much fancier solution, please see the [http://cran.r-project.org...")
 
(R Script)
 
(7 intermediate revisions by the same user not shown)
Line 8: Line 8:
 
<br />
 
<br />
 
The grades are saved in a txt file, 1 grade per line.  Here's a [[List of grades between 0 and 100| typical list]]
 
The grades are saved in a txt file, 1 grade per line.  Here's a [[List of grades between 0 and 100| typical list]]
 +
<br />
 +
=R Script=
 +
<br />
 +
<source lang="rsplus">
 +
 +
# midtermGrades.R
 +
# D. Thiebaut
 +
############################################
 +
# set working directory
 +
setwd( "/Users/thiebaut/Desktop/Dropbox/111/" )
 +
 +
# read the grades, 1 per line, and make grades numeric
 +
g <- read.table( "midtermGrades.txt" )
 +
gg <- sapply( g, as.numeric)
 +
 +
# create a layout with boxplot at bottom
 +
nf <- layout( mat = matrix(c(1,2),2,1, byrow=TRUE),  height = c(3,1))
 +
# set parameters
 +
par( mar=c(3.1, 3.1, 1.1, 2.1) )
 +
 +
# display histogram
 +
hist <- hist( gg, main="Histogram of Midterm Grades", col="lightgreen", breaks=17, xaxt='n')
 +
axis(2)
 +
axis(side=1, at=seq( 0,100,5))
 +
text( 2, 1, "F", cex=0.85 )
 +
text( 22, 1, "F", cex=0.85)
 +
text( 32, 1, "D-", cex=0.85 )
 +
text( 37, 1, "D", cex=0.85 )
 +
text( 42, 1, "D+", cex=0.85 )
 +
text( 47, 1, "C-", cex=0.85 )
 +
text( 52, 1, "C", cex=0.85 )
 +
text( 57, 1, "C", cex=0.85 )
 +
text( 62, 1, "C+", cex=0.85 )
 +
text( 67, 1, "B-", cex=0.85 )
 +
text( 72, 1, "B", cex=0.85 )
 +
text( 77, 1, "B", cex=0.85 )
 +
text( 82, 1, "B+", cex=0.85 )
 +
text( 87, 1, "A-", cex=0.85 )
 +
text( 92, 1, "A", cex=0.85 )
 +
text( 97, 1, "A", cex=0.85 )
 +
 +
# display boxplot
 +
B <- boxplot( gg, horizontal=TRUE, outline=TRUE, frame=FALSE, col="green", notch=TRUE, width=40)
 +
 +
 +
</source>
 +
<br />
 +
 +
=Output=
 +
<br />
 +
<center>[[Image:HistogramBoxPlotGrades.png|640px]]</center>
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:Tutorials]][[Category:R]]

Latest revision as of 08:53, 23 March 2015

--D. Thiebaut (talk) 10:45, 22 March 2015 (EDT)


This is a simple recipe for generating a histogram and boxplot of student grades. For a much fancier solution, please see the R ProfessR package created and maintained by Prof. Jonathan M. Lees at UNC.


Data


The grades are saved in a txt file, 1 grade per line. Here's a typical list

R Script


# midtermGrades.R
# D. Thiebaut
############################################
# set working directory 
setwd( "/Users/thiebaut/Desktop/Dropbox/111/" )

# read the grades, 1 per line, and make grades numeric
g <- read.table( "midtermGrades.txt" )
gg <- sapply( g, as.numeric)

# create a layout with boxplot at bottom
nf <- layout( mat = matrix(c(1,2),2,1, byrow=TRUE),  height = c(3,1))
# set parameters
par( mar=c(3.1, 3.1, 1.1, 2.1) )

# display histogram
hist <- hist( gg, main="Histogram of Midterm Grades", col="lightgreen", breaks=17, xaxt='n')
axis(2)
axis(side=1, at=seq( 0,100,5))
text( 2, 1, "F", cex=0.85 )
text( 22, 1, "F", cex=0.85)
text( 32, 1, "D-", cex=0.85 )
text( 37, 1, "D", cex=0.85 )
text( 42, 1, "D+", cex=0.85 )
text( 47, 1, "C-", cex=0.85 )
text( 52, 1, "C", cex=0.85 )
text( 57, 1, "C", cex=0.85 )
text( 62, 1, "C+", cex=0.85 )
text( 67, 1, "B-", cex=0.85 )
text( 72, 1, "B", cex=0.85 )
text( 77, 1, "B", cex=0.85 )
text( 82, 1, "B+", cex=0.85 )
text( 87, 1, "A-", cex=0.85 )
text( 92, 1, "A", cex=0.85 )
text( 97, 1, "A", cex=0.85 )

# display boxplot
B <- boxplot( gg, horizontal=TRUE, outline=TRUE, frame=FALSE, col="green", notch=TRUE, width=40)


Output


HistogramBoxPlotGrades.png