Examples for 'ggplot2::guides'


Set guides for each scale

Aliases: guides

Keywords:

### ** Examples

## No test: 
# 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.
plot of chunk example-ggplot2-guides-1
# 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.
plot of chunk example-ggplot2-guides-1
p + guides(colour = guide_colorbar(), size = guide_legend(),
  shape = guide_legend())
Warning: Using size for a discrete variable is not advised.
plot of chunk example-ggplot2-guides-1
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.
plot of chunk example-ggplot2-guides-1
 # Remove some guides
 p + guides(colour = "none")
Warning: Using size for a discrete variable is not advised.
plot of chunk example-ggplot2-guides-1
 p + guides(colour = "colorbar",size = "none")
Warning: Using size for a discrete variable is not advised.
plot of chunk example-ggplot2-guides-1
# Guides are integrated where possible

p +
  guides(
    colour = guide_legend("title"),
    size = guide_legend("title"),
    shape = guide_legend("title")
 )
Warning: Using size for a discrete variable is not advised.
plot of chunk example-ggplot2-guides-1
# same as
g <- guide_legend("title")
p + guides(colour = g, size = g, shape = g)
Warning: Using size for a discrete variable is not advised.
plot of chunk example-ggplot2-guides-1
p + theme(legend.position = "bottom")
Warning: Using size for a discrete variable is not advised.
plot of chunk example-ggplot2-guides-1
# position of guides

# Set order for multiple guides
ggplot(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)
 )
plot of chunk example-ggplot2-guides-1
## End(No test)

[Package ggplot2 version 3.3.6 Index]