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")

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