ggplot2绘图R语言初学

【R>>ggh4x】ggplot2分面hacks

2021-07-02  本文已影响0人  高大石头

ggh4x是ggplot2的扩展包,其火星文发音为G-G-hacks。主要功能包括调整分面大小(adjusting the sizes of facets),mapping multiple aesthetics to colors and specifying individual scales for facets。同时还是多个geoms、facets、positions、guides和stats的集合。

安装

#devtools::install_github("teunbrand/ggh4x")

小试牛刀

library(ggh4x)
library(tidyverse)

# 创建示例数据
n <- 200
df <- data.frame(
  x = c(rpois(n, 25),
        rnbinom(n, 5, 0.2),
        rgamma(n, 30, 1.5),
        rchisq(n, 15)),
  distribution = rep(c("Poisson", "Negative Binomial", 
                       "Gamma", "Chi-squared"), each = n),
  type = rep(c("Discrete", "Continuous"), each = 2 * n)
)
head(df)
##    x distribution     type
## 1 24      Poisson Discrete
## 2 27      Poisson Discrete
## 3 26      Poisson Discrete
## 4 23      Poisson Discrete
## 5 33      Poisson Discrete
## 6 14      Poisson Discrete
ggplot(df, aes(x, y = after_stat(count),
               fill = distribution, colour = distribution)) +
  geom_histogram(position = "identity", binwidth = 1, 
                 alpha = 0.3, colour = NA) +
  # One type of theoretical densities for discrete distributions with steps
  stat_theodensity(data = ggsubset(type == "Discrete"),
                   distri = "nbinom", geom = "step",
                   position = position_nudge(x = -0.5)) +
  # Another type for the continuous ones with lines
  stat_theodensity(data = ggsubset(type == "Continuous"),
                   distri = "gamma") +
  scale_colour_discrete(aesthetics = c("colour", "fill"), guide = "none") +
  # Have the facet strips span categories
  facet_nested(~ type + distribution, scales = "free_x") +
  # Precisely control aspect ratio of panels
  force_panelsizes(rows = 1.618, cols = 1, respect = TRUE) 

相关参数

Facets

主要针对常见的wrapgrid布局进行自定义。

接下来这两个参数是Facet helpers函数:

Position guides and scales

添加x轴和y轴的展示细节

Colour scales and guide

两种映射非标准数据的方法

Stats

数据变换后,对不同图层进行统计。

Geoms

几何图层的集合

Positions

可添加到图层的位置调整

Miscellaneous

杂项

特色展示

给每个类别分别赋值进行展示

df <- transform(
  iris,
  Nester=ifelse(Species=="setosa","Short Leaves","Long Leaves")
)

g <- ggplot(df,aes(Sepal.Width,Sepal.Length))+
  theme_classic()+
  theme(strip.background = element_blank())
g <- g + 
  geom_point(aes(SW = Sepal.Width),
             data = ~ subset(., Species == "setosa")) +
  geom_point(aes(PL = Petal.Length),
             data = ~ subset(., Species == "versicolor")) +
  geom_point(aes(PW = Petal.Width),
             data = ~ subset(., Species == "virginica"))
g
library(scales)
g <- g +
  scale_colour_multi(
    aesthetics = c("SW", "PL", "PW"),
    name = list("Blue", "Pink", "Orange"),
    colours = list(
      brewer_pal(palette = "YlGnBu")(6),
      brewer_pal(palette = "RdPu")(6),
      brewer_pal(palette = "YlOrRd")(6)
    ),
    guide = guide_colorbar(barheight = unit(50, "pt"))
  )
g

还可以将重复的分面类型进行合并

g <- g+facet_nested(~Nester+Species,scales = "free",
                    nest_line = TRUE)
g

其实还能给将坐标轴个性化设计

position_scales <- list(
  scale_x_reverse(guide="axis_minor"),
  scale_x_continuous(labels = dollar,guide = "axis_truncated"),
  scale_x_continuous(breaks = c(3,4),expand = c(0,0))
) #对x轴的3端分别自定义
g <- g+facetted_pos_scales(x=position_scales)
g

再仔细看,第3个分面比较大,当然也可以自定义的。

size <- 2/(1+sqrt(5))
g <- g+force_panelsizes(cols = c(1,size,size^2),respect = T)
g

备注

其实ggh4x算是对facets,stat,geoms等方面的调整的一个集合,类似的功能可以在其他包中找到,比如:

当然ggh4x大部分功能来自ggnomics包,但跟其还是有很大区别的。

参考链接:

ggh4x官方文档(由于简书限制外链,就不放具体链接了,github上搜ggh4x就能找到。)

上一篇下一篇

猜你喜欢

热点阅读