Cac loai bieu do tu data frame “mtcars”

Barplot

Bien roi rac: chon bien gear

attach(mtcars)
barplot(table(gear), col='purple', main='Barplot of frequency by gear', ylab='Frequency', xlab='gear')

Bien roi rac phan nhom: chon bien hp

q = hp
q[hp<50] <- 'hp1'
q[hp>=50 & hp<100] <- 'hp2'
q[hp>=100 & hp<200] <- 'hp3'
q[hp>=200] <- 'hp4'
barplot(table(q), ylim=c(0,20), ylab='Frequency', main='Barplot of frequency by hp', col=c('red','yellow','green'))

Bien lien tuc: chon bien disp

a = cut(disp,5)
barplot(table(a), xlab='disp', ylab='Frequency', main='Barplot of Frequency by disp', col='blue')

ggplot

Bien roi rac (gear)

library('ggplot2')
## 
## Attaching package: 'ggplot2'
## The following object is masked from 'mtcars':
## 
##     mpg
z = ggplot(data=mtcars, aes(x=gear))
z + geom_bar(width=0.5, col='black', fill='orange')  + theme_bw() + ggtitle('ggplot of gear')

Bien roi rac phan nhom (bien gear theo nhom am))

s = ggplot(data=mtcars, aes(x=gear, fill=as.factor(am)))
s + geom_bar() + theme_bw() + theme(legend.position='top') + ggtitle('ggplot of frequency by gear by am')

Bien lien tuc (disp)

p = ggplot(mtcars,aes(x=disp))
p + geom_histogram(col='blue', aes(fill=..count..)) + theme_bw() + ggtitle('ggplot of frequency by disp')
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

qplot

Bien roi racc (gear)

qplot(data=mtcars, gear, geom='bar', ylab='Frequency of cars', col=I('red'), fill=I('cyan'))

Bien roi rac phan nhom (gear theo am)

qplot(data=mtcars, gear, geom='bar', ylab='Frequency of cars', col=I('black'), fill=as.factor(am))

Bien lien tuc (disp)

qplot(disp, col=I('red'), fill=I('yellow'), ylab='Frequency of car')
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Baitap1 by Thong