R Data Visualization Recipes
上QQ阅读APP看书,第一时间看更新

How to do it...

Here is how we draw simple box plot using ggplot2:

  1. Begin by calling geom_boxplot() to draw box plot using ggplot2:
> library(ggplot2)
> box1 <- ggplot(data = car::Salaries,
aes( x = rank, y = salary))
> box1 + geom_boxplot( outlier.alpha = .4 )

Resulting graphic looks like the following figure 3.1 (alpha blending is applied to the outliers):

Figure 3.1: Simple ggplot2 box plot

  1. Similar one can be drawn by plotly:
> library(plotly)
> box2 <- plot_ly(data = car::Salaries, y = ~salary, x = ~rank,
type = 'box', marker = list(opacity = .4))
> box2
  1. Using ggvis, calling for alpha requires a little bit more code:
> library(ggvis)
> box3 <- ggvis(data = car::Salaries,
x = ~rank, y = ~salary)
> box3 %>% layer_boxplots(opacity := .4) %>%
layer_boxplots(size := 0)

Let's check the explanations.