Line Chart

x <- 1:6
y <- 1:6

plot(x,y, type='l' )

plot(x,y, type='o' )

x1 <- 2:4
y1 <- c(3,3,3)
plot(x,y,type='n')
lines(x,  y, col="blue")
lines(x1, y1, col="red")

types <- c('p', 'l', 'o', 'b', 'c' , 's', 'h', 'n')
par(mfrow=c(2,4))
for(t in types){
  title <- paste('types:', t)
  plot(x,y, type = 'n', main = title)
  lines(x,y,type=t)
}

par(mfrow=c(1,1))
taipei <- c(92.5,132.6,168.8,159.1,218.7)
tainan <- c(21.2, 30.6, 37.3, 84.6, 184.3)

plot(taipei, type="o", col="blue", ylim=c(0,220), 
     xlab="Month", ylab="Rainfall")

lines(tainan , type="o", pch=22, lty=2, col="red")

Bar Chart

download.file('https://raw.githubusercontent.com/ywchiu/fubonr/master/data/house-prices.csv', 'house-prices.csv')

house <- read.csv('house-prices.csv')
bedroomsTable <- table(house$Bedrooms)

barplot(bedroomsTable, main='Bedrooms Type', xlab = 'Bedroom Type', ylab = 'count', col = "orange", ylim = c(0,80))

barplot(bedroomsTable, main='Bedrooms Type', xlab = 'Bedroom Type', ylab = 'count', col = c(1,2,3,4), ylim = c(0,80))

Histogram

load("D:/OS DATA/Desktop/cdc.Rdata")
hist(cdc$weight)

hist(cdc$height)

hist(cdc$weight, breaks= 500)

head(sort(table(cdc$weight), decreasing = TRUE))
## 
## 160 150 180 170 200 140 
## 992 970 933 922 805 794
table(cdc$weight %% 10)
## 
##    0    1    2    3    4    5    6    7    8    9 
## 9421  207  919  545  525 5865  481  543 1159  335
par(mfrow=c(2,1))
hist(cdc$weight, breaks = 50)
barplot(table(cdc$weight), xlab = 'weight', ylab = 'frequency')

#barplot(ta)

Pie Chart

bedroomsTable
## 
##  2  3  4  5 
## 30 67 29  2
labels <- c('2 unit', '3 unit', '4 unit', '5 unit')
rainbow(length(labels))
## [1] "#FF0000FF" "#80FF00FF" "#00FFFFFF" "#8000FFFF"
tb <- sort(bedroomsTable, decreasing = TRUE)
pie(tb, labels = labels, col = c('#3eb2ad', '#de6000', '#ff6257', '#517d7d'), main='pie chart of bedrooms', init.angle = 90, clockwise = TRUE)

Scatter Chart

plot(cdc$weight, cdc$wtdesire)

plot(cdc$weight, cdc$height)

data(iris)
xlab <- 'Sepal.Length'
ylab <- 'Petal.Length'
class(iris$Species)
## [1] "factor"
plot(iris$Sepal.Length, iris$Petal.Length,col = iris$Species)
abline(h = 4.7, col="orange")
abline(h = 2.4, col="blue")

versicolor <- iris[iris$Species == 'versicolor',]
virginica <- iris[iris$Species == 'virginica',]

plot(versicolor$Petal.Length, versicolor$Petal.Width, col="red", xlim= c(0,6), ylim= c(0,2))
points(virginica$Petal.Length, virginica$Petal.Width, col="orange")

plot(cdc$weight, cdc$wtdesire, xlab = 'Weight', ylab = 'Desire Weight')

fit <- lm(wtdesire ~ weight, data = cdc)
fit
## 
## Call:
## lm(formula = wtdesire ~ weight, data = cdc)
## 
## Coefficients:
## (Intercept)       weight  
##      46.664        0.639
plot(cdc$weight, cdc$wtdesire, xlab = 'Weight', ylab = 'Desire Weight')
abline(fit, col="red")

barplot(c(80, 82 ,84, 88 ))

## Mosaic Plot

table(cdc$gender)
## 
##     m     f 
##  9569 10431
table(cdc$smoke100)
## 
##     0     1 
## 10559  9441
tb <- table(cdc$gender, cdc$smoke100)
colnames(tb) <- c('Non-Smokers', 'Smokers')
rownames(tb) <- c('Male', 'Female')
mosaicplot(tb, col= c(2,3))

Box Chart

hist(cdc$height, breaks = 50)

boxplot(cdc$height)