其他
R可视化08|ggplot2图层-标度图层(scale layer)-图例篇
"pythonic生物人"的第106篇分享
本文详细介绍ggplot2中图例标度(legends scales),续前篇
本文目录
4、图例标度(legends scale)
图例位置设置
修改ggplot2的图例符号
ggplot2的图例顺序|方向等花里胡哨设置
4、图例标度(legends scale)
图例位置设置
图例位置通过theme中的legend.position
设置,有参数“right”, “left”, “top”, “bottom”, or “none” (不显示图例)可选。
toy <- data.frame(
const = 1,
up = 1:4,
txt = letters[1:4],
big = (1:4)*1000,
log = c(2, 5, 10, 2000)
)
base <- ggplot(toy, aes(up, up)) +
geom_point(aes(colour = txt), size = 3) +
xlab(NULL) +
ylab(NULL)
#legend.position控制图例上下左右位置
p1 <- base + theme(legend.position = "left")
p2 <- base + theme(legend.position = "right") # 默认图例在左边
p3 <- base + theme(legend.position = "bottom")
p4 <- base + theme(legend.position = "none")#不显示图例
#legend.position和legend.justification设置图例占比图形比例控制图例位置
base <- ggplot(toy, aes(up, up)) +
geom_point(aes(colour = txt), size = 3)
p5 <- base
p6 <- base + theme(legend.position = c(0, 1), legend.justification = c(0, 1))
p7 <- base + theme(legend.position = c(0.5, 0.5), legend.justification = c(0.5, 0.5))
p8 <- base + theme(legend.position = c(1, 0), legend.justification = c(1, 0))
p9 <- grid.arrange(p1,p2,p3,p4,p5,p6,p7,p8,nrow = 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 <- ggplot(economics_long, aes(date, value01, colour = variable)) +
geom_line()
#draw_key_timeseries指定图例符号为时间序列
p2 <- ggplot(economics_long, aes(date, value01, colour = variable)) +
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 <- ggplot(economics_long, aes(date, value01, colour = variable)) +
geom_line(key_glyph = draw_key_smile)
p4 <- grid.arrange(p1,p2,p3,nrow = 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 <- ggplot(mpg, aes(drv, fill = factor(cyl))) + geom_bar()
p1 <- base
#ncol byrow设置图例排列顺序
p2 <- base + guides(fill = guide_legend(ncol = 2))
p3 <- base + guides(fill = guide_legend(ncol = 2, byrow = TRUE))
#reverse颠倒图例顺序
p4 <- base + guides(fill = guide_legend(reverse = TRUE))
#override.aes:覆盖从每个图层派生的一些图形属性设置
base1 <- ggplot(mpg, aes(displ, hwy, colour = drv)) +
geom_point(size = 4, alpha = .2, stroke = 0)
p5 <- base1 + guides(colour = guide_legend())
p6 <- base1 + guides(colour = guide_legend(override.aes = list(alpha = 1)))
#axis图例处坐标轴控制
base2 <- ggplot(mpg, aes(displ, manufacturer, size = hwy)) +
geom_point(alpha = .2) +
scale_size_binned()
p7 <- base2
p8 <- base2 + guides(size = guide_bins(axis = FALSE))
#direction设置图例方向
p9 <- base2 + guides(size = guide_bins(direction = "vertical"))
p10 <- base2 + guides(size = guide_bins(direction = "horizontal"))
#show.limits,axis.colour, axis.linewidth和axis.arrow控制图例处axis属性
base3 <- ggplot(mpg, aes(cyl, displ, colour = hwy)) +
geom_point(size = 2)
#guide_colourbar() / guide_colorbar()控制colourbar型图例
p13 <- base3
p14 <- base3 + guides(colour = guide_colourbar(reverse = TRUE))
#设置colourbar高度2cm
p15 <- base3 + guides(colour = guide_colourbar(barheight = unit(2, "cm")))
#guide_coloursteps() / guide_colorsteps()控制刻度显示形式
base4 <- ggplot(mpg, aes(displ, hwy, colour = cyl)) +
geom_point() +
scale_color_binned()
p16 <- base4
p17 <- base4 + guides(colour = guide_coloursteps(show.limits = TRUE))#colourbar首尾显示
p18 <- base4 + guides(colour = guide_coloursteps(show.limits = FALSE))
p19 <- grid.arrange(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p13,p14,p15,p16,p17,p18,nrow = 8)
ggsave("scale10.png", p19, width = 5, height = 15)
本文结束,更多好文:
有意见请移步到QQ群629562529反馈,一起进步哈!