When in the data exploratory phase, you can quickly generate very basic visualizations with minimal effort.

For this introductory section on visualization we use the ggvis package because it works seamlessly with pipes (i.e. %>%). You can do a lot with ggvis. You can do even more with ggplot2. We offer a separate workshop on ggplot2.

library(tidyverse)
library(ggvis)

Wrangle Data

Create a data frame without Jabba

not_jabba <- starwars %>% 
  filter(!is.na(mass),
         !is.na(height),
         mass < 400)

Subset the Species list to six species.

starwars_species_small <- starwars %>% 
  filter(!is.na(mass),
         !is.na(height),
         !is.na(birth_year),
         !is.na(species),
         mass < 400,
         species == "Human" | 
           species == "Droid" | 
           species == "Gungan" |
           species == "Mirialan" |
           species == "Ewok" |
           species == "Wookiee")

levels <- starwars_species_small %>%
  group_by(species) %>% 
  count() %>% 
  arrange(-n) #%>% 
  #.[1]

#starwars_species_small_as_fctr  <-  starwars_species_small %>% 
#  mutate(species = parse_factor(species, levels$species))

Scatter Plot

Linear model and 95% confidence interval

not_jabba %>% 
  ggvis(~height, ~mass) %>% 
  layer_points() %>% 
  layer_model_predictions(model = "lm", se = TRUE)
## Guessing formula = mass ~ height

More information on ggvis scatterplots | line graphs

Bar Graph

starwars_species_small %>% 
  ggvis(~fct_infreq(species)) %>% 
  layer_bars()

More information on ggvis bar graphs

Histogram

starwars %>% 
  ggvis(~birth_year) %>% 
  layer_histograms()

More information on ggvis histograms

Grouping data points

starwars_species_small %>% 
  ggvis(~height, ~mass) %>% 
  group_by(species) %>% 
  layer_points(~height, ~mass, 
               stroke := "black", 
               fill = ~species,
               size := ~birth_year, 
               opacity := 0.7)

 

BoxPlot

starwars_species_small %>% 
  filter(species != "Ewok", 
           species != "Wookiee",
           species != "Gungan") %>% 
  ggvis(~fct_infreq(species), ~mass) %>% 
  layer_boxplots()

More information on box plots

 
R We Having Fun Yet‽ -- Learning Series
Data & Visualization Services
Duke University Libraries
C bn
Shareable via Creative Commons: CC By-NC