1-ggplot2

作者: 泥人吴 | 来源:发表于2019-08-24 17:26 被阅读0次
准备工作
#安装tidyverse
install.packages("tidyverse")
# 加载tidyverse
library(tidyverse)

1.2.2 创建ggplot图形

ggplot(data = mpg)+
       geom_point(mapping = aes(x=displ,y=hwy))
  • mapping参数没定义了如何将数据集中的变量映射为图形属性。mapping总是与aes()函数成对出现。

1.3图形属性映射

# 颜色
ggplot(data = mpg)+
        geom_point(mapping = aes(x=displ,y=hwy,,color=class))
#大小size
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy, size = class))
  • 自己形状选择


    shape

1.5分页

  • 单个变量对图进行分面facet_wrap( )
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) + 
  facet_wrap(~ class, nrow = 2)
# 按照~class分类,两行nrow=2

nrow指定分面后显示几行
ncol指定分面后显示几列
注意~分面依据必须离散型变量

  • 要想通过两个变量对图进行分面,需要在绘图命令中加入函数 facet_grid()。这个函数的 第一个参数也是一个公式,但该公式包含由 ~ 隔开的两个变量名。
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) + 
  facet_grid(drv ~ cyl)

1.6对象

# left
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy))

# right
ggplot(data = mpg) + 
  geom_smooth(mapping = aes(x = displ, y = hwy))
  • 几何对象是图中用来表示数据的几何图形对象。
ggplot(data = mpg) + 
  geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv))
#隐式分组-线型(下图)

#隐式分组-颜色
ggplot(data = mpg) +
  geom_smooth(
    mapping = aes(x = displ, y = hwy, color = drv),
    show.legend = FALSE  #不显示图例
  )
  • 根据表示汽车驱动系统的 drv 变量的值,这里的 geom_smooth() 函数分别用 3 条曲线来表 示汽车。


  • 同一张图显示多个几何对象--局部映射和全局映射
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) +
  geom_smooth(mapping = aes(x = displ, y = hwy))
# 全局映射
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
  geom_point() + 
  geom_smooth()
  • 如果将映射放在几何对象函数中,那么 ggplot2 会将其看作这个图层的局部映射,它将使 用这些映射扩展或覆盖全局映射,但仅对该图层有效。这样一来,我们就可以在不同的图 层中显示不同的图形属性:
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
  geom_point(mapping = aes(color = class)) +
  geom_smooth()

1.7统计转换

  • 绘图时用来计算新数据的算法称为 stat(statistical transformation,统计变换)。
  • 每个几何对象函数都有一个默认统计变换,每个统计变换函数都有 一个默认几何对象。一般情况下,这意味着你在使用几何对象函数时不用担心底层的统计 变换。


    统计转换

1.8 位置调整

(1)直方图之堆叠式-fill
# left
ggplot(data = diamonds) +
       geom_bar(mapping = aes(x = cut, color = cut))
# right
ggplot(data = diamonds) +
       geom_bar(mapping = aes(x = cut, fill = cut))
# 所以fill和color表示不同
(2)直方图之对象直接显示-identity
ggplot(data = diamonds, mapping = aes(x = cut, fill = clarity)) + 
  geom_bar(alpha = 1/5, position = "identity")
ggplot(data = diamonds, mapping = aes(x = cut, colour = clarity)) + 
  geom_bar(fill = NA, position = "identity")
(3)直方图之并列式-dodge
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
(4)散点图之扰动-jitter
  • 防止点重叠
# 原始散点图
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy))
#扰动后:
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy), position = "jitter")

1.9 坐标系

(1)coord_flip翻转坐标系
  • 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()
(3)coord_polar 极坐标系
  • 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)
print(bar)
bar + coord_flip() #箱图倒置
bar + coord_polar() #鸡冠图

相关文章

  • 1-ggplot2

    准备工作 1.2.2 创建ggplot图形 mapping参数没定义了如何将数据集中的变量映射为图形属性。mapp...

  • R语言ggplot2系列1-ggplot2简介

    简单介绍一下ggplot2 既然要做“系列”当然要先介绍一下主角-ggplot2。ggplot2是一个十分强大的R...

网友评论

    本文标题:1-ggplot2

    本文链接:https://www.haomeiwen.com/subject/vqnxectx.html