ggplot2绘图之基本概念

1.映射

1)概念: 映射即为数据集中的数据关联到相应的图形属性过程中一种对应关系, 是将一个变量中离散或连续的数据与一个图形属性中以不同的参数来相互关联, 而设定能够将这个变量中所有的数据统一为一个图形属性。aes()函数是ggplot2中的映射函数

例:ggplot2绘图之基本概念

ggplot2绘图之基本概念ggplot2绘图之基本概念

最后一行语句为错误的映射关系, 在aes中, color = “blue”的实际意思是把”blue”当为一个变量, 用这个变量里的数据去关联图形属性中的参数, 因为”blue”只含有一个字符变量, 默认情况下为离散变量, 按默认的颜色标度标记为桃红色

2)分组

默认情况下ggplot2把所有观测点分为了一组, 如果需要把观测点按额外的离散变量进行分组处理, 必须修改默认的分组设置

ggplot2绘图之基本概念 

ggplot2绘图之基本概念ggplot2绘图之基本概念

2.图层

每个图层可以代表一个图形组件, 例如下面要介绍的几何对象、统计变换等图形组件, 这些组件以图层的方式叠加在一起构成一个绘图的整体, 在每个图层中的图形组件又可以分别设定数据、映射或其他相关参数, 因此组件之间又是具有相对独立性的。图层指甲你用+连接

1)在几何对象中设置映射

p <- ggplot(mtcars, aes(x = mpg, y = wt, color = factor(gear)))
#设定默认的映射关系
p + geom_point()
#沿用默认的映射关系来绘制散点图
p + geom_point(aes(shape = factor(carb)))
#添加图层中的shape的映射关系
p + geom_point(aes(y = carb)))
#修改默认的y的映射关系, 注意图中y轴名称仍然以默认的wt表示
p + geom_point(aes(color = NULL))
#删除默认的color映射关系

ggplot2绘图之基本概念ggplot2绘图之基本概念

ggplot2绘图之基本概念ggplot2绘图之基本概念

2) 采用多个数据集或向量数据绘图

1

2

3

4

5

6

7

8

#构建不同于mtcars的数据集mtcars.c

mtcars.c <- transform(mtcars, mpg = mpg^2)

ggplot()+

  geom_point(aes(x = hp, y = mpg), data = mtcars, color = "red") +

  geom_point(aes(x = mtcars$hp, y = mtcars$disp), color = "green")+

  #选用向量数据

  geom_point(aes(x = hp, y= mpg), data = mtcars.c, color = "blue")

  #选用不同的数据集

3.几何对象(geom)和统计变换(stat)
    几何对象执行着图层的实际渲染, 控制着生成的图像类型。例如用geom_point()将会生成散点图, 而geom_line会生成折线图。统计变换即对数据进行统计变化, 通常以某种方式对数据信息进行汇总, 例如通过stat_smooth()添加光滑曲线。

1)geom_point()画图

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

p <- ggplot(mtcars, aes(wt, mpg))

p + geom_point()

#更改颜色-连续变量

p + geom_point(aes(color = qsec))

#更改颜色-离散变量

p + geom_point(aes(color = factor(gear)))

#更改透明度

p + geom_point(aes(alpha = qsec))

#更改形状

p + geom_point(aes(shape = factor(gear)))

#更改点大小

p + geom_point(aes(size = qsec))

#两种颜色的叠加

p + geom_point(color = "grey50", size = 5) + geom_point(aes(color = qsec), size = 4)

#颜色和形状的叠加

p + geom_point(color = "grey50", size = 5) + geom_point(aes(shape = factor(gear)), size = 3)

 

2) geom_histogram()

1

2

3

4

5

6

7

m <- ggplot(movies, aes(rating))

#这里使用movies数据集

m + geom_histogram()

m + geom_histogram(bin = 0.5)

#调整分箱(bin)数据

m + geom_histogram(bin = 1)

m + geom_histogram(bin = 2)

 

geom_histogram()这个几何对象默认使用stat_bin这个统计变换, 而这个统计变换会生成(1)count:每个组里观测值的数目, (2)density:每个组里观测值的密度和(3)x:组的中心位置这三个变量。生成的变量在ggplot()中的再使用..围起来, 因此可以用来生成如下的图

1

2

m + geom_histogram(bin = 0.5, aes(fill =..count..))

m + geom_histogram(bin = 0.5, aes(y = ..density..)) + geom_density()

元素位置的调整共有5种包括了(1)dodge:并排方式; (2)fill:堆叠图像元素, 并将高度标准化为1,(3)identity:不做任何调整; (4)jitter:给点增加扰动避免重合和(5)stack:堆叠图像元素。 

1

2

3

4

5

d <- ggplot(diamonds, aes(x = clarity, fill = cut ))

d + geom_histogram(position = "dodge")

d + geom_histogram(position = "fill")

d + geom_histogram(position = "stack")

ggplot(diamonds) + geom_point(aes(color, price/carat), position = "jitter")

jitter使某每一个点在x轴的方向上产生随机的偏移, 从而减少了图形重叠的问题, 另一种介绍重叠的方式是改变点的透明度, 将在实战中的地图讨论。


很多情况下, 我们会采用固定的x轴和y轴值来进行作图, 此时需要用stat = “identity” 来申明, 即表示不对数据进行统计变换

1

2

3

A <- c(1, 2, 3, 4, 5, 6, 7, 8)

B <- c(2, 10, 11, 5, 6, 1, 10, 20)

ggplot() + geom_histogram(aes(x = A, y = B), stat = "identity")

 

3) geom_smooth()
    geom_smooth()用来给数据添加平滑曲线, 所能采用的方法包括了lm, glm, gam, loess, rlm等, 这些方法需要通过加载公式来实现。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

m <- ggplot(mtcars, aes(qsec, wt))

m + stat_smooth() + geom_point()

m + stat_smooth(se = FALSE) + geom_point()

 #取消默认的置信区间

m + stat_smooth(fill = "red", size = 2, alpha = 0.5, color = "green") + geom_point()

#更改置信区间和线条颜色

m + stat_smooth(method = "lm") + geom_point()

#用一元一次线性方程拟合

m + stat_smooth(method = "lm", formula = y ~ poly(x, 3)) + geom_point()

#使用一元二次方程拟合

require(splines)

require(MASS)

m + stat_smooth(method = "lm", formula = y ~ ns(x, 3)) + geom_point()

# 加载splines和MASS包, 使用*度为3的自然样条来进行拟合

m <- ggplot(mtcars, aes(y = wt, x = mpg, group = factor(cyl)))

m + stat_smooth(method = lm, aes(color = factor(cyl), fill = factor(cyl))) + geom_point( aes(color = factor(cyl)))

#按cyl这个离散变量进行分组, 分别拟合数据

4.分面(facet)
即在一个页面上自动摆放多幅图形, 这一过程先将数据划分为多个子集, 然后将每个子集依次绘制到页面的不同面板中。ggplot2提供两种分面类型:网格型(facet_grid)封面型(facet_wrap)网格分面生成的是一个2维的面板网格, 面板的行与列通过变量来定义, 本质是2维的; 封装分面则先生成一个1维的面板条块, 然后再分装到2维中, 本质是1维的。
在很多情况下, 我们可能需要绘制有两个y轴的坐标系, 而在ggplot2中, 这种做法特别不提倡(stackover的讨论), 可解决的方法要么是把变量归一化, 要么便是采用分面方法

1

2

3

4

5

6

7

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()

p + facet_grid(. ~ cyl) #以cyl为分类变量

p + facet_wrap( ~ cyl, nrow = 3) #wrap与grid的区别

p + facet_grid(cyl ~ .) #以cyl为分类变量

p + facet_wrap( ~ cyl, ncol = 3) #wrap与grid的区别

p + facet_grid(vs ~ am) #以vs和am为分类变量

p + facet_wrap(vs ~ am, ncol = 2) #wrap与grid 的区别

 

 

1

2

3

4

5

p <- p + geom_smooth(method = "lm", se =F, aes(color = factor(cyl))) + geom_point(aes(color = factor(cyl)))

p + facet_grid(vs ~ am)

p + facet_grid(vs ~ am, margins = T) #使用margins来描述边际图

p + facet_grid( ~ cyl, scales = "free")

p + facet_grid( ~ cyl, scales = "free_x")

 

1

2

3

4

5

6

7

p <- ggplot(aes(cty, hwy), data = mpg) + geom_point()

p + facet_wrap( ~ cyl)

#调整scales的标度, 共有fixed, free, free_x和free_y四种变换

p + facet_wrap( ~ cyl, scales = "free") # 这里标度更改为free

p + facet_grid(. ~ cyl, scales = "free", space = "free")

#space设置为free时, 每列的宽度与该列的标度范围成比例

##使用*标度来替代<strong>双坐标轴</strong>的实战的一个例子中

 

5.主题(theme)
    主题系统控制着图形中的非数据元素外观, 它不会影响几何对象和标度等数据元素。主题修改是一个对绘图精雕细琢的过程, 主要对标题、坐标轴标签、图例标签文字调整, 以及网格线、背景、轴须的颜色搭配。

1

2

3

p <- ggplot(movies,  aes(x = rating)) + geom_histogram(bin = 1)

p + theme_bw() #白色背景

p + theme_grey() #默认浅灰色背景

主题由控制图形外观的多个元素组成, 详见官方索引

1

2

3

4

5

##element_text()修改标签和标题

p <- p + labs(title = "histogram")

p + theme(plot.title = element_text(size = 20, color = "red",

                                    hjust = 0, face = "bold",

                                    angle = 180))

 

内置元素共有四个基础类型:文本(text), 线条(line)、矩形(rectangle)、空白(blank), text与其他类型操作相类似, 具体的例子可参考索引, 此处用element_blank()来去除灰色背景。

1

p + theme(panel.background = element_blank()) #blank是去掉某种绘图元素

输出(ggsave)
    ggsave()是ggplot2种特有的输出函数, 是一种极为方便的出图方式

1

2

3

p <- ggplot(mtcars, aes(x = mpg, y = disp)) + geom_point()

ggsave( file = "mtcars_plot.png", width = 5, height = 6, type = "cairo", dpi = 600)

#cairo为抗锯齿包, ggplot默认输出即为cairo处理

ggplot2支持eps矢量图输出, 其他可以支持的格式包括png, jpg, pdf等, 并通过ggsave可以方便的进行修改。