Examples for 'ggplot2::geom_boxplot'


A box and whiskers plot (in the style of Tukey)

Aliases: geom_boxplot stat_boxplot

Keywords:

### ** Examples

p <- ggplot(mpg, aes(class, hwy))
p + geom_boxplot()
plot of chunk example-ggplot2-geom_boxplot-1
# Orientation follows the discrete axis
ggplot(mpg, aes(hwy, class)) + geom_boxplot()
plot of chunk example-ggplot2-geom_boxplot-1
p + geom_boxplot(notch = TRUE)
notch went outside hinges. Try setting notch=FALSE.
notch went outside hinges. Try setting notch=FALSE.
plot of chunk example-ggplot2-geom_boxplot-1
p + geom_boxplot(varwidth = TRUE)
plot of chunk example-ggplot2-geom_boxplot-1
p + geom_boxplot(fill = "white", colour = "#3366FF")
plot of chunk example-ggplot2-geom_boxplot-1
# By default, outlier points match the colour of the box. Use
# outlier.colour to override
p + geom_boxplot(outlier.colour = "red", outlier.shape = 1)
plot of chunk example-ggplot2-geom_boxplot-1
# Remove outliers when overlaying boxplot with original data points
p + geom_boxplot(outlier.shape = NA) + geom_jitter(width = 0.2)
plot of chunk example-ggplot2-geom_boxplot-1
# Boxplots are automatically dodged when any aesthetic is a factor
p + geom_boxplot(aes(colour = drv))
plot of chunk example-ggplot2-geom_boxplot-1
# You can also use boxplots with continuous x, as long as you supply
# a grouping variable. cut_width is particularly useful
ggplot(diamonds, aes(carat, price)) +
  geom_boxplot()
Warning: Continuous x aesthetic -- did you forget aes(group=...)?
plot of chunk example-ggplot2-geom_boxplot-1
ggplot(diamonds, aes(carat, price)) +
  geom_boxplot(aes(group = cut_width(carat, 0.25)))
plot of chunk example-ggplot2-geom_boxplot-1
# Adjust the transparency of outliers using outlier.alpha
ggplot(diamonds, aes(carat, price)) +
  geom_boxplot(aes(group = cut_width(carat, 0.25)), outlier.alpha = 0.1)
plot of chunk example-ggplot2-geom_boxplot-1
## No test: 
# It's possible to draw a boxplot with your own computations if you
# use stat = "identity":
y <- rnorm(100)
df <- data.frame(
  x = 1,
  y0 = min(y),
  y25 = quantile(y, 0.25),
  y50 = median(y),
  y75 = quantile(y, 0.75),
  y100 = max(y)
)
ggplot(df, aes(x)) +
  geom_boxplot(
   aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
   stat = "identity"
 )
plot of chunk example-ggplot2-geom_boxplot-1
## End(No test)

[Package ggplot2 version 3.3.6 Index]