O'Reilly logo

R Data Visualization Recipes by Vitor Bianchi Lanzetta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

This section shows you how to create a faceted bar graph:

  1.  Coerce the Titanic table to the data frame type:
> data_titanic <- as.data.frame(Titanic)
  1. Load ggplot2, design a bar graph, and add the facets using face_grid() function:
> library(ggplot2)> bar <- ggplot(data_titanic, aes(x = Survived)) +    geom_bar(aes(fill = Survived, weight = Freq)) +    facet_grid(Sex ~ Age)
  1. Check the result by calling the object storing the plot:
> bar

Output might look much like the following illustration (Figure 7.1):

Figure 7.1 - Faceted bar graph, Titanic survivals based on age range and gender.
  1. Also, the plotly package could be called ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required