Skip to content
R for the Rest of Us Logo

Bring it All Together (Advanced Data Visualization)

If the video is not playing correctly, you can watch it in a new window

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

View code shown in video
---
title: "R Survey"
author: "David Keyes"
format: html
execute: 
  echo: false
  warning: false
  message: false
  output: true
editor_options: 
  chunk_output_type: console
---

```{r}
# Load packages

library(tidyverse)
library(janitor)
library(scales)
library(ggtext)
```

```{r}
demographics <- read_rds("data/demographics.rds")
other_coding_languages <- read_rds("data/other_coding_languages.rds")
```

Here is a data viz of the most common coding languages people say they know.

```{r}
#| fig-height: 5

update_geom_defaults(geom = "text",
                     aes(family = "Inter"))

other_coding_languages |> 
  count(qcoding_languages) |> 
  slice_max(order_by = n,
            n = 10) |> 
  mutate(qcoding_languages = fct_reorder(qcoding_languages, n)) |> 
  mutate(n_formatted = comma(n)) |> 
  mutate(is_r = case_when(
    qcoding_languages == "R" ~ "Y",
    .default = "N"
  )) |> 
  ggplot(aes(x = n,
             y = qcoding_languages,
             fill = is_r,
             color = is_r)) +
  geom_col(width = 0.5,
           color = "transparent") +
  geom_text(aes(label = n_formatted),
            hjust = 1.25,
            color = "white") +
  geom_text(aes(x = 0,
                label = qcoding_languages),
            hjust = 0,
            vjust = -1.5) +
  labs(title = "<b style='color: #6cabdd;'>R</b> is by far the most the popular language among respondents") +
  theme_void(base_family = "Inter") +
  theme(legend.position = "none",
        plot.title = element_markdown(margin = margin(b = 10,
                                                      unit = "pt"))) +
  scale_x_continuous(expand = expansion(add = 0)) +
  scale_fill_manual(values = c(
    "Y" = "#6cabdd",
    "N" = "grey80"
  )) +
  scale_color_manual(values = c(
    "Y" = "#6cabdd",
    "N" = "grey70"
  ))
```

Here is the data viz of the most common coding languages people say they know **broken out by country**.

```{r}
other_coding_languages |> 
  left_join(demographics,
            join_by(id)) |> 
  count(qcoding_languages, qdegree) |> 
  slice_max(order_by = n,
            n = 10) |> 
  mutate(qcoding_languages = fct_reorder(qcoding_languages, n)) |> 
  ggplot(aes(x = n,
             y = qcoding_languages)) +
  geom_col() +
  theme_minimal() +
  facet_wrap(vars(qdegree))
```

Learn More

Another nice example of creating a nice line plot comes from Albert Rapp's video titled Stop making messy line charts and create meaningful plots instead.

A more general resource comes from the European Union's Data Visualization Guide. It's got section on best practices (including the concepts shown in this course) and some specifics on how to implement these practices with ggplot code.

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.