"pythonic生物人"的第106篇分享
本文详细介绍ggplot2中图例标度(legends scales),续前篇
- R可视化07|ggplot2图层-标度图层(scale layer)-颜色盘篇
本文目录
4、图例标度(legends scale)
图例位置设置
修改ggplot2的图例符号
ggplot2的图例顺序|方向等花里胡哨设置
4、图例标度(legends scale)
图例位置设置
图例位置通过theme中的legend.position
设置,有参数“right”, “left”, “top”, “bottom”, or “none” (不显示图例)可选。
toy const = 1,
up = 1:4,
txt = letters[1:4],
big = (1:4)*1000,
log = c(2, 5, 10, 2000)
)
base geom_point(aes(colour = txt), size = 3) +
xlab(NULL) +
ylab(NULL)
#legend.position控制图例上下左右位置
p1 "left")
p2 "right") # 默认图例在左边
p3 "bottom")
p4 "none")#不显示图例
#legend.position和legend.justification设置图例占比图形比例控制图例位置
base geom_point(aes(colour = txt), size = 3)
p5 p6 0, 1), legend.justification = c(0, 1))
p7 0.5, 0.5), legend.justification = c(0.5, 0.5))
p8 1, 0), legend.justification = c(1, 0))
p9 4)
ggsave("scale8.png", p9, width = 8, height = 10)
修改ggplot2的图例符号
draw_key_*
函数指定图例符号,注意右侧图例符号变化。
options(repr.plot.width = 10, repr.plot.height = 12, repr.plot.res = 300)
p1 geom_line()
#draw_key_timeseries指定图例符号为时间序列
p2 geom_line(key_glyph = draw_key_timeseries)
#自定义图例符号为笑脸
library(grid)
library(rlang)
draw_key_smile function(data, params, size) {
grobTree(
pointsGrob(0.25, 0.75, size = unit(.25, "npc"), pch = 16),
pointsGrob(0.75, 0.75, size = unit(.25, "npc"), pch = 16),
linesGrob(c(0.9, 0.87, 0.78, 0.65, 0.5, 0.35, 0.22, 0.13, 0.1),
c(0.5, 0.35, 0.22, 0.13, 0.1, 0.13, 0.22, 0.35, 0.5)),
gp = gpar(
col = data$colour %||% "grey20",
fill = alpha(data$fill %||% "white", data$alpha),
lwd = (data$size %||% 0.5) * .pt,
lty = data$linetype %||% 1
)
)
}
p3 geom_line(key_glyph = draw_key_smile)
p4 3)
ggsave("scale9.png", p4, width = 10, height = 12)
其它draw_key_*图例符号
ggplot2的图例顺序|方向等花里胡哨设置
这部分详细介绍图例各种个性化设置,看图找代码即可。
options(repr.plot.width = 5, repr.plot.height = 15, repr.plot.res = 300)
base
p1
#ncol byrow设置图例排列顺序
p2 2))
p3 2, byrow = TRUE))
#reverse颠倒图例顺序
p4 TRUE))
#override.aes:覆盖从每个图层派生的一些图形属性设置
base1 geom_point(size = 4, alpha = .2, stroke = 0)
p5 p6 1)))
#axis图例处坐标轴控制
base2 geom_point(alpha = .2) +
scale_size_binned()
p7 p8 FALSE))
#direction设置图例方向
p9 "vertical"))
p10 "horizontal"))
#show.limits,axis.colour, axis.linewidth和axis.arrow控制图例处axis属性
base3 geom_point(size = 2)
#guide_colourbar() / guide_colorbar()控制colourbar型图例
p13 p14 TRUE))
#设置colourbar高度2cm
p15 2, "cm")))
#guide_coloursteps() / guide_colorsteps()控制刻度显示形式
base4 geom_point() +
scale_color_binned()
p16 p17 TRUE))#colourbar首尾显示
p18 FALSE))
p19 8)
ggsave("scale10.png", p19, width = 5, height = 15)
本文结束,更多好文:
- Python可视化|Matplotlib39-Matplotlib 1.4W+字教程(珍藏版)
- Python可视化|Matplotlib&Seaborn36(完结篇)
- python3基础12详解模块和包(库)|构建|使用
- Perl基础系列合集
- NGS各种组学建库原理(图解)
有用请“点赞”“在看”“分享”
有意见请移步到QQ群629562529反馈,一起进步哈!