2018-07-23 R for data science之使用ggplot2进行可视化

library(tidyverse)
library(ggplot2)

查看mpg数据结构

## 采用ggplot2自带的数据mpg来探索引擎与燃油效率之间的关系
## 变量displ:引擎的大小   hwy: 燃油的效率
View(mpg)
image.png

简单可视化

ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy))
image.png

ggplot2画图结构

ggplot(data = <DATA>) +
  <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))

Exercises

  1. Run ggplot(data = mpg). What do you see?
  2. How many rows are in mtcars? How many columns?
  3. What does the drv variable describe? Read the help for ?mpg to
    find out.
  4. Make a scatterplot of hwy versus cyl.
  5. What happens if you make a scatterplot of class versus drv? Why is the plot not useful?
1、 运行后得到的只是灰色的画布,我们并没有指定变量

2、查看数据有多少行列?
> dim(mpg)  ## 表示得到数据的维度
[1] 234  11  ##表示234行 11列
> ncol(mpg) # 列
[1] 11
> nrow(mpg) # 行
[1] 234

3、查看变量drv代表什么?
> ?mpg 
可以看到右边会显示得到如下结果
drv
f = front-wheel drive, r = rear wheel drive, 4 = 4wd 

4、绘制一个hwy与cyl的散点图
ggplot(mpg) +
  geom_point(aes(x = hwy, y = cyl))

5、ggplot(mpg) +
  geom_point(aes(x = class, y = drv))
image.png

Aesthetic Mappings

将颜色映射到class上

> unique(mpg$class)
[1] "compact"    "midsize"    "suv"        "2seater"    "minivan"    "pickup"     "subcompact"
> ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy, color = class))
image.png

以点的大小来代表每一个类别

ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy, size = class))
image.png

使用阴影程度来代表不同的类别

ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy, alpha = class))

使用不同的形状来代表不同的类别

ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy, shape = class))
image.png

如果想改变散点图中点的颜色?

ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
image.png

小结

  • 参数shape定义形状、color定义颜色,可以指定颜色或者根据变量中的level自动填充、size以点的大小来表示,alpha可以定义阴影程度。
  • shape参数形状汇总


    image.png

Exercises

  1. What happens if you map an aesthetic to something other than
    a variable name, like aes(color = displ < 5)?
ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy, color = displ < 5))
## 表示按照条件来填充颜色,不符合小于5的为一种颜色,符合小于5的为一种颜色
image.png

Facets (分面)

## 按照变量class里面不用的类来分面
ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy)) +
  facet_wrap(~ class, nrow = 2) ##表示两行
image.png
## 表示根据变量drv和cyl两个变量里面的类别进行排列组合即4*3=12个面
ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy)) +
  facet_grid(drv ~ cyl)
image.png
ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy)) +
  facet_grid(drv ~ .)  ## 表示以行展示
ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy)) +
  facet_grid(. ~ drv) ##表示以列展示
image.png

Geometric Objects

ggplot(data = mpg) +
  geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv))
image.png
ggplot(data = mpg) +
  geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv, color = drv))
image.png
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
  geom_point(mapping = aes(color = class)) +
  geom_smooth()
image.png

使用filter函数挑选某一类进行smooth

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
  geom_point(mapping = aes(color = class)) +
  geom_smooth(
    data = filter(mpg, class == "subcompact"),
    se = FALSE
  )
image.png

哈哈 中文版买了,现在开始就看中文版了

1.7 统计变换 (P19)

  • geom_bar() 统计变换的过程


    image.png
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut))
ggplot(data = diamonds) + stat_count(mapping = aes(x = cut))
这两句代码是等价的,在这里geom_bar()使用了stat_count()函数进行统计变换
image.png

如果你只想要显示比例而不是计数的话使用以下命令 ,y = ..prop..表示百分比的形式

ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))

你可能想要在代码中强调统计变换。例如,你可以使用 stat_summary() 函数将人们的注意力吸引到你计算出的那些摘要统计量上。 stat_summary() 函数为 x 的每个唯一值计算 y 值的摘要统计:

ggplot(data = diamonds) + stat_summary(mapping = aes(x = cut, y = depth), 
                                       fun.ymin = min,
                                       fun.ymax = max,
                                       fun.y = median
)
## 表示每一个变量对应的深度的值的分布,最大值、最小值、以及中位值,类似箱式图
image.png

1.8、位置调整

添加柱状图边框的颜色

ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, color = cut))
image.png

如果要设置柱状图填充的颜色,就需要使用fill参数

ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, fill = cut))
image.png

如果将 fill 图形属性映射到另一个变量(如 clarity),那么条形会自动分块堆叠起来。每个彩色矩形表示 cut 和 clarity 的一种组合。

ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, fill = clarity))
image.png

这种堆叠是由 position 参数设定的位置调整功能自动完成的。如果不想生成堆叠式条形图,你还可以使用以下 3 种选项之一: "identity"、 "fill" 和 "dodge"。

  • position = "identity" 将每个对象直接显示在图中。这种方式不太适合条形图,因为
    条形会彼此重叠。为了让重叠部分能够显示出来,我们可以设置 alpha 参数为一个较小
    的数,从而使得条形略微透明;或者设定 fill = NA,让条形完全透明:
ggplot(data = diamonds,mapping = aes(x = cut, fill = clarity)) +
  geom_bar(alpha = 1/5, position = "identity")

ggplot(data = diamonds, mapping = aes(x = cut, color = clarity)) +
  geom_bar(fill = NA, position = "identity")
fill = clarity

fill = NA
  • position = "fill" 的效果与堆叠相似,但每组堆叠条形具有同样的高度,因此这种条
    形图可以非常轻松地比较各组间的比例:
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity),position = "fill")
image.png
  • position = "dodge" 将每组中的条形依次并列放置,这样可以非常轻松地比较每个条形
    表示的具体数值:
ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
image.png

1.9、坐标系

坐标系可能是 ggplot2 中最复杂的部分。默认的坐标系是笛卡儿直角坐标系,可以通过其独立作用的 x 坐标和 y 坐标找到每个数据点。

  • coord_flip() 函数可以交换 x 轴和 y 轴。
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
  geom_boxplot()

ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
  geom_boxplot() +
  coord_flip()
image.png
  • coord_polar() 函数使用极坐标系。极坐标系可以揭示出条形图和鸡冠花图间的一种有趣联系:
bar <- ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, fill = cut), show.legend = FALSE,width = 1) +
  theme(aspect.ratio = 1) + labs(x = NULL, y = NULL)
bar
bar + coord_flip()
bar + coord_polar()
image.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,783评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,360评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 142,942评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,507评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,324评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,299评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,685评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,358评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,652评论 1 293
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,704评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,465评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,318评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,711评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,991评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,265评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,661评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,864评论 2 335

推荐阅读更多精彩内容

  • 简介 文章较长,点击直达我的博客,浏览效果更好。本文内容基本是来源于STHDA,这是一份十分详细的ggplot2使...
    taoyan阅读 50,658评论 7 159
  • 写在前面 ggplot2 是一个功能强大且灵活的R包 ,由Hadley Wickham 编写,其用于生成优雅的图...
    Boer223阅读 27,699评论 0 67
  • 一、何谓数据分析 数据分析是指用适当的统计分析方法对收集来的大量数据进行分析,将它们加以汇总和理解并消化,以求最大...
    知奇者也阅读 928评论 0 2