Show code
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
tidyverse,
httr,
readxl,
sysfonts,
ggtext,
janitor, #for clean_names()
scales,
glue,
here
)
font_add_google("Montserrat")
October 14, 2020
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
tidyverse,
httr,
readxl,
sysfonts,
ggtext,
janitor, #for clean_names()
scales,
glue,
here
)
font_add_google("Montserrat")
GET("https://query.data.world/s/vihrcc3hjn3umv2dy67iu2zxuyf2en", write_disk(tf <- tempfile(fileext = ".xlsx")))
Response [https://download.data.world/file_download/makeovermonday/2020w41-data-assets-and-data-culture/Book1.xlsx?auth=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJwcm9kLXVzZXItY2xpZW50Om1yYWZhMyIsImlzcyI6ImFnZW50Om1yYWZhMzo6ODg0MjQ2ZDItMTgzMy00NmZjLTk2YTMtZjQ2MWMzMDJjOTZiIiwiaWF0IjoxNjAyNTI1NjIyLCJyb2xlIjpbInVzZXIiLCJ1c2VyX2FwaV9hZG1pbiIsInVzZXJfYXBpX3JlYWQiLCJ1c2VyX2FwaV93cml0ZSJdLCJnZW5lcmFsLXB1cnBvc2UiOmZhbHNlLCJ1cmwiOiI4NjdiNWU5MjcyM2U4NWEyY2I1ZGFhMzc1NzY2YzkyODY5ODM5Y2VhIn0.RuS7i_y0cGnKRPP1uTVAHZvRhsHkwkg5ExCuT0IKWSw3YrEbh36F7jROtavuinjPgy-pZTixSYm54OMrF1VHkw]
Date: 2024-12-12 19:09
Status: 200
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Size: 9.19 kB
<ON DISK> /var/folders/ck/dmgn8lbx6vl8sl89vlhr0sv00000gn/T//RtmpCqT2p5/file41623c6cafd.xlsx
df <- read_excel(tf) %>%
clean_names()
df %>%
glimpse()
Rows: 16
Columns: 2
$ industry_sector <chr> "Other", "Travel and leisure", "Logistics", "Telec…
$ respondents_percent <dbl> 10.9, 7.6, 2.2, 2.2, 13.0, 6.5, 8.7, 5.4, 3.3, 8.7…
financial_sector <- c('Financial services - other',
'Financial services - insurance',
'Financial services - banking')
df <- df %>%
mutate(financial_sector =
ifelse(industry_sector %in% financial_sector,
'Financial', 'Other')) %>%
mutate(industry_sector_2 = ifelse(financial_sector == 'Financial', industry_sector, 'Other'))
industry_map <- df %>%
select(financial_sector, industry_sector_2)
mm_week <- 41
mm_year <- 2020
my_theme <- theme(
text = element_text(family = 'Montserrat', size=12, color='black'),
plot.title = element_textbox(),
plot.subtitle = element_textbox(),
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_text(size=14, margin = margin(r=-10)),
plot.background = element_rect(fill = 'white'),
panel.background = element_blank(),
legend.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.title=element_blank(),
legend.position = 'none')
industry_cols <- c('Financial' = 'darkorange2',
'Other' = 'gray80')
respondents_plot <- df %>%
ggplot(.,
aes(x=respondents_percent / 100,
y=reorder(industry_sector, respondents_percent))) +
geom_col(aes(fill=financial_sector)) +
geom_text(aes(x=respondents_percent / 100 + .01,
label=paste(respondents_percent, '%', sep="")),
family='Montserrat', fontface='bold', size=7) +
annotate("text", label="Makeover Monday\nWeek 41 (2020)", x=.11, y=2, family="Montserrat",
size=7, lineheight=.9) +
labs(title='Responses to data and analytics strategy adoption survey by **dataIQ**',
subtitle=glue("Percent of respondents by industry, highlighting the <span style='color:darkorange2'>**financial sector**")) +
scale_x_continuous(limits = c(0, .15),
labels = percent) +
scale_fill_manual(values = industry_cols) +
my_theme +
theme(plot.title = element_textbox_simple(size=rel(2), margin = margin(b=5)),
plot.subtitle = element_textbox_simple(size=rel(1.35), margin = margin(b=5)),
axis.text = element_text(size=rel(1.4)))
# Save the plot as PNG
ggsave(
filename = glue("mm_{mm_year}_{mm_week}.png"),
plot = respondents_plot,
width = 10, height = 6, units = "in", dpi = 320
)
# make thumbnail for page
magick::image_read(glue("mm_{mm_year}_{mm_week}.png")) %>%
magick::image_resize(geometry = "400") %>%
magick::image_write(glue("mm_{mm_year}_{mm_week}_thumbnail.png"))
R version 4.4.0 (2024-04-24)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.6.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: America/Denver
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] here_1.0.1 glue_1.8.0 scales_1.3.0 janitor_2.2.0
[5] ggtext_0.1.2 sysfonts_0.8.9 readxl_1.4.3 httr_1.4.7
[9] lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4
[13] purrr_1.0.2 readr_2.1.5 tidyr_1.3.1 tibble_3.2.1
[17] ggplot2_3.5.1 tidyverse_2.0.0 pacman_0.5.1
loaded via a namespace (and not attached):
[1] utf8_1.2.4 generics_0.1.3 xml2_1.3.6 stringi_1.8.4
[5] hms_1.1.3 digest_0.6.37 magrittr_2.0.3 evaluate_1.0.1
[9] grid_4.4.0 timechange_0.3.0 fastmap_1.2.0 rprojroot_2.0.4
[13] cellranger_1.1.0 jsonlite_1.8.9 fansi_1.0.6 textshaping_0.4.0
[17] cli_3.6.3 rlang_1.1.4 commonmark_1.9.2 munsell_0.5.1
[21] withr_3.0.1 yaml_2.3.10 tools_4.4.0 tzdb_0.4.0
[25] colorspace_2.1-1 curl_5.2.3 vctrs_0.6.5 R6_2.5.1
[29] magick_2.8.5 lifecycle_1.0.4 snakecase_0.11.1 htmlwidgets_1.6.4
[33] ragg_1.3.3 pkgconfig_2.0.3 pillar_1.9.0 gtable_0.3.5
[37] Rcpp_1.0.13 systemfonts_1.1.0 xfun_0.48 tidyselect_1.2.1
[41] rstudioapi_0.17.1 knitr_1.48 farver_2.1.2 htmltools_0.5.8.1
[45] labeling_0.4.3 rmarkdown_2.28 compiler_4.4.0 markdown_1.13
[49] gridtext_0.1.5
---
title: "Visualizing responses to an analytics strategy survey"
description: "Refreshing a style-forward graphic to improve understanding"
author:
- name: Mickey Rafa
url: https://mrafa3.github.io/
date: 10-14-2020
categories: [R, "#MakeoverMonday", bar-plot] # self-defined categories
image: "mm_2020_41_thumbnail.png"
draft: false # setting this to `true` will prevent your post from appearing on your listing page until you're ready!
format:
html:
toc: true
toc-depth: 5
code-link: true
code-fold: true
code-tools: true
code-summary: "Show code"
self-contained: true
editor_options:
chunk_output_type: inline
execute:
error: false
message: false
warning: false
eval: true
---
{#fig-1}
{#fig-2}
# 1. Load Packages & Setup
```{r setup, include=TRUE}
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
tidyverse,
httr,
readxl,
sysfonts,
ggtext,
janitor, #for clean_names()
scales,
glue,
here
)
font_add_google("Montserrat")
```
# 2. Read in the Data
```{r read_data, include=TRUE}
GET("https://query.data.world/s/vihrcc3hjn3umv2dy67iu2zxuyf2en", write_disk(tf <- tempfile(fileext = ".xlsx")))
df <- read_excel(tf) %>%
clean_names()
```
# 3. Examine the Data
```{r examine, include=TRUE, echo=TRUE}
df %>%
glimpse()
```
# 4. Tidy the Data
```{r mutate_df, include=TRUE}
financial_sector <- c('Financial services - other',
'Financial services - insurance',
'Financial services - banking')
df <- df %>%
mutate(financial_sector =
ifelse(industry_sector %in% financial_sector,
'Financial', 'Other')) %>%
mutate(industry_sector_2 = ifelse(financial_sector == 'Financial', industry_sector, 'Other'))
industry_map <- df %>%
select(financial_sector, industry_sector_2)
```
# 5. Visualization Parameters
```{r my_theme, include=TRUE}
mm_week <- 41
mm_year <- 2020
my_theme <- theme(
text = element_text(family = 'Montserrat', size=12, color='black'),
plot.title = element_textbox(),
plot.subtitle = element_textbox(),
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_text(size=14, margin = margin(r=-10)),
plot.background = element_rect(fill = 'white'),
panel.background = element_blank(),
legend.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.title=element_blank(),
legend.position = 'none')
industry_cols <- c('Financial' = 'darkorange2',
'Other' = 'gray80')
```
# 6. Plot
```{r respondents_plot, include=TRUE, fig.width=5, fig.height=3, fig.align='center'}
respondents_plot <- df %>%
ggplot(.,
aes(x=respondents_percent / 100,
y=reorder(industry_sector, respondents_percent))) +
geom_col(aes(fill=financial_sector)) +
geom_text(aes(x=respondents_percent / 100 + .01,
label=paste(respondents_percent, '%', sep="")),
family='Montserrat', fontface='bold', size=7) +
annotate("text", label="Makeover Monday\nWeek 41 (2020)", x=.11, y=2, family="Montserrat",
size=7, lineheight=.9) +
labs(title='Responses to data and analytics strategy adoption survey by **dataIQ**',
subtitle=glue("Percent of respondents by industry, highlighting the <span style='color:darkorange2'>**financial sector**")) +
scale_x_continuous(limits = c(0, .15),
labels = percent) +
scale_fill_manual(values = industry_cols) +
my_theme +
theme(plot.title = element_textbox_simple(size=rel(2), margin = margin(b=5)),
plot.subtitle = element_textbox_simple(size=rel(1.35), margin = margin(b=5)),
axis.text = element_text(size=rel(1.4)))
```
# 7. Save
```{r save_plot, include=TRUE}
# Save the plot as PNG
ggsave(
filename = glue("mm_{mm_year}_{mm_week}.png"),
plot = respondents_plot,
width = 10, height = 6, units = "in", dpi = 320
)
# make thumbnail for page
magick::image_read(glue("mm_{mm_year}_{mm_week}.png")) %>%
magick::image_resize(geometry = "400") %>%
magick::image_write(glue("mm_{mm_year}_{mm_week}_thumbnail.png"))
```
# 8. Session Info
::: {.callout-tip collapse="true"}
##### Expand for Session Info
```{r session_info, echo = FALSE}
sessionInfo()
```
:::
# 9. Github Repository
::: {.callout-tip collapse="true"}
##### Expand for GitHub Repo
[Access the GitHub repository here](https://github.com/mrafa3/mrafa3.github.io)
:::