[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章

这个主要是对《ggplot2数据分析与图形艺术》第三章相关练习题的总
ggplot2数据分析与图形艺术课后题:第二章
ggplot2数据分析与图形艺术课后题:第三章
ggplot2数据分析与图形艺术课后题:第五章
ggplot2数据分析与图形艺术课后题:第六章
ggplot2数据分析与图形艺术课后题:第七章
ggplot2数据分析与图形艺术课后题:第八章
library(tidyverse)

3.2 基本图形类型 1.下列图像分别使用哪种几何对象
  1. 散点图:
一般来说使用geom_point。如果散点图的的点有很多重叠的话,可以使用geom_jitter
ggplot(mpg, aes(displ, hwy, color = class)) + geom_point()

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png
  1. 折线图
如果只是从左到右连接各个点的话,可以使用geom_line。如果是按照顺序连接的话可以使用geom_path
ggplot(mpg, aes(displ, hwy, color = class)) + geom_line()

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png
  1. 直方图
直方图我们使用geom_histogram和这个类似用来展示分布的曲线则是geom_freqpoly
ggplot(mpg, aes(displ)) + geom_histogram(color = "grey")+ geom_freqpoly(color = "red")## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png
  1. 条形图
如果在原数据是没有计算各个分类的个数的话,直接使用geom_bar即可。如果已经计算出来了。则可以使用geom_bar(stat = "identity")或者使用geom_col即可
ggplot(mpg, aes(manufacturer)) + geom_bar()

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png
result <- mpg %>% count(manufacturer) head(result)## # A tibble: 6 x 2 ##manufacturern ## ## 1 audi18 ## 2 chevrolet19 ## 3 dodge37 ## 4 ford25 ## 5 honda9 ## 6 hyundai14ggplot(result, aes(manufacturer, n)) + geom_bar(stat = "identity")

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png
  1. 饼图
ggplot2系统没有一个绘图函数可以用来绘制饼图,其绘制饼图的原理是通过折叠坐标轴实现了。使用的函数是coord_polar
ggplot(result, aes("manufacturer", n, fill = manufacturer)) + geom_bar(stat = "identity") + coord_polar(theta = 'y')

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png
  1. line/path/polygon的区别
我们在进行折线图绘制的时候,gemo_line绘制是基于横坐标从大到小的对各个点进行连接,geom_path是基于数据对的顺序对各个对,
进行连接;geom_polygon则是在path的基础上把最后一个和第一个连接起来。形成一个环形
df <- data.frame(x = c(3,1,5), y = c(2,4,6)) p1 <- ggplot(df, aes(x, y)) + geom_line() + ggtitle("line") p2 <- ggplot(df, aes(x, y)) + geom_path() + ggtitle("path") p3 <- ggplot(df, aes(x, y)) + geom_polygon() + ggtitle("polygon") cowplot::plot_grid(p1,p2,p3,nrow = 1)

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png
  1. smooth/boxplot的底层数据
    ===========================
geom_smooth的绘制需要的是两个连续性的变量。geom_boxplot以及geom_violin需要的则是一个分类变量和一个连续性变量即可.
3.5 群组几何对象
  1. cyl的设置
cyl在数据当中是是数值,默认的如果对于连续性变量来为X绘制箱式图的时候,会把所有的数据拟合到一起。这个时候就是这样的
ggplot(mpg, aes(cyl, hwy)) + geom_boxplot()## Warning: Continuous x aesthetic -- did you forget aes(group=...)?

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png 如果要进行分开绘图,需要设置group参数。
ggplot(mpg, aes(cyl, hwy, group = cyl)) + geom_boxplot()

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png PS:X轴其实还是连续性变量。这样的情况。最好还是设置到因子变量来进行绘制。
  1. 调整绘图
绘制displ里每个整数取值的箱式图。这个对于绘图而言没有想到很好的解决办法。目前想到的是从原数据入手
ggplot(mpg[mpg$displ %in% 2:7,], aes(displ, cty, group = displ)) + geom_boxplot()

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png
  1. 颜色映射到线条
我们来先看离散型变量group = 1设置和不设置的区别
p1 <- ggplot(mpg[1:10,], aes(cty, hwy, color = factor(cyl))) + geom_line() p2 <- ggplot(mpg[1:10,], aes(cty, hwy, color = factor(cyl), group = 1)) + geom_line() cowplot::plot_grid(p1, p2, nrow = 1)

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png 通过两个图我们发现,默认的如果不设置group的话,离散型的color会自定设置到group上。通过设置group = 1则代表所线段都连接在一起,然后不同的线段用颜色来区分。
p3 <- ggplot(mpg[1:10,], aes(cty, hwy, color = factor(cyl), group ="asd")) + geom_line() cowplot::plot_grid(p2,p3)

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png 【[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章】至于group = 1group = 2则是一样的。如果我们写group = "asd"这是一样。只是让group统一到一个绘图分组里面
4有多少个条形 默认的图形有多少个条形就看有多少个分类。
length(unique(mpg$drv))## [1] 3ggplot(mpg, aes(drv)) + geom_bar(color = "white")

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png 加了hwy的颜色填充之后,就要看有多少个hwy在drv分组当中有多少个分类
unique(mpg[c("drv", "hwy")]) %>% nrow## [1] 46ggplot(mpg, aes(drv, fill = hwy, group = hwy)) + geom_bar(color = "white")

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png 形成一个递增的变量之后,在用这个变量进行分类的话,则会形成每个变量有一个条形。
mpg2 <- mpg %>% arrange(hwy) %>% mutate(id = seq_along(hwy)) ggplot(mpg2, aes(drv, fill = hwy, group = id)) + geom_bar(color = "white")

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png 5。调整图形 题目给的图形是这样的
library(babynames) hadley <- filter(babynames, name == "Hadley") head(hadley)## # A tibble: 6 x 5 ##year sexnamenprop ## ## 11906 MHadley6 0.0000416 ## 21908 MHadley16 0.0000962 ## 31909 MHadley14 0.0000792 ## 41910 MHadley5 0.0000240 ## 51911 MHadley9 0.0000373 ## 61912 MHadley11 0.0000244ggplot(hadley, aes(year, n)) + geom_line()

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png 这个图形,我们主要是想展示每一年出生孩子叫Hadley的数量。对于一个计数资料,我们在可视化时候,使用柱状图相对来说会比线性图好一些。同时可以把性别分布标记出来。这样的话,我们的图就可以是这样的
ggplot(hadley, aes(year, n, fill = sex)) + geom_col()

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png 3.11 展示数据分布
  1. 最好的组间距
p1 <- ggplot(diamonds, aes(carat)) + geom_histogram(binwidth = 0.01) p2 <- ggplot(diamonds, aes(carat)) + geom_histogram(binwidth = 0.1) p3 <- ggplot(diamonds, aes(carat)) + geom_histogram(binwidth = 1) cowplot::plot_grid(p1,p2,p3, nrow = 1)

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png 经过观察,发现其实还是0.01的会好一些
  1. price的直方图
ggplot(diamonds, aes(price)) + geom_histogram(binwidth = 30)

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png 我们发现,关于价格分部分,呈现一个左偏态的分布。同时中间一部分见过有一些断档。
clarity和carat的关系 我们可以通过箱式图来看两者之间的关系
ggplot(diamonds,aes(clarity, carat, color = clarity)) + geom_boxplot(show.legend = F) + scale_color_brewer(palette = "Set2")

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png 通过图形可以看到,钻石的净度越好,其克拉的重量相对来说也就越好
拼图多边图和密度分布图比较 默认的如果我们绘制histogram以及freqpoly的时候。Y轴是每一个分组计数。但是density则每一个分组的密度。因此,如果不设置Y轴为统一单位的话,那么就没办法绘制。
ggplot(diamonds, aes(depth, y = ..density..)) + geom_freqpoly() + geom_density()## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

[R|[R|ggplot2] ggplot2数据分析与图形艺术课后题:第三章
文章图片
image.png

    推荐阅读