ggh4x 包的坐标轴调整技能!
人生就是一个笑话
1引言
ggh4x 除了对 ggplot 的 分面有强大的支持和扩展外 ,对于 ggplot 的 坐标轴调整 也有很多功能, 非常实用! 欢迎一起学习。
2安装
安装加载 R 包:
install.packages('ggh4x')
library(ggh4x)
library(ggplot2)
3position guides
使用 guide_axis 和 guide_none 调整 xy 轴样式:
g <- ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
theme(axis.line = element_line(colour = "black"))
g + guides(
x = guide_none(title = "x"),
x.sec = guide_axis(title = "x.sec"),
y = "none",
y.sec = "axis"
)
其它写法:
g + scale_x_continuous(
position = "bottom",
guide = guide_axis(position = "top")
)
添加双坐标轴:
g + scale_x_continuous(
sec.axis = dup_axis(breaks = 2:4 + 0.5,
guide = guide_axis(angle = 45))
)
4Coloured axis
给坐标轴添加颜色的不同方式,最后的更加简单:
# Setting all theme elements is a mild inconvenience.
g + theme(
axis.line.x = element_line(colour = "forestgreen"),
axis.ticks.x = element_line(colour = "forestgreen"),
axis.text.x = element_text(colour = "forestgreen")
)
# A little bit easier
g + guides(x = guide_axis_colour(colour = "forestgreen"))
给第二条坐标轴改颜色:
ggplot(economics, aes(date)) +
geom_line(aes(y = unemploy)) +
geom_line(aes(y = pop / 30), colour = "red") +
scale_y_continuous(
sec.axis = sec_axis(
~ .x * 30, name = "pop",
guide = guide_axis_colour(colour = "red"))
) +
theme(axis.line.y = element_line())
5Truncated axes
支持截断型坐标轴:
g + guides(x = "axis_truncated")
支持随意设置截断范围:
# Using grid units to specify data-independent truncation points.
g + guides(x = guide_axis_truncated(trunc_lower = unit(0.1, "npc"),
trunc_upper = unit(0.9, "npc")))
# Using atomic vectors are interpreted as data points that should be mapped.
g + guides(x = guide_axis_truncated(trunc_lower = 2.5,
trunc_upper = 4.5))
使用隐式变量来传递,达到分段坐标轴样式:
g + guides(x = guide_axis_truncated(trunc_lower = c(2, 4),
trunc_upper = c(3, 5)),
y = guide_axis_truncated(trunc_lower = ~ .x - 1,
trunc_upper = ~ .x + 1))
还可以达到上下分段坐标轴的效果:
df <- data.frame(x = seq(-3, 3, length.out = 6), y = LETTERS[1:6])
ggplot(df, aes(x, y)) +
geom_col() +
scale_x_continuous(
breaks = -3:0, guide = "axis_truncated",
sec.axis = dup_axis(
breaks = 0:3, guide = "axis_truncated"
)
) +
theme(axis.line.x = element_line())
6Manual axes
支持更自由的自定义坐标轴样式:
g + guides(y.sec = guide_axis_manual(
breaks = unit(c(1, 3), "cm"),
labels = expression("treshold"^2, "annotation"[3]),
label_colour = c("red", "blue"), label_size = c(8, 12)
))
这样可以在第二坐标轴进行手动注释:
tab <- table(diamonds$cut)
ggplot(diamonds, aes(cut, price)) +
geom_violin() +
guides(x.sec = guide_axis_manual(
breaks = names(tab),
labels = paste0("n = ", tab)
))
此外在绘制热图的时候,比如标注几个目的基因标签:
highlight <- c("New York", "California", "Alabama", "Hawaii")
clust <- hclust(dist(USArrests))
# Melting USArrests
df <- data.frame(
state = rownames(USArrests)[as.vector(row(USArrests))],
crime = colnames(USArrests)[as.vector(col(USArrests))],
value = as.vector(as.matrix(USArrests)),
row.names = NULL
)
ggplot(df, aes(crime, state, fill = value)) +
geom_raster() +
scale_y_dendrogram(hclust = clust, labels = NULL) +
guides(y.sec = guide_axis_manual(breaks = highlight, labels = highlight))
7Minor ticks
给主刻度旁添加次要刻度线:
g + guides(x = "axis_minor", y = "axis_minor")
在指定范围添加次要刻度线:
g + scale_x_continuous(
minor_breaks = seq(2, 4, by = 0.2),
guide = "axis_minor"
)
改变次要刻度线的长度:
g + guides(x = "axis_minor") +
theme(axis.ticks.length.x = unit(0.5, "cm"),
ggh4x.axis.ticks.length.minor = rel(0.5))
8Logarithmic ticks
添加对数刻度,非常 nice!:
pres <- ggplot(pressure, aes(temperature, pressure)) +
geom_line()
pres + scale_y_log10(guide = "axis_logticks") +
theme(axis.ticks.length.y = unit(0.5, "cm"),
ggh4x.axis.ticks.length.minor = rel(0.5),
ggh4x.axis.ticks.length.mini = rel(0.2))
下面方式不一样,达到的效果一样:
# Using annotation log-ticks
pres + scale_y_log10() +
annotation_logticks(sides = 'l', outside = TRUE) +
coord_cartesian(clip = "off") +
theme(axis.text.y = element_text(margin = margin(r = 10)))
# Using axis_logticks, setting tick length equivalently
pres + scale_y_log10(guide = "axis_logticks") +
theme(axis.ticks.length.y = unit(0.3, "cm"))
把刻度朝里:
pres + scale_y_log10(guide = "axis_logticks") +
theme(axis.ticks.length.y = unit(-0.3, "cm"),
axis.text.y = element_text(margin = margin(r = 10)))
9Nested relations
使用嵌套方式来给变量进行分类:
df <- data.frame(
item = c("Coffee", "Tea", "Apple", "Pear", "Car"),
type = c("Drink", "Drink", "Fruit", "Fruit", ""),
amount = c(5, 1, 2, 3, 1),
stringsAsFactors = FALSE
)
ggplot(df, aes(interaction(item, type), amount)) +
geom_col() +
guides(x = "axis_nested")
使用 paste 函数来匹配,用 nonsense 分割,然后注释:
ggplot(df, aes(paste0(item, "~nonsense~", type), amount)) +
geom_col() +
guides(x = guide_axis_nested(delim = "nonsense"))
weave_factors 函数可以保持顺序一一对应:
ggplot(df, aes(weave_factors(item, type), amount)) +
geom_col() +
guides(x = "axis_nested")
改变注释线条及文字样式:
ggplot(df, aes(weave_factors(item, type), amount)) +
geom_col() +
guides(x = "axis_nested") +
theme(
axis.ticks = element_line(colour = "red"),
ggh4x.axis.nestline.x = element_line(size = 2),
ggh4x.axis.nesttext.x = element_text(colour = "blue")
)
可以展示多层嵌套关系:
df$type2 <- c(rep("Consumables", 4), "Vehicle")
df$appletea <- c("", rep("Ingredient of apple tea", 2), rep(NA, 2))
ggplot(df, aes(weave_factors(item, type, appletea, type2), amount)) +
geom_col() +
guides(x = "axis_nested")
10Dendrograms
scale_y_dendrogram 函数可以给热图添加聚类树:
clusters <- hclust(dist(USArrests), "ave")
# reshaping USArrests
df <- data.frame(
State = rownames(USArrests)[row(USArrests)],
variable = colnames(USArrests)[col(USArrests)],
value = unname(do.call(c, USArrests))
)
g <- ggplot(df, aes(variable, State, fill = value)) +
geom_raster()
g + scale_y_dendrogram(hclust = clusters)
使用对应的 y 主题调整参数可以调整树状图:
g + scale_y_dendrogram(hclust = clusters) +
theme(
axis.ticks.y = element_line(size = 2, lineend = "round"),
axis.ticks.length.y = unit(10, "pt")
)
guide_dendro 函数可以调整位置:
g + scale_y_dendrogram(guide = guide_dendro(position = "right"),
hclust = clusters)
11梦想
我有一个梦想, 所以只能做梦的时候想想, 但我需要梦想, 这样我才能在失败中站起来继续前进。
欢迎加入生信交流群。加我微信我也拉你进 微信群聊 老俊俊生信交流群
哦,数据代码已上传至QQ群,欢迎加入下载。
群二维码:
老俊俊微信:
知识星球:
所以今天你学习了吗?
欢迎小伙伴留言评论!
今天的分享就到这里了,敬请期待下一篇!
最后欢迎大家分享转发,您的点赞是对我的鼓励和肯定!
如果觉得对您帮助很大,赏杯快乐水喝喝吧!
往期回顾
◀跟着 NC (Nature Comm) 学画图: 箱线图嵌套并分面添加文本注释
◀跟着 NC (Nature Communications) 学画图: 火山图进阶
◀...