barplot( frequency-table ,
main = "title" ,
ylab = "y-axis-label",
xlab = "x-axis-label",
col = "" ,
border = " " ,
space = #space-btween-bars ,
ylim = c( start# , stop# ))
gender - f , f, m, f, m, f, m, f, f, m,
gender <- c("f", "f","m", "f", "m", "f", "m", "f", "f","m" )
# frequency table
table(gender)
## gender
## f m
## 6 4
# barplot
barplot( table(gender) ,
main = "Bar plot for Gender",
xlab = "Gender",
ylab = "Frequency",
space = 1.5,
ylim = c(0, 8),
col = "skyblue",
border = "darkblue")
R has built in data-sets.
To display a list of data-sets, we run data() within the
CONSOLE.
To call a specific data set, call its exact name in the list.
To see the background story of the data set, we run
help( data-set-name ) within the CONSOLE.
# show the first 5 rows of the data set
head(ChickWeight, n=5)
## weight Time Chick Diet
## 1 42 0 1 1
## 2 51 2 1 1
## 3 59 4 1 1
## 4 64 6 1 1
## 5 76 8 1 1
# show the last 5 rows of the data set
tail(ChickWeight, n=5)
## weight Time Chick Diet
## 574 175 14 50 4
## 575 205 16 50 4
## 576 234 18 50 4
## 577 264 20 50 4
## 578 264 21 50 4
# check structure
str(ChickWeight)
## Classes 'nfnGroupedData', 'nfGroupedData', 'groupedData' and 'data.frame': 578 obs. of 4 variables:
## $ weight: num 42 51 59 64 76 93 106 125 149 171 ...
## $ Time : num 0 2 4 6 8 10 12 14 16 18 ...
## $ Chick : Ord.factor w/ 50 levels "18"<"16"<"15"<..: 15 15 15 15 15 15 15 15 15 15 ...
## $ Diet : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
## - attr(*, "formula")=Class 'formula' language weight ~ Time | Chick
## .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
## - attr(*, "outer")=Class 'formula' language ~Diet
## .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
## - attr(*, "labels")=List of 2
## ..$ x: chr "Time"
## ..$ y: chr "Body weight"
## - attr(*, "units")=List of 2
## ..$ x: chr "(days)"
## ..$ y: chr "(gm)"
# check dimension: row x column
dim(ChickWeight)
## [1] 578 4
# check for column names
colnames(ChickWeight)
## [1] "weight" "Time" "Chick" "Diet"
# generate bargraph for column Diet in ChickWeight
# access column: data-set-name$column-name
barplot( table(ChickWeight$Diet),
main = "Bar plot of different diet for chicken",
ylab = "Counts",
xlab = "Diet Types",
space = 1.25,
ylim = c(0,250),
col = "#4361ee",
border = "#dc2f02")
hist( variable ,
main = "" ,
xlab = "",
ylab = "" ,
col = " " ,
border = " " ,
xlim = c( start# , stop#) ,
ylim = c( start# , stop#) )
exam - 9, 10, 7, 6, 8, 4, 5, 9, 10, 8, 7, 7, 6, 6, 8, 10, 9, 10, 9, 10, 10
exam <- c(9, 10, 7, 6, 8, 4, 5, 9, 10, 8, 7, 7, 6, 6, 8, 10, 9, 10, 9, 10, 10)
# generate histogram
hist(exam,
main = "Histogram of Exam Scores",
xlab = "Scores",
xlim = c(0, 12),
ylim = c(0,7),
col = "#52b788")
colnames(ChickWeight)
## [1] "weight" "Time" "Chick" "Diet"
hist( ChickWeight$weight,
main = "Histogram of Chicken Weight",
xlab = "Weight (gm)",
ylim = c(0,250),
col = "#9381ff")
boxplot ( variable ,
main = " " ,
ylab = " " ,
col = " " ,
border = " " ,
horizontal = TRUE-or-FALSE)
exam - 9, 10, 7, 6, 8, 4, 5, 9, 10, 8, 7, 7, 6, 6, 8, 10, 9, 10, 9, 10, 10
exam
## [1] 9 10 7 6 8 4 5 9 10 8 7 7 6 6 8 10 9 10 9 10 10
# generate boxplot
boxplot(exam,
horizontal = TRUE,
main = "Boxplot of Exam Score",
xlab = "Scores",
col = "#01baef")
boxplot(ChickWeight$Time,
main = "Boxplot of the number of days since birth",
ylab = "Number of Days",
col = "#3ACF76")