R視覺化09|ggplot2-圖層圖形語法 (1)

pythonic生物人發表於2020-10-14

前面介紹了ggplot2的圖層(layers),接下來幾篇會系統較深入的介紹ggplot2圖層圖形語法(the Grammar of Graphics),不糾結某一種圖的具體繪製方法,不糾結某一個引數的具體設定,這些都交給幫助文件。

本文目錄

1、ggplot2圖層圖形語法(the Grammar of Graphics)的益處

2、圖層圖形語法(the Grammar of Graphics)中一張圖組成

圖層(layer)介紹

ggplot2繪圖模板

3、資料集(Data)

4、影像屬性(aes)


1、ggplot2圖層圖形語法(the Grammar of Graphics)的益處

  • 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、圖層圖形語法(the Grammar of Graphics)中一張圖組成

  • 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)

 


本文結束,更多好文,歡迎關注:pythonic生物人

相關文章