## ggplot2 包的学习与应用
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.3.2
# qplot()函数
diamonds #砖石数据
## # A tibble: 53,940 × 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
## 7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47
## 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53
## 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49
## 10 0.23 Very Good H VS1 59.4 61 338 4.00 4.05 2.39
## # ... with 53,930 more rows
qplot(carat,price,data=diamonds) #重量和价格图像

# 对数变换看是否为指数关系
qplot(log(carat),log(price),data=diamonds)

# 体积 vs 重量
qplot(carat,x*y*z,data=diamonds)

## 装饰属性
set.seed(1410)#设置随机数种子
dsmall<-diamonds[sample(nrow(diamonds),100),]
dsmall
## # A tibble: 100 × 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 1.35 Ideal J VS2 61.4 57 5862 7.10 7.13 4.37
## 2 0.30 Good G VVS1 64.0 57 678 4.23 4.27 2.72
## 3 0.75 Ideal F SI2 59.2 60 2248 5.87 5.92 3.49
## 4 0.26 Ideal F VS1 60.9 57 580 4.13 4.11 2.51
## 5 0.33 Premium H VVS1 61.4 59 752 4.42 4.44 2.72
## 6 1.52 Ideal G VVS1 62.4 55 15959 7.30 7.39 4.58
## 7 0.32 Ideal G IF 61.3 54 918 4.41 4.47 2.72
## 8 2.25 Ideal I SI2 62.4 57 17143 8.39 8.32 5.21
## 9 0.25 Premium E VVS2 62.5 59 740 4.04 4.02 2.52
## 10 1.02 Premium H I1 62.5 60 3141 6.39 6.41 4.00
## # ... with 90 more rows
qplot(carat,price,data=dsmall,colour=color)#画不同的颜色

## 散点的形状
qplot(carat,price,data=dsmall,shape=cut)

# Alpha值 透明度
qplot(carat,price,data=diamonds,alpha=I(1/10))

qplot(carat,price,data=diamonds,alpha=I(1/100))

## 平滑曲线
qplot(carat,price,data=dsmall,colour=color,geom=c("point","smooth"))
## `geom_smooth()` using method = 'loess'

qplot(carat,price,data=dsmall,geom=c("point","smooth"))
## `geom_smooth()` using method = 'loess'

# 箱线图
qplot(color,price/carat,data=diamonds,geom="boxplot")

# jittre
qplot(color,price/carat,data=diamonds,geom="jitter")

# 直方图
qplot(carat,data=diamonds,geom="histogram",fill=color)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# 密度曲线
qplot(carat,data=diamonds,geom="density")

qplot(carat,data=diamonds,geom="density",colour=color)

## economics 数据集
head(economics,10)
## # A tibble: 10 × 6
## date pce pop psavert uempmed unemploy
## <date> <dbl> <int> <dbl> <dbl> <int>
## 1 1967-07-01 507.4 198712 12.5 4.5 2944
## 2 1967-08-01 510.5 198911 12.5 4.7 2945
## 3 1967-09-01 516.3 199113 11.7 4.6 2958
## 4 1967-10-01 512.9 199311 12.5 4.9 3143
## 5 1967-11-01 518.1 199498 12.5 4.7 3066
## 6 1967-12-01 525.8 199657 12.1 4.8 3018
## 7 1968-01-01 531.5 199808 11.7 5.1 2878
## 8 1968-02-01 534.2 199920 12.2 4.5 3001
## 9 1968-03-01 544.9 200056 11.6 4.1 2877
## 10 1968-04-01 544.6 200208 12.2 4.6 2709
## 数据集
mpg
## # A tibble: 234 × 11
## manufacturer model displ year cyl trans drv cty hwy
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29
## 3 audi a4 2.0 2008 4 manual(m6) f 20 31
## 4 audi a4 2.0 2008 4 auto(av) f 21 30
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26
## 7 audi a4 3.1 2008 6 auto(av) f 18 27
## 8 audi a4 quattro 1.8 1999 4 manual(m5) 4 18 26
## 9 audi a4 quattro 1.8 1999 4 auto(l5) 4 16 25
## 10 audi a4 quattro 2.0 2008 4 manual(m6) 4 20 28
## # ... with 224 more rows, and 2 more variables: fl <chr>, class <chr>
head(mpg,5)
## # A tibble: 5 × 11
## manufacturer model displ year cyl trans drv cty hwy fl
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p
## 3 audi a4 2.0 2008 4 manual(m6) f 20 31 p
## 4 audi a4 2.0 2008 4 auto(av) f 21 30 p
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p
## # ... with 1 more variables: class <chr>
qplot(displ,hwy,data=mpg,colour=factor(cyl))

#######3
## ggtheme 包
# viewprot()函数
#箱线图
p <- ggplot(mpg, aes(class,hwy,fill=class))
p+geom_boxplot()

p+geom_violin(alpha=0.3,width=0.9)+
geom_jitter(shape=21)

#随机生成100次风向,并汇集到16个区间内
dir <- cut_interval(runif(100,0,360),n=16)
#随机生成100次风速,并划分成4种强度
mag <- cut_interval(rgamma(100,15),4)
sample <- data.frame(dir=dir,mag=mag)
#将风向映射到X轴,频数映射到Y轴,风速大小映射到填充色,生成条形图后再转为极坐标形式即可
p <- ggplot(sample,aes(x=dir,y=..count..,fill=mag))
p + geom_bar()+ coord_polar()
