barplot() FORMULA:
barplot( frequency ,
main= "Title" ,
ylab= "Vertical Line Name" ,
xlab = "Horizontal Line Name" ,
col = "color-name/code ,
border = "border-color",
space = #space-between-bar,
ylim = c( #start , #end ))
Raw data of drinks ordered
drinks <- c("s", "m", "m", "s", "l", "s", "l", "l", "m", "s", "s", "l", "m", "l", "l")
drinks
## [1] "s" "m" "m" "s" "l" "s" "l" "l" "m" "s" "s" "l" "m" "l" "l"
Generating frequency
drinks.frequency <- table(drinks)
drinks.frequency
## drinks
## l m s
## 6 4 5
Generating bar plot using barplot()
barplot(drinks.frequency,
main = "Drinks Counts Based on Size",
ylab = "Frequency",
xlab = "Size",
col = "lavender",
border = "purple",
space = -5,
ylim = c(0, 7))
Horizontal Bar
barplot(drinks.frequency,
main = "Drinks Counts Based on Size",
ylab = "Frequency",
xlab = "Size",
col = "lavender",
border = "purple",
space = -5,
xlim = c(0, 7),
horiz = T)
List of color
#colors()
See a list of R built-in data-set
Recommend use data() in the Console to avoid knitting
issues.
#data()
Load the data set by the EXACT name
We should ALWAYS rename the dataset before using to avoid making permanent change in the original data-set.
chicks <- ChickWeight
str(chicks)
## 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)"
We recognize that Diet and Chick are factor (QuaLitative data) – suitabble for bar graph.
Inspection of these two columns
unique(chicks$Chick)
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
## 50 Levels: 18 < 16 < 15 < 13 < 9 < 20 < 10 < 8 < 17 < 19 < 4 < 6 < 11 < ... < 48
Column Chick seems to be an identification number for
different chickens.
unique(chicks$Diet)
## [1] 1 2 3 4
## Levels: 1 2 3 4
Column Diet has 4 different types, not an identification
number for the chickens. Therefore, it is more suitable to use bar graph
to compare between frequency of chicken eating that type of diet.
Generate Frequency
diet.frq <- table(chicks$Diet)
diet.frq
##
## 1 2 3 4
## 220 120 120 118
Bar graph
barplot(diet.frq,
main = "Chickens Counts Based Diet",
ylab = "Frequency",
xlab = "Diet Type",
col = "#a7c957",
border = "#386641",
space = 1.5,
ylim = c(0, 250))
hist() FORMULA:
hist( column/variable/vector ,
main= "Title" ,
ylab= "Vertical Line Name" ,
xlab = "Horizontal Line Name" ,
col = "color-name/code ,
border = "border-color",
ylim = c( #start , #end ),
xlim = c( #start , #end ),
labels = TRUE/FALSE)
Generate histogram for weight
hist(chicks$weight,
main = " Histogram of Chicken Weight",
xlab = "Weight (gm)",
ylab = "Counts" ,
col = "#c0fdff",
border = "#5465ff",
ylim = c(0, 250),
labels =T)
boxplot() FORMULA.
boxplot( column/variable/vector ,
main= "Title" ,
ylab= "Vertical Line Name" ,
col = "color-name/code ,
border = "border-color",
ylim = c( #start , #end ),
horiz = TRUE/FALSE)
boxplot(chicks$Time,
main = "Boxplot of Chicken's Number of Days Since Birth",
ylab = "Number of Days",
ylim = c(0, 25),
col = "#f4d35e",
border = "#f72585")