Examples for 'ggplot2::aes_group_order'


Aesthetics: grouping

Aliases: aes_group_order group

Keywords:

### ** Examples

## No test: 

p <- ggplot(mtcars, aes(wt, mpg))
# A basic scatter plot
p + geom_point(size = 4)
plot of chunk example-ggplot2-aes_group_order-1
# Using the colour aesthetic
p + geom_point(aes(colour = factor(cyl)), size = 4)
plot of chunk example-ggplot2-aes_group_order-1
# Using the shape aesthetic
p + geom_point(aes(shape = factor(cyl)), size = 4)
plot of chunk example-ggplot2-aes_group_order-1
# Using fill
p <- ggplot(mtcars, aes(factor(cyl)))
p + geom_bar()
plot of chunk example-ggplot2-aes_group_order-1
p + geom_bar(aes(fill = factor(cyl)))
plot of chunk example-ggplot2-aes_group_order-1
p + geom_bar(aes(fill = factor(vs)))
plot of chunk example-ggplot2-aes_group_order-1
# Using linetypes
ggplot(economics_long, aes(date, value01)) +
  geom_line(aes(linetype = variable))
plot of chunk example-ggplot2-aes_group_order-1
# Multiple groups with one aesthetic
p <- ggplot(nlme::Oxboys, aes(age, height))
# The default is not sufficient here. A single line tries to connect all
# the observations.
p + geom_line()
plot of chunk example-ggplot2-aes_group_order-1
# To fix this, use the group aesthetic to map a different line for each
# subject.
p + geom_line(aes(group = Subject))
plot of chunk example-ggplot2-aes_group_order-1
# Different groups on different layers
p <- p + geom_line(aes(group = Subject))
# Using the group aesthetic with both geom_line() and geom_smooth()
# groups the data the same way for both layers
p + geom_smooth(aes(group = Subject), method = "lm", se = FALSE)
`geom_smooth()` using formula 'y ~ x'
plot of chunk example-ggplot2-aes_group_order-1
# Changing the group aesthetic for the smoother layer
# fits a single line of best fit across all boys
p + geom_smooth(aes(group = 1), size = 2, method = "lm", se = FALSE)
`geom_smooth()` using formula 'y ~ x'
plot of chunk example-ggplot2-aes_group_order-1
# Overriding the default grouping
# Sometimes the plot has a discrete scale but you want to draw lines
# that connect across groups. This is the strategy used in interaction
# plots, profile plots, and parallel coordinate plots, among others.
# For example, we draw boxplots of height at each measurement occasion.
p <- ggplot(nlme::Oxboys, aes(Occasion, height)) + geom_boxplot()
p
plot of chunk example-ggplot2-aes_group_order-1
# There is no need to specify the group aesthetic here; the default grouping
# works because occasion is a discrete variable. To overlay individual
# trajectories, we again need to override the default grouping for that layer
# with aes(group = Subject)
p + geom_line(aes(group = Subject), colour = "blue")
plot of chunk example-ggplot2-aes_group_order-1
## End(No test)

[Package ggplot2 version 3.3.6 Index]