# 3.1.2 ggplot2绘图

## 1. 读取数据

    rm(list = ls())
    # install.packages(ggplot2)
    library(ggplot2)
    # install.packages(plyr)
    library(plyr)
    # 加载钻石数据
    data("diamonds")
    # 抽取前500条数据,且保留6个变量
    set.seed(30)
    diamond = diamonds[sample(nrow(diamonds), 500), c(1:4, 7, 10)]
    head(diamond)
    ##  # A tibble: 6 x 6
    ##    carat     cut color clarity price     z
    ##    <dbl>   <ord> <ord>   <ord> <int> <dbl>
    ##  1  0.90   Ideal     E     SI2  3808  3.86
    ##  2  2.01   Ideal     G     SI2 15841  5.05
    ##  3  1.50    Good     I     SI1  8254  4.46
    ##  4  0.38   Ideal     G     SI1   629  2.86
    ##  5  1.15   Ideal     G     VS2  6492  4.15
    ##  6  1.12 Premium     I     VS2  4321  4.13
    summary(diamond)
    ##       carat               cut      color      clarity        price
    ##   Min.   :0.2300   Fair     : 16   D: 69   VS2    :126   Min.   :  366.0
    ##   1st Qu.:0.4000   Good     : 40   E: 88   SI1    :116   1st Qu.:  970.8
    ##   Median :0.7100   Very Good:102   F: 88   SI2    : 82   Median : 2546.0
    ##   Mean   :0.7881   Premium  :130   G:107   VS1    : 76   Mean   : 3793.2
    ##   3rd Qu.:1.0400   Ideal    :212   H: 77   VVS2   : 46   3rd Qu.: 5150.2
    ##   Max.   :2.4100                   I: 56   VVS1   : 32   Max.   :18274.0
    ##                                    J: 15   (Other): 22
    ##         z
    ##   Min.   :2.430
    ##   1st Qu.:2.900
    ##   Median :3.540
    ##   Mean   :3.538
    ##   3rd Qu.:4.020
    ##   Max.   :5.280
    ##

## 2. 柱状图

    ## 柱状图 ##
    # 基础柱状图
    p = ggplot(data = diamond, mapping = aes(x = clarity))
    p + geom_bar()

    # 增加其他映射元素,成为累计柱状图
    p = ggplot(data = diamond, mapping = aes(x = clarity, fill = cut))
    p + geom_bar()

    # 分组柱状图
    p = ggplot(data = diamond, mapping = aes(x = clarity, fill = cut))
    p + geom_bar(position = "dodge")

## 3. 饼图

    ## 饼图 ##
    df1 = ddply(diamond, .(cut), nrow)
    (df1 = df1[order(df1$V1, decreasing = T), ])
    ##          cut  V1
    ##  5     Ideal 212
    ##  4   Premium 130
    ##  3 Very Good 102
    ##  2      Good  40
    ##  1      Fair  16
    (pos = (cumsum(df1$V1) - df1$V1/2))
    ##  [1] 106 277 393 464 492
    ggplot(df1, aes(x="", y = V1, fill = factor(cut))) +
      geom_bar(width = 1,stat = "identity") +
      scale_fill_manual(values = rainbow(5,alpha = 0.4))

    ggplot(df1, aes(x="", y = V1, fill = factor(cut))) +
      geom_bar(width = 1,stat = "identity") +
      coord_polar(theta = "y") +
      geom_text(aes(y = pos, label = paste(round(V1 / sum(V1) * 100, 2), "%", ""))) +
      scale_fill_manual(values = rainbow(5,alpha = 0.4)) +
      theme(axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank())

## 4. 直方图

    ## 直方图 ##
    # 基础作图
    p = ggplot(data = diamond, mapping = aes(x = price))
    p + geom_histogram()

    # 调整组距
    p = ggplot(data = diamond, mapping = aes(x = price))
    p + geom_histogram(binwidth = 500)

    # 调整组数
    p = ggplot(data = diamond, mapping = aes(x = price))
    p + geom_histogram(bins = 100)

    # 按照切工分颜色
    p = ggplot(data = diamond, mapping = aes(x = price, fill = cut))
    p + geom_histogram(binwidth = 500)

    # 按照切工画密度曲线
    ggplot(data = diamond, mapping = aes(price, colour = cut)) +
      geom_freqpoly(binwidth = 500)

    # 分面来看不同切工的对比
    p = ggplot(data = diamond, mapping = aes(x = price, fill = cut))
    p + geom_histogram() + facet_grid( ~ cut)

## 5. 箱线图

    ## 箱线图 ##
    # 分组箱线图
    ggplot(diamond) + geom_boxplot(aes(x = cut, y = price, fill = cut))

    # 增加自定义配色
    ggplot(diamond) + geom_boxplot(aes(x = cut, y = price, fill = cut)) + scale_fill_manual(values = c("lightpink", "lightyellow", "lightgreen", "lightblue", "mediumpurple1"))

    ggplot(diamond) + geom_boxplot(aes(x = cut, y = price, fill = cut)) + scale_fill_manual(values = rainbow(5, alpha = 0.4))

## 6. 折线图

    ## 折线图 ##
    # 将搜索指数index变成时间序列格式
    index = c(127910, 395976, 740802, 966845, 1223419, 1465722, 1931489, 2514324, 3024847, 3174056, 3208696, 3644736, 4198117, 3868350, 3576440, 3524784, 3621275, 3695967, 3728965, 3845193, 3525579, 3452680, 3535350, 3655541, 3884779, 3780629) / 10000
    dat = seq(as.Date("2017/3/28"), length = 26, by = "day")
    people_index = data.frame(date = dat, index = index)
    p = ggplot(people_index, mapping = aes(x = date, y = index))
    p + geom_line()

    p + geom_line(colour = "green") + geom_area(colour = "green", alpha = 0.2)

## 7. 散点图

    ## 散点图 ##
    # 基础作图
    p = ggplot(data = diamond, mapping = aes(x = carat, y = price))
    p + geom_point()

    # 添加映射元素
    # 根据定性变量标识不同颜色
    p = ggplot(data = diamond, mapping = aes(x = carat, y = price, colour = cut))
    p + geom_point()

    # 根据定量变量标识不同颜色
    p = ggplot(data = diamond, mapping = aes(x = carat, y = price, colour = z))
    p + geom_point()

    # 增加统计变换
    p = ggplot(diamond, aes(x = carat, y = price)) + geom_point()
    p + scale_y_log10()

    # 增加拟合曲线
    p = ggplot(diamond, aes(x = carat, y = price)) + geom_point()
    p + scale_y_log10() + stat_smooth()

    # 基于cut分块
    p = ggplot(diamond, aes(x = carat, y = price)) + geom_point() + scale_y_log10() + stat_smooth()
    p + facet_grid( ~ cut)