+ - 0:00:00
Notes for current slide
Notes for next slide



Visaluzing Data:
Part II

Dr. Mine Dogucu

1 / 41

Review

2 / 41

Outline

  • More ggplot

  • ggplot extensions

  • Accessible Data Visualization

3 / 41

  • Using the penguins data,
  • Map bill depth to x-axis, bill length to y-axis, species to shape and color.
  • Add a layer of points and set the size of the points to 4.
ggplot(penguins,
aes(x = bill_depth_mm,
y = bill_length_mm,
shape = species,
color = species)) +
geom_point(size = 4)
4 / 41

Labs

  • Using the penguins data,
  • Map bill depth to x-axis, bill length to y-axis, species to shape.
  • Add a layer of points and set the size of the points to 4.
  • Add labels to x-axis (Bill Depth(mm)), y-axis (Bill Length(mm)), and the title of the plot (Palmer Penguins).
ggplot(penguins,
aes(x = bill_depth_mm,
y = bill_length_mm,
shape = species,
color = species)) +
geom_point(size = 4) +
labs(x = "Bill Depth (mm)",
y = "Bill Length (mm)",
title = "Palmer Penguins")
5 / 41
ggplot(penguins,
aes(x = bill_depth_mm,
y = bill_length_mm,
shape = species,
color = species)) +
geom_point() +
labs(x = "Bill Depth (mm)",
y = "Bill Length (mm)",
title = "Palmer Penguins") +
theme_bw()

6 / 41
ggplot(penguins,
aes(x = bill_depth_mm,
y = bill_length_mm,
shape = species,
color = species)) +
geom_point() +
labs(x = "Bill Depth (mm)",
y = "Bill Length (mm)",
title = "Palmer Penguins") +
theme_bw() +
theme(text = element_text(size=20))

8 / 41
ggplot(penguins,
aes(x = bill_depth_mm,
y = bill_length_mm,
shape = species,
color = species)) +
geom_point(size = 4) +
facet_grid(.~species)

10 / 41
ggplot(penguins,
aes(x = bill_depth_mm,
y = bill_length_mm,
shape = species,
color = species)) +
geom_point(size = 4) +
facet_grid(species~.)

11 / 41
ggplot(penguins,
aes(x = bill_depth_mm,
y = bill_length_mm)) +
geom_point() +
xlim(0, 30) +
ylim(0, 70)

12 / 41

Check out the ggplot flipbook for some inspiration. Find your favorite new function/feature. Share it with your neighbor.

14 / 41
library(maps)
Warning: package 'maps' was built under R version 4.1.1
usa <- map_data("usa")
glimpse(usa)
Rows: 7,243
Columns: 6
$ long <dbl> -101.4078, -101.3906, -101.3620, -101.3505, -101.3219, -101.~
$ lat <dbl> 29.74224, 29.74224, 29.65056, 29.63911, 29.63338, 29.64484, ~
$ group <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ~
$ order <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1~
$ region <chr> "main", "main", "main", "main", "main", "main", "main", "mai~
$ subregion <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ~
15 / 41




ggplot(data = usa,
aes(x = long,
y = lat))

16 / 41




ggplot(data = usa,
aes(x = long,
y = lat)) +
geom_polygon()

17 / 41




ggplot(data = usa,
aes(x = long,
y = lat)) +
geom_polygon() +
coord_fixed(1.4)

18 / 41




ggplot(data = usa,
aes(x = long,
y = lat)) +
geom_polygon(fill = NA,
color = "steelblue") +
coord_fixed(1.4)

19 / 41




ggplot(data = usa,
aes(x = long,
y = lat)) +
geom_polygon(fill = NA,
color = "steelblue",
aes(group = group)) +
coord_fixed(1.4)

20 / 41
location <- c("Northampton", "Columbus", "Sarasota","Irvine" )
long <- c(-72.6412, -82.998795, -82.538602, -117.824719)
lat <- c(42.3251, 39.961178, 27.336483, 33.685908)
pop <- c(28726, 892533, 57738, 282572)
home <- tibble::tibble(location = location,
lat = lat,
long = long,
pop = pop)
21 / 41
home
# A tibble: 4 x 4
location lat long pop
<chr> <dbl> <dbl> <dbl>
1 Northampton 42.3 -72.6 28726
2 Columbus 40.0 -83.0 892533
3 Sarasota 27.3 -82.5 57738
4 Irvine 33.7 -118. 282572
22 / 41




usa_map <-
ggplot(data = usa,
aes(x = long,
y = lat)) +
geom_polygon(fill = NA, color = "steelblue",
aes(group = group)) +
coord_fixed(1.4)
23 / 41




usa_map +
geom_point(data = home,
aes(x = long,
y = lat,
color = pop))

24 / 41




usa_map +
geom_point(data = home,
aes(x = long,
y = lat,
size = pop))

25 / 41




states <- map_data("state")
ggplot(data = states,
aes(x = long,
y = lat,
fill = region)) +
geom_polygon(aes(group = group)) +
coord_fixed(1.4) +
guides(fill = "none")

26 / 41

ggplot extensions

28 / 41
  • patchwork combining plots into a single plot
  • gganimate animated graphics
  • ggthemes additional set of themes
  • ggtext improved text rendering support for ggplot2
  • ggdag Causal directed acyclic graphs (DAGs)
  • survminer survival analysis and visualization
  • gggenes gene arrow maps

There are more extensions.

29 / 41

Making Accessible Visualizations

30 / 41

Color blindness simulation: red-blind/protanopia

31 / 41

Color blindness simulation: green-blind/deuteranopia

32 / 41

Color blindness simulation: blue-blind/tritanopia

33 / 41
palette.colors(palette = "Okabe-Ito")
black orange skyblue bluishgreen yellow
"#000000" "#E69F00" "#56B4E9" "#009E73" "#F0E442"
blue vermillion reddishpurple gray
"#0072B2" "#D55E00" "#CC79A7" "#999999"

Okabe-Ito color palette allows us to make figures that are colorblind friendly. You can use the HEX code printed for selecting colors.

34 / 41
ggplot(penguins,
aes(x = bill_depth_mm,
y = bill_length_mm,
shape = species,
color = species)) +
geom_point(size = 4) +
facet_grid(species~.) +
scale_color_manual(values =
c("#E69F00",
"#009E73",
"#56B4E9"))

35 / 41

Detour: R Markdown chunk options

{r, echo=TRUE, message=FALSE}

36 / 41

(some) Chunk Options in R Markdown

echo = FALSE hides the code
message = FALSE hides messages
warning = FALSE hides warning
error = TRUE renders despite errors and displays the error
fig.cap = "Some figure caption" creates a figure caption
fig.alt = "Some alternate text for figure" creates alternate text for figures
37 / 41

Alternate Text

"Alt text" describes contents of an image. It is used in HTML pages such as the R Markdown outputs we have been writing. Screen-readers cannot read images but can read alt text. Alt text has to be provided and cannot magically appear.

38 / 41
39 / 41

Potential Content of Alternate Text for Data Visualization

  1. Data source/frame
  2. Variables and axes information including units
  3. The plot type
  4. The message that the plot conveys.
40 / 41

Please complete the in-class activity provided to you in lecture notes.

41 / 41

Review

2 / 41
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow