繪製基本圖形

增添額外資訊

plot type

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

plot example

data(iris)
str(iris)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
plot(iris$Petal.Length,iris$Petal.Width,type='n')
points(iris$Petal.Length,iris$Petal.Width,col=iris$Species)
abline(h=0.9,col='grey',lty = 2)
abline(v=2.5,col='grey',lty = 2)
#legend(4.5,1,pch=1,legend = levels(iris$Species),col=1:3)
legend('bottomright',pch=1,legend = levels(iris$Species),col=1:3)
#text(iris$Petal.Length+0.1,iris$Petal.Width+0.05,1:nrow(iris),cex=0.5)
text(iris$Petal.Length+0.1,iris$Petal.Width+0.05,paste('(',iris$Petal.Length,', ',iris$Petal.Width,')'),cex=0.5)

text(2,2,'aaaaa',cex=2)

title("iris")

package:ggplot2

why ggplot2?
  • fancy by default, good for demo and report
  • consistent across all kinds of plot in syntax and behavior
  • strong support community(the mostly download package on CRAN)
#basic syntax
#ggplot(data,aes(x,y,group,...))+geom_object(...)

#install.packages('ggplot2')

library('ggplot2')
setwd('~/lecture/riii')

load('./Statistics/cdc.Rdata')
cdc$exerany = as.factor(cdc$exerany)
cdc$hlthplan = as.factor(cdc$hlthplan)
cdc$smoke100 = as.factor(cdc$smoke100)

g <- ggplot(cdc,aes(x=height,y=weight))
g+geom_point()

g <- ggplot(cdc,aes(x=height,y=weight,col=gender))
g+geom_point()

g <- ggplot(cdc,aes(x=height,y=weight))
g+geom_point(aes(col=gender))

g <- ggplot(cdc,aes(x=genhlth))
g+geom_bar()

g+geom_bar() + ylab('次數') + ggtitle('健康狀況長條圖')

#fill => 填滿顏色; color => 邊線顏色
g+geom_bar(fill='snow',color='black')

g+geom_bar(aes(fill=gender))+ylab("次數")+ggtitle("健康狀況長條圖")

ggplot2(續)

## theme
g+geom_bar(aes(col=gender))+ylab("次數")+ggtitle("健康狀況長條圖") + theme(text=element_text(size=16,  family="Songti SC"))

#stat function
?geom_bar
g <- ggplot(cdc,aes(x=genhlth))
g+geom_bar()

g+stat_count()

##position
g = ggplot(cdc,aes(x=gender))
g+geom_bar(aes(fill=genhlth),position='stack')

g+geom_bar(aes(fill=genhlth),position='dodge')

g+geom_bar(aes(fill=genhlth),position='fill')

g+geom_bar(aes(fill=genhlth),position='identity')

## geom_area example
g = ggplot(data=cdc,aes(x=weight))
g+geom_area(aes(y=..density..,fill=gender), stat = "bin")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

g+geom_area(aes(y=..density..,fill=gender), stat = "bin",position='stack')
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

g+geom_area(aes(y=..density..,fill=gender), stat = "bin",position='identity')
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

g+geom_area(aes(y=..density..,fill=gender), stat = "bin",position='identity',alpha=0.4)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

g+geom_area(aes(y=..density..,fill=gender), stat = "bin",position='identity',alpha=0.5)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#facet
g <- ggplot(cdc,aes(x=weight))
g+ geom_histogram()+facet_wrap(~genhlth)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

g+ geom_histogram()+facet_grid(~genhlth)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

g+ geom_histogram()+facet_grid(gender~genhlth)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#coordinate
g <- ggplot(cdc,aes(x=genhlth))
g+geom_bar()+coord_flip()

g+geom_bar()+coord_polar(theta = 'x')

g+geom_bar()+coord_polar(theta = 'y')

g+geom_bar(aes(fill=gender))+coord_polar(theta = 'y')

#pie chart
ggplot(cdc,aes(x=1)) + geom_bar(aes(fill=genhlth)) + coord_polar(theta = 'y')

precounted = as.data.frame(table(cdc$genhlth,dnn = c('genhlth')))
precounted
##     genhlth Freq
## 1 excellent 4657
## 2 very good 6972
## 3      good 5675
## 4      fair 2019
## 5      poor  677
g2 = ggplot(precounted,aes(x=genhlth,y=Freq))+ geom_bar(stat='identity')

#save
ggsave(filename = './g2.jpg',plot=g2)
## Saving 7 x 5 in image

Esquisse

#install.packages('esquisse')
#library('esquisse')
#esquisse::esquisser()