Closed ixxmu closed 7 months ago
ggh4x 是一个非常优秀且功能丰富的 R 包,对 ggplot 拓展了不少实用的功能。这里介绍一些也许会用到的,具体使用见官方使用手册。
映射多个颜色标度:
library(ggh4x)
# Separating layers by species and declaring (yet) unknown aesthetics
g <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
geom_point(aes(swidth = Sepal.Width),
data = ~ subset(., Species == "setosa")) +
geom_point(aes(pleng = Petal.Length),
data = ~ subset(., Species == "versicolor")) +
geom_point(aes(pwidth = Petal.Width),
data = ~ subset(., Species == "virginica")) +
facet_wrap(~ Species, scales = "free_x")
#> Warning in geom_point(aes(swidth = Sepal.Width), data = ~subset(., Species == :
#> Ignoring unknown aesthetics: swidth
#> Warning in geom_point(aes(pleng = Petal.Length), data = ~subset(., Species == :
#> Ignoring unknown aesthetics: pleng
#> Warning in geom_point(aes(pwidth = Petal.Width), data = ~subset(., Species == :
#> Ignoring unknown aesthetics: pwidth
# This generated quite some warnings, but this is no reason to worry!
g + scale_colour_multi(
aesthetics = c("swidth", "pleng", "pwidth"),
# Providing colours as a list distributes list-elements over different scales
colours = list(c("black", "green"),
c("gray", "red"),
c("white", "blue")),
guide = list(guide_colourbar(barheight = unit(35, "pt")))
)
把图例换成文字:
p1 <- ggplot(diamonds, aes(price, carat, colour = clarity)) +
geom_point() +
scale_colour_brewer(palette = "Dark2")
p2 <- ggplot(diamonds, aes(price, carat, colour = clarity)) +
geom_point(shape = ".") +
scale_colour_brewer(palette = "Dark2", guide = "stringlegend")
p1 + p2
调整细节:
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(colour = class))
p2 <- p + guides(colour = guide_stringlegend(face = "bold", spacing = 15))
p3 <- p + guides(colour = guide_stringlegend(spacing.x = 0, spacing.y = 5,
family = "mono", ncol = 2))
p2 + p3
给点之间添加线段:
set.seed(0)
df <- data.frame(
x = 1:10,
y = cumsum(rnorm(10))
)
p <- ggplot(pressure, aes(temperature, pressure)) +
geom_pointpath()
p + theme(aspect.ratio = 0.5)
p + theme(aspect.ratio = 2)
内部坐标轴:
p <- ggplot(mpg, aes(displ - mean(displ), hwy - mean(hwy))) +
geom_point() +
theme(axis.line = element_line())
p + coord_axes_inside()
在内部显示刻度:
p + coord_axes_inside(labels_inside = TRUE) +
scale_x_continuous(
labels = ~ ifelse(.x == 0, "", .x),
guide = "axis_minor"
) +
scale_y_continuous(
labels = ~ ifelse(.x == 0, "", .x),
guide = "axis_truncated"
)
这个你也可以看看 geom_cross_axis 绘制交叉坐标轴。
文字旋转:
ggplot(mpg, aes(factor(1), fill = class)) +
geom_bar(show.legend = FALSE, position = "fill") +
geom_text_aimed(aes(x = 1.2, label = class, group = class),
position = position_fill(vjust = 0.5),
stat = "count") +
coord_polar("y") +
theme_void()
ggplot(diamonds, aes(cut, fill = clarity)) +
geom_bar(width = 1) +
geom_text_aimed(aes(label = cut, group = cut),
angle = 90,
stat = "count", nudge_y = 2000) +
scale_x_discrete(labels = NULL) +
coord_polar()
#> Warning: The following aesthetics were dropped during statistical transformation: fill
#> ℹ This can happen when ggplot fails to infer the correct grouping structure in
#> the data.
#> ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
#> variable into a factor?
路漫漫其修远兮,吾将上下而求索。
欢迎加入生信交流群。加我微信我也拉你进 微信群聊 老俊俊生信交流群 (微信交流群需收取 20 元入群费用,一旦交费,拒不退还!(防止骗子和便于管理)) 。QQ 群可免费加入, 记得进群按格式修改备注哦。
老俊俊微信:
知识星球:
听说你想插入 GO 和 KEGG 图形注释? ClusterGVis 添加自定义图形注释 听说你想绘制 scanpy 的 tracksPlot? 重复序列元件定量分析 如何下载重复序列元件的注释信息 更换 Rstudio 图标 蛋白亚定位工具集合 aPEAR 优雅的可视化通路富集结果 BSgenomeForge 一行代码轻松构建 BSgenome package 听说你想画个 monocle3 的拟时序热图?
https://mp.weixin.qq.com/s/oSX4Xg5QbL2wnE-YZf7HMQ