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
主要针对常见的wrap
和grid
布局进行自定义。
facet_grid2():Extended grid facets
facet_wrap2():Extended wrapped facets
facet_nested():Layout panels in a grid with nested strips
facet_nested_wrap():Ribbon of panels with nested strips.
接下来这两个参数是Facet helpers函数:
force_panelsizes()
facetted_pos_scales()
Position guides and scales
添加x轴和y轴的展示细节
scale_x/y_dendrogram() :Dendrogram position scales
guide_dendro():Dendrogram guide
guide_axis_truncated() guide_axis_colour() guide_axis_color(): Axis guide with truncated line
guide_axis_minor(): Axis guide with ticks for minor breaks
guide_axis_logticks(): Axis guide with ticks for logarithmic breaks
guide_axis_nested(): Nested axis guide
Colour scales and guide
两种映射非标准数据的方法
guide_stringlegend():String legend
scale_fill_multi() scale_colour_multi():Multiple gradient colour scales
scale_listed():Add a list of scales for non-standard aesthetics
Stats
数据变换后,对不同图层进行统计。
stat_theodensity():Fitted theoretical density
stat_difference():Difference ribbon
stat_rollingkernel():Rolling Kernel
stat_funxy() stat_centroid() stat_midpoint():Apply function to position coordinates
stat_rle():Run length encoding
Geoms
几何图层的集合
geom_pointpath():Point Paths
geom_polygonraster():Polygon parameterisation for rasters
geom_rectmargin() geom_tilemargin():Rectangular rugs in the margins
geom_text_aimed() :Aimed text
Positions
可添加到图层的位置调整
position_disjoint_ranges():Segregrating overlapping ranges
position_lineartrans():Linearly transform coordinates
Miscellaneous
杂项
help_secondary():Secondary axis helper
center_limits():Center limits
特色展示
给每个类别分别赋值进行展示
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等方面的调整的一个集合,类似的功能可以在其他包中找到,比如:
facetscales:调整row/column的分面
egg:设置分面大小
lemon:调整坐标轴位置
relayer和ggnewscale:对同一图形映射多个颜色
当然ggh4x大部分功能来自ggnomics包,但跟其还是有很大区别的。
参考链接:
ggh4x官方文档(由于限制外链,就不放具体链接了,github上搜ggh4x就能找到。)