查看原文
其他

R可视化09|ggplot2-图层图形语法 (1)

pythonic生物人 pythonic生物人 2022-10-25

"pythonic生物人"的第107篇分享

前面介绍了ggplot2的图层(layers),接下来几篇会系统较深入的介绍ggplot2图层图形语法(the Grammar of Graphics),不纠结某一种图的具体绘制方法,不纠结某一个参数的具体设置,这些都交给帮助文档,只求理解图背后的原理,一通百通。

本文目录

1、ggplot2图层图形语法益处

2、图层图形语法一张图组成
      图层(layer)介绍
      ggplot2绘图模板

3、数据集(Data)

4、图像属性(aes)


1、ggplot2图层图形语法益处

  • The layered grammar is based on Wilkinson’s grammar of graphics , but adds a number of enhancements  that help it to be more expressive and fit seamlessly into the R environment.
  • It also encourages the use of graphics customised to a particular problem, rather than relying on specific chart types.
  • The grammar makes it easier for you to iteratively update a plot, changing a single feature at a time.
  • The grammar is also useful because it suggests the high-level aspects of a plot that can be changed, giving you a framework to think about graphics, and hopefully shortening the distance from mind to paper.

2、图层图形语法一张图组成

  • A default dataset and set of mappings from variables to aesthetics【数据集(data)和图像属性(aes)】.
  • One or more layers, each composed of a geometric object, a statistical transformation, a position adjustment, and optionally, a dataset and aesthetic mappings【一或多个图层(layers):每个图层由数据集(data),图像属性(aes),统计变换(stat),几何对象(geom)和位置调整(position adjustment)组成】.
  • One scale for each aesthetic mapping【标度(scale)】.
  • A coordinate system【坐标系(coord)】.
  • The facetting specification【分面(facet)】.

以上各部分体现在图中如下。

图层(layer)介绍

图层一般由以下五部分构成

> layer
function (
geom = NULL#绘制什么图,例如散点图:geom = "point",
stat = NULL#默认为“identity”, histograms和smoothers图时常用
data = NULL#指定数据集
mapping = NULL#aes()函数
position = NULL#设置多个图之间相对位置,防折叠、设置堆叠等
..........
          )
{...............
}

了解layer函数会帮助更好理解图层图形语法的底层,不过一般绘图时,不会直接使用layer函数,而是使用阉割版的geom_系列函数添加图层,举个栗子。

library('gridExtra')
library('ggplot2')
options(repr.plot.width = 6, repr.plot.height = 3, repr.plot.res = 300)
#创建一个画布,包含坐标轴和数据信息
p1 <- ggplot(mpg, aes(displ, hwy))


#geom_系列函数添加散点图(geom_point()图层
p2 <- p1 + geom_point()+ggtitle('By geom_point()')

#geom_point()是捷径,实际上后台调用layer()函数以创建一个散点图图层
p3 <- p1 + layer(
  mapping = NULL
  data = NULL,
  geom = "point"#指定散点图
  stat = "identity",
  position = "identity"
)+ggtitle('By layer()')

p4 <- grid.arrange(p2,p3,nrow = 1)
ggsave("scale11.png", p4, width = 6, height = 3)

可以看出效果一模一样。

ggplot2绘图模板

3、数据集(Data)

ggplot2中对干净数据定义为,variables in the columns and observations in the rows【每一列为一个变量,每一行为一个观测】, 可以通过控制数据集以突出想要突出展示的数据。

library(dplyr) 
options(repr.plot.width = 4.5, repr.plot.height = 3.5, repr.plot.res = 300)
mod <- loess(hwy ~ displ, data = mpg)
grid <- data_frame(displ = seq(min(mpg$displ), max(mpg$displ), length = 50))
grid$hwy <- predict(mod, newdata = grid)#loess变换构建新data grid

std_resid <- resid(mod) / mod$s
outlier <- filter(mpg, abs(std_resid) > 2)#划分离散点


ggplot(mpg, aes(displ, hwy)) + 
  geom_point() + #添加散点图图形
  geom_line(data = grid) + #拟合曲线
  geom_text(data = outlier, aes(label = model))#离散点添加文本

4、图像属性(aes

设置Colour, size, shape等等。

  • 在ggplot和在图层中设置aes的区别
library(dplyr)
class <- mpg %>% 
  group_by(class) %>% 
  summarise(n = n(), hwy = mean(hwy))

#以下四种方式按class绘制散点图效果一致。
#在ggplot中设置aes
ggplot(mpg, aes(displ, hwy, colour = class)) + 
  geom_point()

ggplot(mpg, aes(displ, hwy)) + 
  geom_point(aes(colour = class))

#在图层中设置aes
ggplot(mpg, aes(displ)) + 
  geom_point(aes(y = hwy, colour = class))

ggplot(mpg) + 
  geom_point(aes(displ, hwy, colour = class))

  • **aes内和外设置参数的区别 **
options(repr.plot.width = 6, repr.plot.height = 3.5, repr.plot.res = 300)

p1 <- ggplot(mpg, aes(cty, hwy)) + 
  geom_point(colour = "darkblue")  
#if you want override the default size or colour, put the value outside of aes().

p2 <- ggplot(mpg, aes(cty, hwy)) + 
  geom_point(aes(colour = "darkblue"))
#  If you want appearance to be governed by a variable, put the specification inside aes()

p3 <- ggplot(mpg, aes(cty, hwy)) + 
  geom_point(aes(colour = "darkblue")) + 
  scale_colour_identity()

p4 <- ggplot(mpg, aes(displ, hwy)) + 
  geom_point() +
  geom_smooth(aes(colour = "loess"), method = "loess", se = FALSE) + 
  geom_smooth(aes(colour = "lm"), method = "lm", se = FALSE) +
  labs(colour = "Method")

p5 <- grid.arrange(p1,p2,p3,p4,nrow = 2)
ggsave("scale11.png", p4, width = 6, height = 3)


未完待续,更多好文:

有用请“点赞”“在看”“分享”

有意见请移步到QQ群629562529反馈,一起进步哈!

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存