ggplot2----Guides:axes and legen
Guides for each scale can be set scale-by-scale with the guide argument, or en masse with guides()
图例的每个参数都可以一个个进行设置
guides(...)
Arguments 参数
List of scale name-guide pairs. The guide can either be a string (i.e. "colorbar" or "legend"), or a call to a guide function (i.e. guide_colourbar or guide_legend) specifying additional arguments.
guides 可以成对设置图例的参数,比如 colorbar和legend,或者使用guide_colorbar 或者 guide_legend 来进行设置。
Value
A list containing the mapping between scale and guide.See alsoOther guides: guide_colourbar, guide_legend
Examples
# ggplot object
dat <- data.frame(x = 1:5, y = 1:5, p = 1:5, q = factor(1:5), r = factor(1:5))
p <- ggplot(dat, aes(x, y, colour = p, size = q, shape = r)) + geom_point()
# without guide specification
p
#> Warning: Using size for a discrete variable is not advised.
不建议使用大小作为离散变量
![](https://img.haomeiwen.com/i7087195/7f0c397a0653f94e.png)
# Show colorbar guide for colour.
# All these examples below have a same effect.
p + guides(colour = "colorbar", size = "legend", shape = "legend")
#> Warning: Using size for a discrete variable is not advised.
![](https://img.haomeiwen.com/i7087195/d8a60ece83a81626.png)
#> Warning: Using size for a discrete variable is not advised.
![](https://img.haomeiwen.com/i7087195/0c94b115a742f307.png)
p + scale_colour_continuous(guide = "colorbar") + scale_size_discrete(guide = "legend") + scale_shape(guide = "legend")
#> Warning: Using size for a discrete variable is not advised.
![](https://img.haomeiwen.com/i7087195/dd4aba2d3c0ed261.png)
# Remove some guides p + guides(colour = "none")
#> Warning: Using size for a discrete variable is not advised.
![](https://img.haomeiwen.com/i7087195/86cc4c47e0539723.png)
p + guides(colour = "colorbar",size = "none")
#> Warning: Using size for a discrete variable is not advised.
![](https://img.haomeiwen.com/i7087195/a0e4fbe5169ff596.png)
#> Warning: Using size for a discrete variable is not advised.
![](https://img.haomeiwen.com/i7087195/e3837a7a3c25e87f.png)
# same asg <- guide_legend("title")p + guides(colour = g, size = g, shape = g)
#> Warning: Using size for a discrete variable is not advised.
![](https://img.haomeiwen.com/i7087195/ec67d33ceb27b1f6.png)
p + theme(legend.position = "bottom")
#> Warning: Using size for a discrete variable is not advised.
![](https://img.haomeiwen.com/i7087195/70c050fd8e3b09e1.png)
# position of guides# Set order for multiple guidesggplot(mpg, aes(displ, cty)) + geom_point(aes(size = hwy, colour = cyl, shape = drv)) + guides( colour = guide_colourbar(order = 1), shape = guide_legend(order = 2), size = guide_legend(order = 3) )
![](https://img.haomeiwen.com/i7087195/b5267c80afc026b3.png)