├── 2022
├── Week_39
│ ├── README.md
│ ├── week_39.png
│ └── week_39.R
├── Week_41
│ ├── README.md
│ ├── week_41.png
│ └── week_41.R
├── Week_42
│ ├── README.md
│ ├── week_42.png
│ └── week_42.R
├── Week_38
│ ├── README.md
│ ├── week_38.png
│ └── week_38.R
├── Week_34
│ ├── p1.png
│ ├── p2.png
│ ├── week_34.png
│ ├── README.md
│ └── week_34.R
├── Week_26
│ ├── week_26.png
│ ├── README.md
│ └── week_26.R
├── Week_27
│ ├── week_27.png
│ ├── README.md
│ └── week_27.R
├── Week_28
│ ├── week_28.png
│ ├── README.md
│ └── week_28.R
├── Week_29
│ ├── week_29.png
│ ├── README.md
│ └── week_29.R
├── Week_31
│ ├── week_31.png
│ ├── README.md
│ └── week_31.R
├── Week_33
│ ├── week_33.png
│ ├── Characters.png
│ ├── README.md
│ └── week_33.R
├── Week_35
│ ├── week_35.png
│ ├── README.md
│ └── week_35.R
├── Week_36
│ ├── week_36.png
│ ├── README.md
│ └── week_36.R
└── Week_37
│ ├── week_37.png
│ ├── README.md
│ └── week_37.R
├── 2023
├── Week-03
│ ├── README.md
│ ├── week-03.png
│ └── week-03.R
├── Week-08
│ ├── README.md
│ ├── week-08.png
│ └── week-08.R
├── Week-39
│ ├── README.md
│ ├── week-39.png
│ └── week-39.R
├── Week-48
│ ├── README.md
│ ├── week-48.png
│ └── week-48.R
├── Week-09
│ ├── README.md
│ ├── week-09.png
│ └── week-09.R
└── Week-10
│ ├── README.md
│ ├── week-10.png
│ └── week-10.R
├── 2024
├── week04
│ ├── week-04.png
│ └── uk_education.R
├── 2024-02-24
│ ├── Week08.png
│ ├── README.md
│ └── 2024-02-24.R
└── 2024-03-09
│ ├── Week10.png
│ ├── README.md
│ └── 2024-03-09.R
├── .gitignore
├── temp.R
├── new_week.R
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | *.Rhistory
2 | *.csv
3 | *.xlsx
--------------------------------------------------------------------------------
/temp.R:
--------------------------------------------------------------------------------
1 | source("D:/Projects/TidyTuesday/new_week.R")
2 | new_week()
3 | q()
--------------------------------------------------------------------------------
/2023/Week-03/README.md:
--------------------------------------------------------------------------------
1 | # 2023, Week 03
2 |
3 | 
4 |
--------------------------------------------------------------------------------
/2023/Week-08/README.md:
--------------------------------------------------------------------------------
1 | # 2023, Week 08
2 |
3 | 
4 |
--------------------------------------------------------------------------------
/2023/Week-39/README.md:
--------------------------------------------------------------------------------
1 | # 2023, Week 39
2 |
3 | 
4 |
--------------------------------------------------------------------------------
/2023/Week-48/README.md:
--------------------------------------------------------------------------------
1 | # 2023, Week 48
2 |
3 | 
4 |
--------------------------------------------------------------------------------
/2022/Week_39/README.md:
--------------------------------------------------------------------------------
1 | # 2022, Week 39
2 |
3 | 
4 |
--------------------------------------------------------------------------------
/2022/Week_41/README.md:
--------------------------------------------------------------------------------
1 | # 2022, Week 41
2 |
3 | 
4 |
--------------------------------------------------------------------------------
/2022/Week_42/README.md:
--------------------------------------------------------------------------------
1 | # 2022, Week 42
2 |
3 | 
4 |
--------------------------------------------------------------------------------
/2023/Week-09/README.md:
--------------------------------------------------------------------------------
1 | # 2023, Week 09
2 |
3 | 
4 |
--------------------------------------------------------------------------------
/2022/Week_38/README.md:
--------------------------------------------------------------------------------
1 | # 2022, Week 38
2 |
3 | 
4 |
--------------------------------------------------------------------------------
/2023/Week-10/README.md:
--------------------------------------------------------------------------------
1 | # 2023, Week 10
2 |
3 | 
4 |
--------------------------------------------------------------------------------
/2022/Week_34/p1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_34/p1.png
--------------------------------------------------------------------------------
/2022/Week_34/p2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_34/p2.png
--------------------------------------------------------------------------------
/2022/Week_26/week_26.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_26/week_26.png
--------------------------------------------------------------------------------
/2022/Week_27/week_27.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_27/week_27.png
--------------------------------------------------------------------------------
/2022/Week_28/week_28.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_28/week_28.png
--------------------------------------------------------------------------------
/2022/Week_29/week_29.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_29/week_29.png
--------------------------------------------------------------------------------
/2022/Week_31/week_31.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_31/week_31.png
--------------------------------------------------------------------------------
/2022/Week_33/week_33.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_33/week_33.png
--------------------------------------------------------------------------------
/2022/Week_34/week_34.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_34/week_34.png
--------------------------------------------------------------------------------
/2022/Week_35/week_35.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_35/week_35.png
--------------------------------------------------------------------------------
/2022/Week_36/week_36.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_36/week_36.png
--------------------------------------------------------------------------------
/2022/Week_37/week_37.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_37/week_37.png
--------------------------------------------------------------------------------
/2022/Week_38/week_38.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_38/week_38.png
--------------------------------------------------------------------------------
/2022/Week_39/week_39.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_39/week_39.png
--------------------------------------------------------------------------------
/2022/Week_41/week_41.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_41/week_41.png
--------------------------------------------------------------------------------
/2022/Week_42/week_42.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_42/week_42.png
--------------------------------------------------------------------------------
/2023/Week-03/week-03.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2023/Week-03/week-03.png
--------------------------------------------------------------------------------
/2023/Week-08/week-08.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2023/Week-08/week-08.png
--------------------------------------------------------------------------------
/2023/Week-09/week-09.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2023/Week-09/week-09.png
--------------------------------------------------------------------------------
/2023/Week-10/week-10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2023/Week-10/week-10.png
--------------------------------------------------------------------------------
/2023/Week-39/week-39.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2023/Week-39/week-39.png
--------------------------------------------------------------------------------
/2023/Week-48/week-48.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2023/Week-48/week-48.png
--------------------------------------------------------------------------------
/2024/week04/week-04.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2024/week04/week-04.png
--------------------------------------------------------------------------------
/2024/2024-02-24/Week08.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2024/2024-02-24/Week08.png
--------------------------------------------------------------------------------
/2024/2024-03-09/Week10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2024/2024-03-09/Week10.png
--------------------------------------------------------------------------------
/2022/Week_33/Characters.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/imagineazhar/TidyTuesday/HEAD/2022/Week_33/Characters.png
--------------------------------------------------------------------------------
/2022/Week_27/README.md:
--------------------------------------------------------------------------------
1 | # SF Rentals | Week 27
2 | 
3 |
--------------------------------------------------------------------------------
/2022/Week_28/README.md:
--------------------------------------------------------------------------------
1 | # EU Flights | Week 28
2 | 
3 |
--------------------------------------------------------------------------------
/2022/Week_29/README.md:
--------------------------------------------------------------------------------
1 | # Steel PK | Week 29
2 | 
3 |
--------------------------------------------------------------------------------
/2022/Week_34/README.md:
--------------------------------------------------------------------------------
1 | # 2022, Week 34
2 |
3 | 
--------------------------------------------------------------------------------
/2022/Week_36/README.md:
--------------------------------------------------------------------------------
1 | # 2022, Week 36
2 |
3 | 
4 |
--------------------------------------------------------------------------------
/2022/Week_37/README.md:
--------------------------------------------------------------------------------
1 | # 2022, Week 37
2 |
3 | 
4 |
--------------------------------------------------------------------------------
/2022/Week_35/README.md:
--------------------------------------------------------------------------------
1 | # 2022, Week 35
2 |
3 | 
4 |
--------------------------------------------------------------------------------
/2022/Week_26/README.md:
--------------------------------------------------------------------------------
1 | # UK Pay Gap | Week 26
2 |
3 | 
4 |
--------------------------------------------------------------------------------
/2022/Week_31/README.md:
--------------------------------------------------------------------------------
1 |
2 | # Frogs | Week 31
3 |
4 | 
5 |
--------------------------------------------------------------------------------
/2024/2024-03-09/README.md:
--------------------------------------------------------------------------------
1 | # 2024
2 | # Week Number: 10
3 |
4 | 
--------------------------------------------------------------------------------
/2024/2024-02-24/README.md:
--------------------------------------------------------------------------------
1 | # 2024
2 | # Week Number: 08
3 | 
4 |
5 |
--------------------------------------------------------------------------------
/2022/Week_33/README.md:
--------------------------------------------------------------------------------
1 | # 2022, Week 33
2 |
3 | 
4 | 
5 |
--------------------------------------------------------------------------------
/2022/Week_27/week_27.R:
--------------------------------------------------------------------------------
1 | library(dplyr)
2 | library(ggtext)
3 | library(ggplot2)
4 | library(tidytuesdayR)
5 | library(showtext)
6 | library(hrbrthemes)
7 |
8 |
9 | #default font from show text
10 | font_add_google("Noto Serif", "Noto Serif")
11 | font_add_google("Roboto", "roboto")
12 | showtext_auto()
13 |
14 | #read data
15 | tt_data <- tt_load(2022, week=27)
16 | rent <- tt_data$rent
17 |
18 | #clean Data
19 | rent_clean <- rent |> filter(city=='san francisco') |>
20 | drop_na()
21 |
22 | rent_sum <- rent_clean |> group_by(year, nhood)|>
23 | mutate(year=factor(year,levels = c('2014','2015','2016','2017','2018'))) |>
24 | summarise(price=mean(price))
25 |
26 | #plot
27 | rent_sum|> ggplot(aes(x=price))+
28 | geom_histogram( binwidth=1000, fill="#69b3a2", color="#e9ecef", alpha=0.9) +
29 | labs(x,title = "Price distribution of San Francisco Rentals",
30 | caption = "Data: Kate Pennington | Graphic: @imagineazhar") +
31 | theme_ipsum() +
32 | theme(
33 | plot.background = element_rect(fill = "#F0EBE3"),
34 | panel.background = element_rect(fill = "#F0EBE3"),
35 | plot.title = element_text( face = "bold", size=20.8,
36 | margin = margin(t=2), family = "Noto Serif"),
37 | plot.subtitle = element_text(face = "plain", size=12, color = "grey50",
38 | margin = margin(t=5,b=15),
39 | family = "Roboto", lineheight = 1.2),
40 | plot.caption = element_text(color = "grey50", vjust = 0, size = 11),
41 | plot.margin = unit(c(0.5,0.5,0.5,0.5), "cm"))
42 |
43 | #Save plot
44 | ggsave("week_27.png",
45 | units = 'in', width = 5, height = 4, dpi = 100)
46 |
--------------------------------------------------------------------------------
/2022/Week_28/week_28.R:
--------------------------------------------------------------------------------
1 | library(tidyverse)
2 | library(dplyr)
3 | library(ggtext)
4 | library(ggplot2)
5 | library(tidytuesdayR)
6 | library(showtext)
7 |
8 |
9 | #default font from showtext
10 | font_add_google("Noto Serif", "Noto Serif")
11 | font_add_google("Roboto", "roboto")
12 | showtext_auto()
13 |
14 | #read data
15 | tt_data <- tt_load(2022, week=28)
16 | flights <- tt_data$flights
17 |
18 | #clean data
19 | total <- flights|>group_by(STATE_NAME)|>
20 | ungroup()|>
21 | group_by(STATE_NAME, YEAR, MONTH_NUM)|>
22 | summarise(total_flights = sum(FLT_TOT_1))|>
23 | ungroup()|>
24 | unite(col = "yearmonth", YEAR, MONTH_NUM, remove = FALSE)|>
25 | distinct()|>
26 | group_by(STATE_NAME)|>
27 | mutate(share = total_flights/sum(total_flights, na.rm = TRUE)*100)|>
28 | ungroup()|>
29 | mutate(State2=STATE_NAME)
30 |
31 | #plot
32 | total |> ggplot(aes(x=yearmonth, y=total_flights, group=1))+
33 | geom_line(data = total|> dplyr::select(-STATE_NAME), aes(group=State2),
34 | color="grey", size=0.5, alpha=0.5)+
35 | geom_line(aes(color=STATE_NAME), color="#DF7861", size=1.2 )+
36 |
37 | labs(title = "Commercial Flights in Europe",
38 | subtitle = "Each line is associated to a European country with complete flight data from January 2016 to May 2022. The y-axis represents the total flights from and to each country.",
39 | caption = "Data Source:Eurocontrol | Created by @imagineazhar",
40 | x = "",
41 | y = "")+
42 | theme_void()+
43 | theme(
44 | plot.background = element_rect(fill = "#F0EBE3"),
45 | panel.background = element_rect(fill = "#F0EBE3"),
46 | plot.title = element_text( face = "bold", size=24.4,
47 | margin = margin(t=25), family = "Noto Serif"),
48 | plot.subtitle = element_text(face = "plain", size=12, color = "grey50",
49 | margin = margin(t=5,b=15),
50 | family = "Roboto", lineheight = 1.2),
51 | plot.caption = element_text(color = "grey50", vjust = 0, size = 11),
52 | plot.margin = unit(c(1,1,1,1), "cm"))+
53 | facet_wrap(~STATE_NAME)
54 |
55 | #Save plot
56 | ggsave("week_28.png",
57 | units = 'in', width = 7, height = 5, dpi = 300)
58 |
--------------------------------------------------------------------------------
/2022/Week_36/week_36.R:
--------------------------------------------------------------------------------
1 | library(tidyverse)
2 | library(showtext)
3 | library(hrbrthemes)
4 |
5 |
6 | # Set the Stage ------------------------------------------------
7 |
8 | font <- "Righteous"
9 | font_add_google(font, font)
10 | showtext_auto(enable = TRUE)
11 | theme_set(theme_ipsum(base_family = font))
12 | bg <- "#F0EBE3"
13 | txt_col <- 'black'
14 |
15 |
16 | # read data-----------------------------------------------------
17 |
18 | sets <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-09-06/sets.csv.gz')
19 | themes <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-09-06/themes.csv.gz')
20 |
21 |
22 | # data wrangling-------------------------------------------------
23 |
24 | df <- sets |>
25 | left_join(themes |>
26 | select(id, theme_name=name),
27 | by=c(theme_id="id"))|>
28 | group_by(name, year, theme_name)|>
29 | summarise(n_sets=n(), num_parts)
30 |
31 | # plot ----------------------------------------------------------
32 |
33 | df |> ggplot(aes(year, n_sets, fill= 'orange' ))+
34 | geom_col()+
35 | labs(
36 | title = 'LEGO SETS',
37 | subtitle = "The number of sets is increasing.",
38 | caption = "Muhammad Azhar | #TidyTuesday Week36 | Data:LEGO database",
39 | y = "# of LEGO sets",
40 | x = " "
41 | )+
42 | theme(
43 | axis.title.x = element_blank(),
44 | axis.title.y = element_text(hjust = 0.5),
45 | plot.title = element_text(size=20, color=txt_col,
46 | face="bold", margin=margin(0,0,10,0)),
47 | plot.subtitle = element_text(size=14, color='grey60',
48 | face="bold", margin=margin(0,0,30,0)),
49 |
50 | plot.caption = element_text(hjust=.5, margin=margin(20,0,0,0),
51 | size=10, color='grey20', face="plain"),
52 | plot.background = element_rect(color=bg, fill=bg),
53 | plot.margin = margin(30,30,30,30),
54 | legend.position = 'hidden')
55 |
56 | # Save plot ----------------------------------------------------------
57 |
58 | showtext_opts(dpi = 320)
59 |
60 | ggsave("week_36.png", height = 8, width = 10, dpi=320)
61 |
62 | showtext_auto(FALSE)
63 |
--------------------------------------------------------------------------------
/2022/Week_41/week_41.R:
--------------------------------------------------------------------------------
1 | library(tidyverse)
2 | library(tidytuesdayR)
3 | library(showtext)
4 | library(MetBrewer)
5 |
6 | # Set the Stage ------------------------------------------------
7 |
8 | font <- "Satisfy"
9 | font_add_google(family=font, font,db_cache = FALSE)
10 | showtext_auto(enable = TRUE)
11 | theme_set(theme_void(base_family = font))
12 | bg <- "#F0EBE3"
13 | txt_col <- "black"
14 |
15 | # read data-----------------------------------------------------
16 |
17 | tt <- tt_load(2022, week=41)
18 | readme(tt)
19 | yarn <- tt$yarn
20 |
21 |
22 | # data wrangling-------------------------------------------------
23 |
24 | df <- yarn |>
25 | select(yarn_company_name, rating_average, rating_count, yarn_weight_name)|>
26 | drop_na()
27 |
28 | df <- df |>
29 | group_by(yarn_company_name, yarn_weight_name)|>
30 | summarise(average_rating = mean(rating_average),
31 | average_count = mean(rating_count))|>
32 | ungroup()
33 |
34 | # Text ----------------------
35 |
36 | title_text <- "Spooky Threads"
37 | caption_text <- "Muhammad Azhar | #TidyTuesday Week41 | Data: Ravelry.com"
38 |
39 |
40 |
41 | # plot ----------------------------------------------------------
42 |
43 | df |> ggplot(aes(x=average_count, y=average_rating, colour=yarn_weight_name))+
44 | geom_line(size=1) +
45 | scale_x_log10(limits =c(1,1e5)) +
46 | scale_colour_manual(values = met.brewer(name = "Derain", n = 15)) +
47 | coord_polar()+
48 |
49 | labs(title = title_text,
50 | caption = caption_text,
51 | x = " ",
52 | y = " ")+
53 | theme(
54 | legend.position = 'None',
55 |
56 | # Title
57 | plot.title = element_text(size=34,
58 | color=txt_col,
59 | lineheight=1,
60 | hjust=0.5,
61 | face="bold"),
62 | # Caption
63 | plot.caption = element_text(hjust=.5,
64 | size=12,
65 | color=txt_col,
66 | face="bold",
67 | margin=margin(10,0,0,0)),
68 | plot.margin = margin(20,20,20,20),
69 | plot.background = element_rect(color=bg, fill=bg))
70 |
71 | # Save plot ----------------------------------------------------------
72 |
73 | showtext_opts(dpi = 320)
74 |
75 | ggsave("week_41.png", height = 10, width = 10, dpi=320)
76 |
77 | showtext_auto(FALSE)
78 |
--------------------------------------------------------------------------------
/2022/Week_31/week_31.R:
--------------------------------------------------------------------------------
1 | library(tidytuesdayR)
2 | library(showtext)
3 | library(tidyverse)
4 | library(janitor)
5 | library(hrbrthemes)
6 | library(MetBrewer)
7 |
8 |
9 | # Set the Stage ------------------------------------------------
10 | font <- "Mouse Memoirs"
11 | font_add_google(font, font)
12 | showtext_auto(enable = TRUE)
13 | theme_set(theme_minimal(base_family = font))
14 | bg <- "#F0EBE3"
15 |
16 | # read data-----------------------------------------------------
17 | tt_data <- tt_load(2022, week=31)
18 | frogs <- tt_data$frogs |> clean_names()
19 |
20 |
21 | # data wrangling-------------------------------------------------
22 |
23 | df <- frogs |> group_by(hab_type, structure) |>
24 | summarise(count=n()) |>
25 | mutate(percent=round(count/sum(count)*100))
26 |
27 | # plot----------------------------------------------------------
28 |
29 | df|> ggplot()+
30 | geom_bar(aes(x=percent, y=hab_type, fill=structure), stat = "identity",
31 | position = "stack")+
32 | scale_x_continuous(expand = c(0,0))+
33 | scale_y_discrete(expand = c(0,0))+
34 | scale_fill_met_d(name = "Isfahan1", direction = -1)+
35 |
36 | labs(
37 | title = "Oregon Frogs prefer Herbaceous Vegetation",
38 | subtitle = "percentage of frogs spotted",
39 | caption = "Muhammad Azhar | #TidyTuesday Week 31 | Data: USGS") +
40 |
41 | coord_cartesian(clip="off") +
42 | theme(
43 | panel.grid = element_blank(),
44 | axis.title.x = element_blank(),
45 | axis.title.y = element_blank(),
46 | axis.text.x = element_text(size=12, color="black"),
47 | axis.text.y = element_text(size=18, color="black"),
48 | plot.title = element_text(size=30, face="bold",
49 | margin=margin(10,0,10,0)),
50 | plot.subtitle = element_text(size=20, color="grey50", face="bold",
51 | margin=margin(0,0,20,0)),
52 | plot.caption = element_text(size=14, color="black", face="plain", hjust=0.5,
53 | margin=margin(20,0,0,0)),
54 | plot.background = element_rect(color = bg, fill=bg),
55 | plot.margin = margin(30,30,30,30),
56 | legend.title = element_blank(),
57 | legend.text = element_text(size=12),
58 | legend.position = "top",
59 | legend.justification="left"
60 | )
61 |
62 | # Save plot ----------------------------------------------------------
63 |
64 | showtext_opts(dpi = 320)
65 |
66 | ggsave("week_31.png", height = 7, width = 7, dpi=320,)
67 |
68 | showtext_auto(FALSE)
69 |
--------------------------------------------------------------------------------
/2022/Week_39/week_39.R:
--------------------------------------------------------------------------------
1 | library(tidyverse)
2 | library(tidytuesdayR)
3 | library(showtext)
4 | library(usmap)
5 |
6 | # Set the Stage ------------------------------------------------
7 |
8 | font <- "Roboto"
9 | font_add_google(family=font, font,db_cache = FALSE)
10 | showtext_auto(enable = TRUE)
11 | theme_set(theme_minimal(base_family = font))
12 | bg <- "#F0EBE3"
13 | txt_col <- "black"
14 |
15 | # read data-----------------------------------------------------
16 |
17 | tt <- tt_load(2022, week=39)
18 | artists <- tt$artists
19 | # data wrangling-------------------------------------------------
20 |
21 | df <- artists |>
22 | filter(type=="Musicians")|>
23 | group_by(state)|>
24 | select(state, race, artists_n, artists_share, location_quotient)|>
25 | drop_na() |>
26 | ungroup()
27 |
28 | # plot ----------------------------------------------------------
29 |
30 | plot_usmap(data = df, values = "artists_n")+
31 | scale_fill_continuous(low = "#ecf39e", high = "#007200", limits=c(0,18000),
32 | name="# of Musicians",
33 | guide=guide_legend(
34 | keyheight = unit(3, units = "mm"),
35 | keywidth=unit(12, units = "mm"),
36 | label.position = "bottom",
37 | title.position = 'top', title.hjust=0.5, nrow=1))+
38 | labs( title = "Musicians in United States",
39 | subtitle = "California has more than 15000 musicians.",
40 | caption = "Muhammad Azhar | #TidyTuesday Week 39 | Data: arts.gov"
41 | )+
42 |
43 | theme(legend.position = 'bottom',
44 | legend.background = element_rect(color = bg, fill=bg),
45 | legend.justification = "center",
46 | plot.title = element_text(size=34, color=txt_col,lineheight=1,
47 | hjust=0.5,face="bold",
48 | margin=margin(10,0,10,0)),
49 | plot.subtitle = element_text(size=20, color="grey50", face='bold',
50 | hjust=0.5, margin=margin(0,0,15,0)),
51 | plot.caption = element_text(hjust=.5, margin=margin(10,0,0,0),
52 | size=12, color=txt_col, face="bold"),
53 | plot.margin = margin(20,20,20,20),
54 | plot.background = element_rect(color=bg, fill=bg))
55 |
56 | # Save plot ----------------------------------------------------------
57 |
58 | showtext_opts(dpi = 320)
59 |
60 | ggsave("week_39.png", height = 10, width = 10, dpi=320)
61 |
62 | showtext_auto(FALSE)
63 |
--------------------------------------------------------------------------------
/2022/Week_33/week_33.R:
--------------------------------------------------------------------------------
1 | library(tidytuesdayR)
2 | library(tidyverse)
3 | library(showtext)
4 | library(MetBrewer)
5 | library(hrbrthemes)
6 |
7 | # Set the Stage ------------------------------------------------
8 | font <- "Concert One"
9 | font_add_google(font, font)
10 | theme_set(theme_ipsum(base_family = font))
11 | showtext_auto(enable = TRUE)
12 | bg <- "#F0EBE3"
13 |
14 | # read data-----------------------------------------------------
15 | tt_data <- tt_load(2022, week=33)
16 | show_name = "Friends"
17 | characters <- tt_data$characters |> filter(uni_name==show_name)
18 | myer_briggs <- tt_data$myers_briggs |> filter(uni_name==show_name)
19 | stats <- tt_data$psych_stats |> filter(uni_name==show_name)
20 |
21 | traits <- c("romantic", "driven", "curious", "moody", "gossiping", "chatty")
22 |
23 | # data wrangling-------------------------------------------------
24 |
25 | df <- characters|>
26 | select(char_name=name)|>
27 | left_join(
28 | stats|>
29 | filter(personality %in% traits)|>
30 | select(char_name, personality, avg_rating)
31 | )
32 |
33 |
34 |
35 | # plot----------------------------------------------------------
36 |
37 | df |>ggplot(aes(y=as.factor(char_name), x=avg_rating, fill=char_name))+
38 | geom_col(width = 0.7)+
39 | facet_wrap(~personality)+
40 | scale_fill_met_d(name = "Veronese", direction = 1)+
41 | labs(
42 | title = "F.R.I.E.N.D.S",
43 | subtitle = "Personality traits of the main characters.",
44 | caption = "Muhammad Azhar | #TidyTuesday Week 33 | Data: openpsychometrics.org",
45 | x = "",
46 | y = "") +
47 | scale_x_continuous(expand = c(0,0))+
48 | scale_y_discrete(expand = c(0,0))+
49 | geom_text(
50 | aes(0, y = char_name, label = char_name),
51 | hjust = 0, nudge_x = 0.3, colour = "white", size = 3.5, fontface='bold')+
52 | theme(
53 | axis.ticks = element_blank(),
54 | axis.text.x = element_text(size=12, color="grey60", face='plain'),
55 | axis.text.y = element_blank(),
56 | axis.title.x = element_blank(),
57 | axis.title.y = element_blank(),
58 | plot.title = element_text(size=34, face="bold",
59 | margin=margin(10,0,5,0)),
60 | plot.subtitle = element_text(size=22, face="bold",color = "grey50",
61 | margin=margin(0,0,20,0)),
62 | plot.background = element_rect(color = bg, fill=bg),
63 | plot.margin = margin(30,50,30,50),
64 | plot.caption = element_text(size=12, color="black", face="plain", hjust=0.5,
65 | margin=margin(20,0,0,0)),
66 | strip.text = element_text(size = 18),
67 | legend.position = "none")
68 |
69 | # Save plot ----------------------------------------------------------
70 |
71 | showtext_opts(dpi = 320)
72 |
73 | ggsave("week_33.png", height = 8, width = 10, dpi=320)
74 |
75 | showtext_auto(FALSE)
76 |
--------------------------------------------------------------------------------
/2022/Week_29/week_29.R:
--------------------------------------------------------------------------------
1 | library(tidytuesdayR)
2 | library(showtext)
3 | library(tidyverse)
4 | library(janitor)
5 | library(glue)
6 | library(hrbrthemes)
7 | library(ggtext)
8 |
9 |
10 | #read data
11 | tt_data <- tt_load(2022, week=29)
12 | tech <- tt_data$technology
13 |
14 | codes <- read_csv("https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.csv") |>
15 | clean_names()
16 |
17 |
18 | #data cleaning
19 | technology <- tech |>
20 | left_join(
21 | codes |> select(iso3c=alpha_3, region, sub_region)
22 | )
23 |
24 | df <- technology|>
25 | filter(variable %in% c("steel_production", "steel_demand") & iso3c=='PAK' & year>1990)|>
26 | select(iso3c, year, label, value) |> group_by(year, label) |>
27 | rename(metric=label) |> ungroup()
28 |
29 | # Set the Stage
30 | font <- "Noto Serif"
31 | font_add_google(family=font, font)
32 | font_add_google("Roboto", "roboto")
33 | showtext_auto(enable = TRUE)
34 | theme_set(theme_minimal(base_family = font))
35 | bg <- "#F0EBE3"
36 | txt_col <- "black"
37 | col <- "#0F3D3E"
38 |
39 | #plot
40 | ggplot(df, aes(x=year, y=value, color=metric))+
41 | geom_line(aes(color=metric),size=1)+
42 | labs(
43 | title = glue("Steel Supply
44 | & Demand in Pakistan."),
45 | subtitle = "Steel production & demand measured in thousand metric tons,\n 1991-2019",
46 | caption = "Muhammad Azhar | #TidyTuesday Week 29 | Data: data.nber.org",
47 | y= "Thousand metric tons"
48 | ) +
49 | coord_cartesian(clip="off") +
50 | theme_ipsum_tw()+
51 | theme(
52 | panel.grid = element_blank(),
53 | axis.title.x = element_blank(),
54 | axis.title.y = element_text(color=txt_col, size=10),
55 | axis.text = element_text(color=txt_col, size=10),
56 | axis.line = element_line(),
57 | plot.title = element_markdown(size=20, color=txt_col, hjust=.5,lineheight=1,
58 | family = font,
59 | face="bold", margin=margin(0,0,10,0)),
60 | plot.subtitle = element_text(size=12, family="Roboto",
61 | color = "grey50",hjust=.5,
62 | lineheight=1, face = "plain",
63 | margin = margin(0,0,20,0)),
64 | plot.caption = element_text(hjust=.5, margin=margin(20,0,0,0), size=8,
65 | color=txt_col, face="plain"),
66 | plot.background = element_rect(color=bg, fill=bg),
67 | plot.title.position = "panel",
68 | plot.margin = margin(30,30,30,30),
69 | legend.position = "bottom",
70 | legend.title = element_blank(),
71 | legend.justification = "center",
72 | )
73 |
74 | #Save plot
75 | showtext_opts(dpi = 320)
76 |
77 | ggsave("week_29.png",
78 | height = 7,
79 | width = 10,
80 | dpi=320,
81 |
82 | )
83 |
84 | showtext_auto(FALSE)
85 |
--------------------------------------------------------------------------------
/2022/Week_38/week_38.R:
--------------------------------------------------------------------------------
1 | library(tidyverse)
2 | library(tidytuesdayR)
3 | library(showtext)
4 | library(maps)
5 |
6 | # Set the Stage ------------------------------------------------
7 |
8 | font <- "Mukta"
9 | font_add_google(family=font, font,db_cache = FALSE)
10 | showtext_auto(enable = TRUE)
11 | theme_set(theme_minimal(base_family = font))
12 | bg <- "#F0EBE3"
13 | txt_col <- "black"
14 |
15 | # read data-----------------------------------------------------
16 |
17 | tt <- tt_load(2022, week=38)
18 | hydro <- tt$HydroWASTE_v10
19 |
20 | # data wrangling-------------------------------------------------
21 |
22 | df <- hydro |>
23 | filter(CNTRY_ISO == "PAK") |>
24 | select(WASTE_ID, LAT_WWTP, LON_WWTP, LAT_OUT, LON_OUT, POP_SERVED, WASTE_DIS)
25 |
26 |
27 | map <- as_tibble(map_data("world")) |>
28 | filter(region =="Pakistan")
29 |
30 | # plot ----------------------------------------------------------
31 |
32 | map |>
33 | ggplot() +
34 | geom_polygon(aes(x=long, y=lat, group=group),fill="#7E9982", color="grey40", size=.1) +
35 | geom_point(data=df, aes(x=LON_WWTP, y=LAT_WWTP, size=POP_SERVED, fill="#002B5B"),
36 | color="black", shape=21, alpha=0.8) +
37 | scale_size_continuous(range = c(2,10),
38 | labels = scales::unit_format(unit = "M", scale = 1e-6))+
39 | coord_map(projection = "mercator", xlim=c(60,80)) +
40 | labs( title="Wastewater Plants in Pakistan",
41 | subtitle = "Pakistan has 25 wastewater plants, estimated to serve 23.5M people.",
42 | caption = "Muhammad Azhar | #TidyTuesday Week 38 | Data: Macedo et al (2022)")+
43 | theme(
44 | panel.grid = element_blank(),
45 | axis.title = element_blank(),
46 | axis.text = element_blank(),
47 | plot.title = element_text(size=40, color=txt_col, hjust=0.5,lineheight=1,
48 | face="bold", margin=margin(0,0,10,0)),
49 | plot.subtitle = element_text(size=20, color="grey30", face="bold", hjust=0.5,
50 | margin=margin(0,0,20,0)),
51 | plot.caption = element_text(size=12, color=txt_col,face = "plain",
52 | hjust=0.5, margin=margin(0,0,0,0),
53 | lineheight = 1.4),
54 | plot.background = element_rect(color=bg, fill=bg),
55 | plot.title.position = "panel",
56 | plot.margin = margin(20,20,20,20),
57 | legend.position ="bottom",
58 | legend.margin = margin(10,0,20,0),
59 | legend.title = element_text(size=12, hjust=0.5),
60 | legend.text = element_text(size=12, hjust=0.5)
61 | ) +
62 | guides(fill="none", size=guide_legend(title="Circle size represent the population served",
63 | title.position = "top",
64 | title.hjust = .5))
65 |
66 | # Save plot ----------------------------------------------------------
67 |
68 | showtext_opts(dpi = 320)
69 |
70 | ggsave("week_38.png", height = 10, width = 10, dpi=320)
71 |
72 | showtext_auto(FALSE)
73 |
--------------------------------------------------------------------------------
/2023/Week-10/week-10.R:
--------------------------------------------------------------------------------
1 | library(tidyverse)
2 | library(ggtext)
3 | library(ozmaps)
4 | library(ggrepel)
5 | library(showtext)
6 |
7 | # ------ Get Data ------
8 |
9 | numbats <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-03-07/numbats.csv')
10 |
11 | # ------ Data Wrangling ------
12 |
13 | df <- numbats |>
14 | select(recordID, year, month, wday, hour, day,
15 | decimalLatitude, decimalLongitude)|>
16 | filter(!is.na(year))
17 |
18 | df_count <- df |>
19 | group_by(year)|>
20 | summarise(n=n())|>
21 | arrange(year)|>
22 | ungroup()
23 |
24 | sf_oz <- ozmap_data("states")
25 | df_map <- df|>
26 | mutate(thresh=case_when(year<2020 ~ "Before 2020",
27 | year>=2020 & year!=2023 ~ "2020 to 2022",
28 | year == 2023 ~ "As of 2023"))|>
29 | select(decimalLatitude, decimalLongitude,thresh )
30 |
31 | # ------ Typography ------
32 |
33 | font_add_google("Outfit", "title_font")
34 | font_add_google("Roboto Condensed", "body_font")
35 | showtext_auto()
36 |
37 | title_font <- "title_font"
38 | body_font <- "body_font"
39 |
40 | # ------ Set theme ------
41 |
42 | bg <- "#FFFFFF"
43 |
44 | theme_set(theme_minimal(base_size = 19, base_family = "Roboto"))
45 | theme_update(
46 | text = element_text(color = "grey12"),
47 | axis.title = element_blank(),
48 | axis.text.x = element_text(family = body_font),
49 | axis.text.y = element_blank(),
50 | panel.grid.major.y = element_blank(),
51 | panel.grid.minor = element_blank(),
52 | plot.margin = margin(20, 5, 10, 10),
53 | plot.subtitle = element_textbox_simple(family = body_font, size = 14,
54 | lineheight = 1.6),
55 | plot.title.position = "plot",
56 | plot.caption = element_text(family = body_font, color = "grey12",
57 | hjust = .5, size = 10,
58 | margin = margin(35, 0, 0, 0))
59 | )
60 |
61 | # ------ Plot ------
62 | p1 <- ggplot(sf_oz) +
63 | geom_sf() +
64 | geom_point(data = df_map, aes(x=decimalLongitude, y=decimalLatitude),
65 | color='#b40059', size=1.5, alpha=0.8)+
66 | facet_wrap(~factor(thresh, levels = c("Before 2020", "2020 to 2022", "As of 2023")))+
67 | labs(title = "Where are Numbats found?",
68 | subtitle = 'Numbats were widely distributed across Southern Australia, from Western Australia to New South Wales. The species has survived only in two small patches of land in the Western Australia.',
69 | caption = "Graphic: Muhammad Azhar | Twitter: @imagineazhar | Data: Atlas of Living Australia")+
70 | theme(
71 | strip.text.x = element_text(family = title_font, face='bold',
72 | size = 13, colour = "grey10"),
73 | plot.title = element_markdown(family = title_font, face = 'bold',
74 | size = 28,color = "black",
75 | margin = margin(5, 35, 15, 35)),
76 | plot.subtitle = element_textbox_simple(margin = margin(5, 35, 15, 35)),
77 | panel.grid.major = element_blank(),
78 | axis.text.x = element_blank(),
79 | plot.background = element_rect(color=bg, fill=bg)
80 | )
81 | p1
82 |
83 |
84 | # ------ Save Plot ------
85 |
86 | showtext_opts(dpi = 320)
87 | ggsave("week-10.png",dpi=320,
88 | width = 10, height = 6)
89 | showtext_auto(FALSE)
90 |
91 |
--------------------------------------------------------------------------------
/2022/Week_26/week_26.R:
--------------------------------------------------------------------------------
1 | library(tidytuesdayR)
2 | library(tidyverse)
3 | library(janitor)
4 | library(showtext)
5 | library(MetBrewer)
6 |
7 | # Set the Stage ------------------------------------------------
8 | font <- "Mukta"
9 | font_add_google(font, font)
10 | theme_set(theme_minimal(base_family = font))
11 | showtext_auto(enable = TRUE)
12 | bg <- "#F0EBE3"
13 |
14 | # read data-----------------------------------------------------
15 | tt_data <- tt_load(2022, week=26)
16 | paygap <- tt_data$paygap |> clean_names()
17 |
18 |
19 | # data wrangling-------------------------------------------------
20 |
21 | df <- paygap |> filter(substr(due_date,1,4)=="2022")
22 | # employer_size=="20,000 or more")|>
23 | # select(employer_size, diff_mean_hourly_percent,diff_mean_bonus_percent)
24 |
25 |
26 | # plot----------------------------------------------------------
27 |
28 | df |> ggplot( aes(diff_mean_hourly_percent, fct_rev(employer_size),
29 | color=employer_size))+
30 | ggridges::stat_density_ridges(
31 | aes(fill = stat(quantile)),
32 | geom = "density_ridges_gradient",
33 | quantile_lines = TRUE, quantiles = 4,
34 | color = "black", alpha = .8, size = 0.8
35 | ) +
36 | scale_fill_met_d(name = "Morgenstern", direction = 1)+
37 | scale_x_continuous(expand = c(0,-1), limits = c(-105, 105)) +
38 | scale_y_discrete( expand = c(0,0), limits=c('Not Provided',
39 | 'Less than 250', '250 to 499',
40 | '500 to 999', '1000 to 4999',
41 | '5000 to 19,999',
42 | '20,000 or more'))+
43 | labs(
44 | title = "UK Gender Pay Gap",
45 | subtitle = "Mean % difference between male and female hourly pay.
46 | (negative = women's mean hourly pay is higher) ",
47 | caption = "Muhammad Azhar | #TidyTuesday Week 26 | Data: ONS.gov.uk",
48 | x = "Mean % difference hourly pay",
49 | y = "Compnay Size --->") +
50 |
51 | coord_cartesian(clip="off") +
52 | theme(
53 | panel.grid = element_blank(),
54 | axis.title.x = element_text (size=11, color="grey20",
55 | margin = margin(10,0,0,0) ),
56 | axis.title.y = element_text (size=11, color="grey20", hjust=0.4,
57 | margin = margin(r=5)),
58 | axis.text.x = element_text(size=11, color="black"),
59 | axis.text.y = element_text(size=11, color="black"),
60 |
61 | plot.title = element_text(size=24, face="bold",
62 | margin=margin(10,0,10,0)),
63 | plot.title.position = 'plot',
64 | plot.subtitle = element_text(size=16, color="grey50", face="plain",
65 | margin=margin(0,0,10,0)),
66 | plot.caption = element_text(size=11, color="black", face="plain", hjust=0.5,
67 | margin=margin(20,0,0,0)),
68 | plot.background = element_rect(color = bg, fill=bg),
69 | plot.margin = margin(30,30,30,30),
70 | legend.title = element_text("Quantiles"),
71 | legend.text = element_text(size=12),
72 | legend.position = "top",
73 | legend.justification="right"
74 | )
75 |
76 | # Save plot ----------------------------------------------------------
77 |
78 | showtext_opts(dpi = 320)
79 |
80 | ggsave("week_26.png", height = 7, width = 8, dpi=320)
81 |
82 | showtext_auto(FALSE)
83 |
--------------------------------------------------------------------------------
/new_week.R:
--------------------------------------------------------------------------------
1 | new_week <- function() {
2 |
3 | # Get today's date and format it
4 | todays_date <- format(Sys.Date(), "%Y-%m-%d")
5 |
6 | # Set the base path for file creation
7 | base_path <- "D:/Projects/TidyTuesday/2024"
8 |
9 | # Check if the folder already exists and delete it if so
10 | full_folder_path <- file.path(base_path, todays_date)
11 | if (dir.exists(full_folder_path)) {
12 | unlink(full_folder_path, recursive = TRUE) # Delete existing folder and contents
13 | }
14 |
15 | # Create the folder with today's date in the specified path
16 | dir.create(full_folder_path)
17 |
18 | # Get the year and week number
19 | current_year <- format(Sys.Date(), "%Y")
20 | week_number <- strftime(Sys.Date(), "%V")
21 |
22 | # Create the README.md file with Markdown content
23 | file_path <- file.path(full_folder_path, "README.md")
24 | cat(paste("#", current_year, "\n"), file = file_path)
25 | cat(paste("# Week Number:", week_number), file = file_path, append = TRUE)
26 |
27 | # Create the R script file with the provided code snippet
28 | r_script_path <- file.path(full_folder_path, paste0(todays_date, ".R"))
29 | cat(paste("library(ggplot2)\n",
30 | "library(showtext)\n",
31 | "library(tidyverse)\n",
32 | "library(ggtext)\n\n",
33 | "# ------ Get Data ------\n\n",
34 | "# ------ Data Wrangling ------\n\n",
35 | "# ------ Typography ------\n\n",
36 | "font_add_google(\"Outfit\", \"title_font\")\n",
37 | "font_add_google(\"Outfit\", \"body_font\")\n",
38 | "showtext_auto()\n\n",
39 | "title_font <- \"title_font\"\n",
40 | "body_font <- \"body_font\"\n\n",
41 | "# ------ Texts ------\n\n",
42 | "title_text <- \" \"\n",
43 | "subtitle_text <- \"\"\n",
44 | "caption_text <- \"Graphic: Muhammad Azhar Data: \"\n\n",
45 | "# ------ Plot ------\n\n",
46 | "ggplot(df, aes()) +\n\n\n",
47 | "labs(title = title_text,\n",
48 | "subtitle = subtitle_text,\n",
49 | "caption = caption_text)+ \n",
50 | "theme_minimal()+\n",
51 | "theme(\n",
52 | "axis.title.x = element_text(hjust = 0.32, color = \"grey30\"),\n",
53 | "axis.title.y = element_blank(),\n",
54 | "axis.text.x = element_text(family = body_font,\n",
55 | "face = \"bold\",\n",
56 | "size=12),\n",
57 | "axis.text.y = element_text(family = body_font,\n",
58 | "face = \"bold\",\n",
59 | "size=12),\n",
60 | "\n",
61 | "# Legend\n",
62 | "legend.position = \"none\",\n",
63 | "\n",
64 | "# TITLE\n",
65 | "plot.title.position = \"plot\",\n",
66 | "plot.title = element_textbox(margin = margin(20, 0, 5, 0),\n",
67 | "size = 24,\n",
68 | "family = title_font,\n",
69 | "face = \"bold\",\n",
70 | "width = unit(75, \"lines\")),\n\n",
71 | "# SUB-TITLE\n",
72 | "plot.subtitle = element_textbox(margin = margin(5, 0, 30, 0),\n",
73 | "size = 16,\n",
74 | "family = body_font,\n",
75 | "face = \"bold\",\n",
76 | "width = unit(75, \"lines\")),\n\n",
77 | "plot.background = element_rect(color=, fill=),\n",
78 | "plot.margin = margin(20, 20, 20, 20)))\n"
79 | ),
80 | file = r_script_path)
81 | }
82 |
--------------------------------------------------------------------------------
/2022/Week_37/week_37.R:
--------------------------------------------------------------------------------
1 | library(tidyverse)
2 | library(tidytuesdayR)
3 | library(showtext)
4 | library(hrbrthemes)
5 | library(patchwork)
6 | library(lubridate)
7 | library(ggtext)
8 |
9 | # Set the Stage ------------------------------------------------
10 |
11 | font <- "Special Elite"
12 | font_add_google(font, font)
13 | showtext_auto(enable = TRUE)
14 | theme_set(theme_ipsum(base_family = font))
15 | bg <- "#F0EBE3"
16 | txt_col <- "black"
17 |
18 |
19 |
20 | # read data-----------------------------------------------------
21 |
22 | tt <- tt_load(2022, week=37)
23 | bigfoot <- tt$bigfoot
24 |
25 | # data wrangling-------------------------------------------------
26 |
27 | df <- bigfoot|>
28 | select(county, state, season, latitude, longitude, date, classification)|>
29 | mutate(year=year(date)) |>
30 | filter(year >= 1950)|>
31 | mutate(season = na_if(season, "Unknown"))
32 |
33 |
34 | # plot 1 ----------------------------------------------------------
35 |
36 | p1 <- df|> count(year)|> ggplot(aes(x=year, y=n))+
37 | geom_line(size=1.2, color='#636175') +
38 | geom_point(size=1.3, color='#636175')+
39 | coord_cartesian(clip="off")+
40 | labs(
41 | title = 'Bigfoot sightings over the years',
42 | subtitle = "2004 onwards Bigfoot Sightings have decreased significantly.",
43 | y = "# of sightings",
44 | x = " ")+
45 | theme(
46 | axis.title.x = element_blank(),
47 | axis.title.y = element_text(hjust = 0.5),
48 | plot.title = element_text(size=20, color=txt_col,
49 | face="bold", margin=margin(0,0,10,0)),
50 | plot.subtitle = element_text(size=12, color='grey50',
51 | face="bold", margin=margin(0,0,30,0)),
52 |
53 | plot.caption = element_text(hjust=.5, margin=margin(20,0,0,0),
54 | size=10, color='grey20', face="plain"),
55 | plot.background = element_rect(color=bg, fill=bg),
56 | plot.title.position = "plot",
57 | legend.position = 'hidden')
58 | p1
59 |
60 | # plot 2 ----------------------------------------------------------
61 |
62 | top_5 <- df |> count(state, sort = TRUE)|>
63 | head(5) |>
64 | ggplot(aes(n, y= reorder(state, n)))+
65 | geom_col(width = 0.5, fill='#636175')+
66 | scale_y_discrete()+
67 | labs(title = "Top 5 states with most Sightings")+
68 | theme(
69 | panel.grid = element_blank(),
70 | axis.title.x = element_blank(),
71 | axis.title.y = element_blank(),
72 | axis.text.y = element_text(size=10, color='grey50'),
73 | plot.title = element_text(size=11, color=txt_col,
74 | face="bold", margin=margin(0,0,5,0)),
75 | plot.background = element_rect(color=bg, fill=bg),
76 | plot.title.position = "plot",
77 | panel.border = element_rect(colour = "black", fill=NA, size=1),
78 | legend.position = 'hidden')
79 |
80 | # Patchwork ----------------------------------------------------------
81 |
82 | p1 + inset_element( top_5,
83 | left = 0.1, bottom = 0.4,
84 | right = 0.6, top = 1,
85 | align_to = 'panel')+
86 | plot_annotation(caption = "Muhammad Azhar | #TidyTuesday Week 37 | Data: Data.World/BFRO") &
87 | theme(plot.caption = element_text(size=12, color=txt_col,face = "plain", hjust=0.5,
88 | margin=margin(0,0,0,0), lineheight = 1.4),
89 | plot.margin = margin(30,30,30,30),
90 | plot.background = element_rect(color=bg, fill=bg))
91 |
92 | # Save plot ----------------------------------------------------------
93 |
94 | showtext_opts(dpi = 320)
95 |
96 | ggsave("week_37.png", height = 8, width = 10, dpi=320)
97 |
98 | showtext_auto(FALSE)
99 |
--------------------------------------------------------------------------------
/2024/2024-03-09/2024-03-09.R:
--------------------------------------------------------------------------------
1 | library(ggplot2)
2 | library(showtext)
3 | library(tidyverse)
4 | library(ggtext)
5 | library(camcorder)
6 | library(glue)
7 |
8 | gg_record(dir = "tidytuesday-temp", device = "png", width = 8, height = 10, units = "in", dpi = 320)
9 |
10 | # ------ Get Data ------
11 |
12 | trashwheel <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-03-05/trashwheel.csv')
13 |
14 |
15 | # ------ Data Wrangling ------
16 |
17 | df <- trashwheel|>
18 | filter(Year==2023)|>
19 | select(Name, PlasticBottles:CigaretteButts,
20 | PlasticBags, Wrappers)|>
21 | pivot_longer(cols = PlasticBottles:Wrappers,
22 | names_to = "trash_type",
23 | values_to = "num_items")|>
24 | filter(num_items>0)|>
25 | group_by(Name, trash_type)|>
26 | summarise(total=sum(num_items))|>
27 | mutate(col_id = row_number(),
28 | fraction = total/sum(total),
29 | ymax = cumsum(fraction),
30 | ymin = c(0, head(ymax, n=-1)),
31 | label_pos = (ymax + ymin)/2,
32 | label = paste0(round(fraction*100, digits = 0),"%")) |>
33 | ungroup()
34 |
35 | # ------ Typography ------
36 |
37 | font_add_google("Outfit", "title_font")
38 | font_add_google("Chivo", "body_font")
39 | showtext_auto()
40 |
41 | title_font <- "title_font"
42 | body_font <- "body_font"
43 |
44 | # ------ Texts ------
45 |
46 | title_text <- glue(" The Cigarette Butt : Baltimore's Trash Wheels' Most Prevalent Catch in 2023")
47 | subtitle_text <- glue("Highlighting the Percentage Distribution of Collected Item Types")
48 | caption_text <- glue("Graphic: Muhammad Azhar | Data: mrtrashwheel.com")
49 |
50 | # ------ Plot ------
51 | ggplot(df, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=trash_type)) +
52 | geom_rect() +
53 | geom_text( x=3.5, aes(y=label_pos, label=label), size=9) +
54 | scale_y_continuous()+
55 | scale_fill_manual(values =c("#ef476f", "#e9ecef", "#6c757d","#adb5bd", "#495057" ))+
56 | coord_polar(theta="y") +
57 | xlim(c(1.5, 4)) +
58 | facet_wrap(vars(Name))+
59 | labs(title = title_text, subtitle = subtitle_text, caption = caption_text)+
60 | theme_minimal()+
61 | theme(
62 | axis.title.x = element_blank(),
63 | axis.title.y = element_blank(),
64 | axis.text.x = element_blank(),
65 | axis.text.y = element_blank(),
66 |
67 | strip.text = element_text(family=title_font, size = 24,
68 | face = "bold", color = "grey10"),
69 |
70 | # Legend
71 | legend.position = "top",
72 | legend.title = element_blank(),
73 | legend.spacing = unit(0.5, 'cm'),
74 | legend.key.height= unit(0.5, 'cm'),
75 | legend.key.width= unit(0.7, 'cm'),
76 | legend.text = element_text(family = body_font,
77 | size=20,
78 | face = 'plain',
79 | color = "grey10"),
80 | # TITLE
81 | plot.title.position = "plot",
82 | plot.title = element_textbox(margin = margin(20, 0, 5, 0), size = 40,
83 | family = title_font, face = "bold",
84 | width = unit(75, "lines")),
85 |
86 | # SUB-TITLE
87 | plot.subtitle = element_textbox(margin = margin(5, 0, 30, 0), size = 34,
88 | family = body_font, face = "plain",
89 | colour = "grey20",
90 | width = unit(75, "lines")),
91 |
92 | plot.caption = element_text(family = body_font, face = 'plain',
93 | size = 24, colour = 'grey30', hjust = 0.5),
94 |
95 | plot.background = element_rect(color="white", fill="white"),
96 | plot.margin = margin(20, 40, 20, 40))
97 |
--------------------------------------------------------------------------------
/2023/Week-39/week-39.R:
--------------------------------------------------------------------------------
1 | library(tidyverse)
2 | library(ggtext)
3 | library(ggrepel)
4 | library(showtext)
5 |
6 | # ------ Get Data ------
7 |
8 | richmondway <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-09-26/richmondway.csv')|>
9 | janitor::clean_names()
10 |
11 | <<<<<<< HEAD
12 | =======
13 |
14 | # ------ Data Wrangling ------
15 |
16 | richmondway$season <- factor(data$x, levels=rev(levels(richmondway$season)))
17 |
18 |
19 | >>>>>>> 9e214cfc35b441eece28f9e12cdccce6f3dcf79a
20 | # ------ Typography ------
21 |
22 | font_add_google("Outfit", "title_font")
23 | font_add_google("Roboto Condensed", "body_font")
24 | showtext_auto()
25 |
26 | title_font <- "title_font"
27 | body_font <- "body_font"
28 |
29 | # ------ Set theme ------
30 |
31 | bg <- "#FFFFFF"
32 |
33 | # ------ Plot ------
34 | richmondway|> ggplot(aes(y=season, x=as.factor(episode), label = f_count_rk,
35 | fill=f_count_rk)) +
36 | geom_tile(color="white") +
37 | <<<<<<< HEAD
38 | geom_text(aes(label=f_count_rk), color="grey50")+
39 | =======
40 | geom_text(aes(label=f_count_rk))+
41 | >>>>>>> 9e214cfc35b441eece28f9e12cdccce6f3dcf79a
42 | scale_fill_gradient(low = '#9bf6ff', high = "#ff6b6b") +
43 | scale_y_reverse(breaks = 1:12) +
44 | coord_equal()+
45 | theme_minimal()+
46 | labs(title = "FUCKS GIVEN!",
47 | <<<<<<< HEAD
48 | subtitle = "Roy Kent is a prominent character in the Apple TV+ series 'Ted Lasso'.\nThis visualization depicts the number of times Roy Kent uses the word 'F**K' in each episode.",
49 | x = "EPISODE",
50 | =======
51 | subtitle = "BY ROY KENT IN EACH EPISODE.",
52 | x= "EPISODE",
53 | >>>>>>> 9e214cfc35b441eece28f9e12cdccce6f3dcf79a
54 | y = "SEASON",
55 | caption = "Graphic: Muhammad Azhar | Twitter: @imagineazhar | Data:richmondway {R package}")+
56 | theme(
57 |
58 | legend.position = "none",
59 | <<<<<<< HEAD
60 | plot.title = element_text(family = title_font, face = 'bold',
61 | size = 28,color = "black",
62 | margin = margin(0, 0, 5, 0)),
63 | plot.subtitle = element_text(family = body_font, color="grey30",
64 | size=14, face="plain",
65 | =======
66 | plot.title = element_markdown(family = title_font, face = 'bold',
67 | size = 28,color = "black",
68 | margin = margin(0, 0, 5, 0)),
69 | plot.subtitle = element_textbox_simple(family = body_font, color="grey50",
70 | size=16, face="bold",
71 | >>>>>>> 9e214cfc35b441eece28f9e12cdccce6f3dcf79a
72 | margin = margin(5, 0, 10, 0)),
73 | plot.caption = element_text(family = body_font, color = "grey12",
74 | hjust = .5, size = 10,
75 | margin = margin(35, 0, 0, 0)),
76 | axis.text = element_text(family = body_font, color = "grey30", size = 10,
77 | margin = margin(0, 2, 2, 0), face = "bold"),
78 | axis.title = element_text(family = body_font, margin = margin(5, 5, 5, 5),
79 | size = 12, color = "grey30", face = "bold"),
80 | panel.grid.major = element_blank(),
81 | panel.grid.minor = element_blank(),
82 | plot.background = element_rect(color=bg, fill=bg),
83 | <<<<<<< HEAD
84 | plot.margin = margin(30, 10, 30, 10)
85 | =======
86 | plot.margin = margin(50, 10, 50, 10)
87 | >>>>>>> 9e214cfc35b441eece28f9e12cdccce6f3dcf79a
88 | )
89 |
90 |
91 |
92 | # ------ Save Plot ------
93 |
94 | showtext_opts(dpi = 320)
95 | ggsave("week-39.png",dpi=320,
96 | width = 10, height = 6)
97 | showtext_auto(FALSE)
98 |
99 |
--------------------------------------------------------------------------------
/2024/2024-02-24/2024-02-24.R:
--------------------------------------------------------------------------------
1 | library(ggplot2)
2 | library(showtext)
3 | library(forcats)
4 | library(tidyverse)
5 | library(ggtext)
6 |
7 | # ------ Get Data ------
8 |
9 | isc_grants <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-02-20/isc_grants.csv')
10 |
11 | # ------ Data Wrangling ------
12 |
13 | df <- isc_grants|>
14 | filter(year==2023)|>
15 | arrange(desc(funded))
16 | df$label_text <- paste(df$title, "(",df$proposed_by,")")
17 |
18 | # ------ Typography ------
19 |
20 | font_add_google("Outfit", "title_font")
21 | font_add_google("Outfit", "body_font")
22 | showtext_auto()
23 |
24 | title_font <- "title_font"
25 | body_font <- "body_font"
26 |
27 | # ------ Texts ------
28 |
29 | title_text <- "R Consortium\nInfrastructure Steering Committee (ISC) Grant Program 2023"
30 | subtitle_text <- "ISC funds projects contributing to the R community’s technical and social infrastructures."
31 | caption_text <- "Graphic: Muhammad Azhar | Data: R Consortium"
32 |
33 | # ------ Plot ------
34 |
35 | ggplot(df, aes(x=funded, y=title))+
36 | geom_col(aes(fill = proposed_by == "Jon Harmon"), width = 0.8) +
37 | facet_wrap(~ reorder(label_text, -funded), ncol = 1, scales = "free_y")+
38 | scale_x_continuous( name = "Funded Amount", expand = c(0, 0),
39 | labels = scales::label_currency(prefix = "$", scale_cut=c(0, k=1e3)))+
40 | scale_y_discrete(guide = "none", expand = expansion(add = c(.8, .6))) +
41 | scale_fill_manual(values = c("grey50", "#525CEB"), guide = "none") +
42 | geom_text(aes(label = paste0(funded, "$", " "), color=funded>15000,
43 | hjust=1.1),
44 | size = 4,
45 | fontface = "bold",
46 | family = body_font) +
47 | scale_x_continuous(guide = "none", name = NULL, expand = c(0, 0))+
48 | scale_color_manual(values = c("black", "white"), guide = "none")+
49 | labs(title = title_text,
50 | subtitle = subtitle_text,
51 | caption = caption_text)+
52 | theme_void()+
53 | theme(
54 | strip.text = element_text(hjust = 0, margin = margin(1, 0, 0.5, 0),
55 | size = rel(1.1), face = "plain",
56 | color = "grey10",
57 | family = body_font),
58 | axis.title.x = element_blank(),
59 | axis.title.y = element_blank(),
60 | axis.text.x = element_text(family = body_font,
61 | face = "bold",
62 | size=12),
63 | axis.text.y = element_text(family = body_font,
64 | face = "bold",
65 | size=12),
66 | # Legend
67 | legend.position = "none",
68 |
69 | # TITLE
70 | plot.title.position = "plot",
71 | plot.title = element_textbox(margin = margin(20, 0, 5, 0),
72 | size = 20,
73 | family = title_font,
74 | face = "bold",
75 | width = unit(33, "lines")),
76 | # SUB-TITLE
77 | plot.subtitle = element_textbox(margin = margin(5, 0, 30, 0),
78 | size = 14,
79 | family = body_font,
80 | face = "plain",
81 | width = unit(35, "lines")),
82 | # Caption
83 | plot.caption = element_text(family=body_font,
84 | face="plain",
85 | size=12,
86 | color="grey50",
87 | margin=margin(10,0,0,0)),
88 | plot.background = element_rect(color="white", fill="white"),
89 | plot.margin = margin(30, 50, 30, 50))
90 |
91 | # ------ Save Plot ------
92 |
93 | showtext_opts(dpi = 320)
94 | ggsave("Week08.png", height = 10,
95 | width = 8, dpi=320)
96 | showtext_auto(FALSE)
97 |
--------------------------------------------------------------------------------
/2023/Week-03/week-03.R:
--------------------------------------------------------------------------------
1 | library(tidyverse)
2 | library(tidytuesdayR)
3 | library(ggtext)
4 | library(MetBrewer)
5 | library(showtext)
6 |
7 | # ------ Get Data ------
8 |
9 | tt_data <- tt_load(2023, week=03)
10 | readme(tt_data)
11 | artists <- tt_data$artists
12 |
13 | # ------ Data Wrangling ------
14 |
15 | nations <- artists|>
16 | filter(book=="Gardner")|>
17 | filter(artist_nationality_other!="NA")
18 |
19 | df <- nations|> count(year, artist_nationality_other)|>
20 | rename(artist_nation=artist_nationality_other)
21 |
22 | # ------ Typography ------
23 |
24 | font_add_google("PT Serif", "title_font")
25 | font_add_google("Chivo", "body_font")
26 | showtext_auto()
27 |
28 | title_font <- "title_font"
29 | body_font <- "body_font"
30 |
31 | # ------ Set theme ------
32 |
33 | bg <- "#F0EBE3"
34 | txt_col <- "black"
35 |
36 | # ------ Texts ------
37 |
38 | title_text <- "The Nationality of the artist in Gardner Books"
39 | subtitle_text <- "This visualization provides an overview of the percentage of artists from different nationalities published by Gardner from 1926 to 2020. The percentage of American artists published by Gardner has increased significantly since the 1940s, while the percentage of British artists has remained relatively stable."
40 | caption_text <- "Muhammad Azhar Twitter: @imagineazhar Data: {arthistory}"
41 |
42 |
43 | # ------ Plot ------
44 |
45 | df |> ggplot(aes(fill=artist_nation, y=n, x=as.factor(year)))+
46 | geom_bar(position = "fill", stat = "identity", width = 0.85)+
47 | scale_y_continuous(labels = scales::percent)+
48 | scale_fill_manual(values=met.brewer("Demuth", 6))+
49 | guides(fill = guide_legend(nrow = 1,
50 | label.position = "top"))+
51 | labs(title = title_text,
52 | subtitle = subtitle_text,
53 | caption = caption_text,
54 | x="",
55 | y="")+
56 | theme_minimal()+
57 | theme(
58 | axis.text.x = element_text(family = body_font,
59 | size = 12,
60 | face = 'plain',
61 | color = txt_col),
62 |
63 | # Legend
64 | legend.position = "bottom",
65 | legend.title = element_blank(),
66 | legend.key.height= unit(0.5, 'cm'),
67 | legend.key.width= unit(2, 'cm'),
68 | legend.text = element_text(family = body_font,
69 | size=12,
70 | face = 'plain',
71 | color = txt_col),
72 | # TITLE
73 | plot.title.position = "plot",
74 | plot.title = element_text(family = title_font,
75 | face = "bold",
76 | color = txt_col,
77 | size = 40,
78 | lineheight = 1,
79 | margin = margin(20,0,10,0)),
80 | # Subtitle
81 | plot.subtitle = element_textbox(family=body_font,
82 | face = "plain",
83 | width = unit(65, "lines"),
84 | size = 16,
85 | color = "grey20",
86 | lineheight = 1,
87 | margin = margin(0,0,10,0)),
88 | # Caption
89 | plot.caption = element_text(family=body_font,
90 | face="plain",
91 | size=12,
92 | color="grey40",
93 | hjust=.5,
94 | margin=margin(20,0,0,0)),
95 |
96 | plot.background = element_rect(color=bg, fill=bg),
97 | plot.margin = margin(30,100,30,100)
98 | )
99 |
100 | # ------ Save Plot ------
101 |
102 | showtext_opts(dpi = 320)
103 | ggsave("week-03.png", height = 10,
104 | width = 16, dpi=320)
105 | showtext_auto(FALSE)
106 |
--------------------------------------------------------------------------------
/2024/week04/uk_education.R:
--------------------------------------------------------------------------------
1 | library(ggplot2)
2 | library(showtext)
3 | library(tidyverse)
4 | library(ggtext)
5 |
6 |
7 | # ------ Get Data ------
8 |
9 | tt_data <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-01-23/english_education.csv')
10 |
11 |
12 | # ------ Data Wrangling ------
13 | df <- tt_data|>
14 | filter(!size_flag %in% c("Not BUA", "Other Small BUAs")) |>
15 | mutate(
16 | size = case_when(
17 | str_detect(tolower(size_flag), "london") ~ "Inner and outer London",
18 | size_flag == "City" ~ "Cities (excluding London)",
19 | TRUE ~ size_flag
20 | ),
21 | income = case_when(
22 | str_detect(income_flag, "deprivation") ~ income_flag,
23 | TRUE ~ NA
24 | )
25 | ) |>
26 | mutate(
27 | size = fct_inorder(size),
28 | income = fct_relevel(income,
29 | "Higher deprivation towns",
30 | "Mid deprivation towns",
31 | "Lower deprivation towns",
32 | )
33 | )
34 |
35 | # ------ Typography ------
36 |
37 | font_add_google("Outfit", "title_font")
38 | font_add_google("Outfit", "body_font")
39 | showtext_auto()
40 |
41 | title_font <- "title_font"
42 | body_font <- "body_font"
43 |
44 | ## custom colors
45 | my_pal <- rcartocolor::carto_pal(n = 8, name = "Bold")
46 |
47 | # ------ Texts ------
48 |
49 | title_text <- "North East of England has the lowest education attainment score. "
50 | subtitle_text <- "Educational attainment score by region. This score estimates your odds of acheiving a higher educational degree."
51 | caption_text <- "Graphic: Muhammad Azhar Data: UK Office of National Statistics. "
52 |
53 |
54 | # ------ Plot ------
55 |
56 | ggplot(df, aes(education_score, fct_rev(rgn11nm))) +
57 | coord_cartesian(clip = "off") +
58 | scale_y_discrete(expand = c(.07, .07)) +
59 | scale_color_manual(values = my_pal, guide = "none") +
60 | scale_fill_manual(values = my_pal, guide = "none") +
61 | ggridges::stat_density_ridges(
62 | quantile_lines = TRUE, quantiles = 2,
63 | rel_min_height = 0.01, fill = "#92C7CF", alpha = .8, size = 1.5) +
64 | labs(title = title_text,
65 | subtitle = subtitle_text,
66 | x = paste0("← Lower attainment", strrep(" ", 30), "Educational attainment index score", strrep(" ", 30), "Higher attainment →"),
67 | caption = caption_text)+
68 | theme_minimal()+
69 | theme(
70 | axis.title.x = element_text(hjust = 0.32, color = "grey30"),
71 | axis.title.y = element_blank(),
72 | axis.text.x = element_text(family = body_font,
73 | face = "bold",
74 | size=12),
75 | axis.text.y = element_text(family = body_font,
76 | face = "bold",
77 | size=12),
78 |
79 | # Legend
80 | legend.position = "none",
81 |
82 | # TITLE
83 | plot.title.position = "plot",
84 | plot.title = element_textbox(margin = margin(20, 0, 5, 0),
85 | size = 24,
86 | family = title_font,
87 | face = "bold",
88 | width = unit(75, "lines")),
89 | # SUB-TITLE
90 | plot.subtitle = element_textbox(margin = margin(5, 0, 30, 0),
91 | size = 14,
92 | color = "grey30",
93 | family = body_font,
94 | width = unit(70, "lines")),
95 |
96 | # Caption
97 | plot.caption = element_text(family=body_font,
98 | face="plain",
99 | size=9,
100 | color="grey",
101 | margin=margin(10,0,0,0)),
102 |
103 | plot.background = element_rect(color="#FBF9F1", fill="#FBF9F1"),
104 | plot.margin = margin(20, 20, 20, 20)
105 | )
106 |
107 | # ------ Save Plot ------
108 |
109 | showtext_opts(dpi = 320)
110 | ggsave("week-04.png", height = 8,
111 | width = 12, dpi=320)
112 | showtext_auto(FALSE)
113 |
--------------------------------------------------------------------------------
/2023/Week-08/week-08.R:
--------------------------------------------------------------------------------
1 | library(tidyverse)
2 | library(tidytuesdayR)
3 | library(ggtext)
4 | library(showtext)
5 |
6 | # ------ Get Data ------
7 |
8 | tt_data <- tt_load(2023, week=08)
9 | readme(tt_data)
10 | bob_ross <- tt_data$bob_ross
11 |
12 | # ------ Data Wrangling ------
13 | df <- bob_ross|>
14 | select(painting_index, painting_title,
15 | season, episode, num_colors,
16 | color_hex)
17 | # df$season_string <- paste("season", bob_ross$season, sep = " ")
18 |
19 | # ------ Typography ------
20 |
21 | font_add_google("Catamaran", "title_font")
22 | font_add_google("Chivo", "body_font")
23 | showtext_auto()
24 |
25 | title_font <- "title_font"
26 | body_font <- "body_font"
27 |
28 | # ------ Set theme ------
29 |
30 | bg <- "#F0EBE3"
31 | txt_col <- "black"
32 |
33 | # ------ Texts ------
34 |
35 | title_text <- "Colors of Bob Ross"
36 | subtitle_text <- "This visualization presents the number of colors used in each episode of the television series, The Joy of Painting, hosted by Bob Ross. The show aired for 31 seasons, running from 1983 to 1994. The x-axis represents the episode number, while the y-axis shows the number of colors used, depicted as the height of each bar."
37 | caption_text <- "Created by: Muhammad Azhar Twitter: @imagineazhar"
38 |
39 |
40 | # ------ Plot ------
41 |
42 | df |> ggplot(aes(x=as.factor(episode), y=as.integer(num_colors),))+
43 | geom_bar(stat="identity", fill='#7286D3', width = 0.8)+
44 | geom_text(aes(label = num_colors), vjust = 1.5, colour = "black")+
45 | facet_wrap(vars(season),
46 | ncol = 4,
47 | labeller = labeller(season = function(x) {ifelse(x >= 1, paste("Season", x), " ")}))+
48 | scale_x_discrete()+
49 | scale_y_continuous()+
50 | labs(title = title_text,
51 | subtitle = subtitle_text,
52 | caption = caption_text,
53 | x="",
54 | y="")+
55 | ggthemes::theme_tufte()+
56 | theme(
57 | panel.spacing = unit(1.2, "lines"),
58 | strip.text.x = element_text(size = 14,
59 | family = body_font,
60 | face= 'bold',
61 | color = 'grey25'),
62 | axis.text.x = element_text(family = body_font,
63 | size = 9,
64 | face = 'plain',
65 | color = 'grey30'),
66 | axis.text.y = element_blank(),
67 | axis.ticks.y = element_blank(),
68 | # Legend
69 | legend.position = "bottom",
70 | legend.title = element_blank(),
71 | legend.key.height= unit(0.5, 'cm'),
72 | legend.key.width= unit(2, 'cm'),
73 | legend.text = element_text(family = body_font,
74 | size=12,
75 | face = 'plain',
76 | color = txt_col),
77 | # TITLE
78 | plot.title.position = "plot",
79 | plot.title = element_text(family = title_font,
80 | face = "bold",
81 | color = txt_col,
82 | size = 40,
83 | lineheight = 1,
84 | margin = margin(20,0,10,0)),
85 | # Subtitle
86 | plot.subtitle = element_textbox(family=body_font,
87 | face = "plain",
88 | width = unit(58, "lines"),
89 | size = 18,
90 | color = "grey20",
91 | lineheight = 1,
92 | margin = margin(5,0,20,0)),
93 | # Caption
94 | plot.caption = element_text(family=body_font,
95 | face="plain",
96 | size=13,
97 | color="grey40",
98 | hjust=.5,
99 | margin=margin(20,0,0,0)),
100 |
101 | plot.background = element_rect(color=bg, fill=bg),
102 | plot.margin = margin(30,60,30,60)
103 | )
104 |
105 | # ------ Save Plot ------
106 |
107 | showtext_opts(dpi = 320)
108 | ggsave("week-08.png", height = 15,
109 | width = 13, dpi=320)
110 | showtext_auto(FALSE)
111 |
--------------------------------------------------------------------------------
/2022/Week_34/week_34.R:
--------------------------------------------------------------------------------
1 | library(tidytuesdayR)
2 | library(tidyverse)
3 | library(MetBrewer)
4 | library(patchwork)
5 | library(showtext)
6 |
7 | # Set the Stage ------------------------------------------------
8 |
9 | font <- "Share Tech"
10 | font_add_google(font, font)
11 | theme_set(hrbrthemes::theme_ipsum(base_family = font))
12 | showtext_auto(enable = TRUE)
13 | bg <- "#F0EBE3"
14 |
15 | # read data-----------------------------------------------------
16 |
17 | raw_chips <- read.csv("chip_dataset.csv")
18 |
19 |
20 | # data wrangling-------------------------------------------------
21 |
22 | chips <- raw_chips|>
23 | janitor::clean_names()|>
24 | mutate(year = lubridate::year(as.Date(release_date)))|>
25 | select(product, year, type, die_size_mm_2, transistors_million)|>
26 | drop_na()
27 |
28 |
29 |
30 | # plot----------------------------------------------------------
31 |
32 |
33 | p1 <- chips |> ggplot(aes(x=year, y=transistors_million, color=type))+
34 | geom_point(size = 3.5, alpha=0.25)+
35 | scale_color_manual(values=met.brewer("Egypt"))+
36 | scale_y_log10()+
37 | geom_smooth(method = 'loess', se=FALSE)+
38 | labs(
39 | title = "Transistor Count.",
40 | x = "Year",
41 | y = "Tansistors (millions)") +
42 | coord_cartesian(clip="off") +
43 |
44 | theme(
45 | axis.ticks = element_blank(),
46 | axis.text.x = element_text(size=10, color="grey40", face='plain'),
47 | axis.text.y = element_text(size=10,color="grey40", face='plain'),
48 | axis.title.x = element_blank(),
49 | axis.title.y = element_text(size=10,color="grey40", face='plain', hjust=0.5),
50 | plot.title = element_text(size=16, face="bold"),
51 | plot.background = element_rect(color = bg, fill=bg),
52 | plot.margin = margin(20,30,0,30),
53 | plot.title.position = "plot",
54 | legend.position = "bottom",
55 | legend.title = element_blank())
56 |
57 | # plot 2 ------------------------------------------------------
58 |
59 | p2 <- chips |> ggplot(aes(x=year, y=die_size_mm_2))+
60 | geom_point(aes(color=type, alpha=0.4))+
61 | scale_color_manual(values=met.brewer("Egypt"))+
62 | geom_smooth(method = 'loess', se=FALSE)+
63 | facet_wrap(~type)+
64 | labs(
65 | title = "Die density (number of transistors per unit area).",
66 | x = "Year",
67 | y = "Die size (mm^2)") +
68 | coord_cartesian(clip="off")+
69 | theme(
70 | axis.ticks = element_blank(),
71 | axis.text.x = element_text(size=10, color="grey40", face='plain'),
72 | axis.text.y = element_text(size=10,color="grey40", face='plain'),
73 | axis.title.x = element_blank(),
74 | axis.title.y = element_text(size=12,color="grey40", face='plain', hjust=0.5),
75 | plot.title = element_text(size=16, face="bold"),
76 | plot.background = element_rect(color = bg, fill=bg),
77 | plot.margin = margin(30,30,30,30),
78 | plot.title.position = "plot",
79 | legend.position = "none",
80 | strip.text = element_text(size = 18))
81 |
82 | # PATCHWORK ----------------------------------------------------------
83 |
84 | (p1/p2) +
85 | plot_annotation(
86 | title = stringr::str_to_upper("Does Moore's law still hold?"),
87 | subtitle = "Moore’s Law predicted that the number of transistors on a chip
88 | would double every 18 months. Particularly for GPUs,
89 | the number of transistors on a device continues to rise rapidly with time.
90 | Die density is one of the factors driving up transistor count in GPUs.",
91 | caption = "Muhammad Azhar | #TidyTuesday Week 34 | Data: CHIP dataset ",
92 | theme = theme(
93 | plot.title = element_text(size=34, face="bold",
94 | margin=margin(10,0,10,0)),
95 | plot.subtitle = element_text(size=16, face="bold",color = "grey40"),
96 | plot.caption = element_text(hjust=.5, margin=margin(20,0,0,0),
97 | size=12, face="bold"),
98 | plot.title.position = "plot",
99 | plot.margin = margin(30,30,30,30),
100 | plot.background = element_rect(color=bg, fill=bg)))
101 |
102 | # Save plot ----------------------------------------------------------
103 |
104 | showtext_opts(dpi = 320)
105 |
106 | ggsave("week_34.png", height = 10, width = 8, dpi=320)
107 |
108 | showtext_auto(FALSE)
109 |
--------------------------------------------------------------------------------
/2023/Week-48/week-48.R:
--------------------------------------------------------------------------------
1 | library(tidytuesdayR)
2 | library(tidyverse)
3 | library(ggforce)
4 | library(ggtext)
5 | library(glue)
6 | library(prismatic)
7 | library(showtext)
8 |
9 |
10 | # ------ Get Data ------
11 |
12 | tuesdata <- tt_load(2023, week = 48)
13 |
14 | drwho_episodes <- tuesdata$drwho_episodes
15 | drwho_directors <- tuesdata$drwho_directors
16 | drwho_writers <- tuesdata$drwho_writers
17 |
18 | # ------ Data Wrangling ------
19 |
20 | df <- left_join(drwho_episodes, drwho_directors, by="story_number")|>
21 | filter(type=="episode")
22 |
23 | df <- left_join(df, drwho_writers, by="story_number")|>
24 | select(season_number, episode_number, uk_viewers, rating, duration)|>
25 | group_by(season_number)
26 |
27 |
28 |
29 |
30 | # ------ Typography ------
31 |
32 | font_add_google("Fraunces", "title_font")
33 | font_add_google("Chivo", "body_font")
34 | showtext_auto()
35 |
36 | title_font <- "title_font"
37 | body_font <- "body_font"
38 |
39 | # ------ Set theme ------
40 |
41 | bg <- "#FFFFFF"
42 | txt_col <- "black"
43 |
44 | # ------ Texts ------
45 |
46 | title_text <- "Dr. WHO?"
47 | subtitle_text <- "Doctor Who episodes rating by UK viewers."
48 | caption_text <- "Graphic: Muhammad Azhar | @imagineazhar"
49 |
50 | # ------ Plot ------
51 |
52 | df |> ggplot(aes(y=uk_viewers, x=rating))+
53 | geom_point(size=3, shape=20, alpha=0.4)+
54 | geom_point(shape=1, size=3)+
55 | facet_wrap(vars(season_number), ncol = 3,
56 | labeller = labeller(season_number = function(x) {ifelse(x >= 1,
57 | paste("Season", x),
58 | " ")}),
59 | scales = "fixed")+
60 | labs(title = title_text,
61 | subtitle = subtitle_text,
62 | caption = caption_text,
63 | y="Viewers (millions)",
64 | x="Rating")+
65 | hrbrthemes::theme_ipsum()+
66 | theme(
67 | strip.text.x = element_text(family = title_font,
68 | face = 'bold',
69 | size = 15, colour = "grey20"),
70 | axis.text.y = element_text(family = body_font,
71 | face = 'plain',
72 | size = 9, colour = "grey40"),
73 | axis.ticks.y = element_blank(),
74 |
75 | axis.text.x = element_text(family = body_font,
76 | face = 'plain',
77 | size = 9, colour = "grey60"),
78 | axis.ticks.x = element_blank(),
79 |
80 | axis.title.x = element_text(family = body_font,
81 | face = 'bold', hjust = 0.5,
82 | size = 11, colour = "grey60",
83 | margin = margin(10, 0, 0, 0)),
84 | axis.title.y = element_text(family = body_font,
85 | face = 'bold', hjust = 0.5,
86 | size = 11, colour = "grey40",
87 | margin = margin(0, 20, 20, 0)),
88 |
89 | # TITLE
90 | plot.title.position = "panel",
91 | plot.title = element_text(margin = margin(20, 0, 10, 0),
92 | size = 40,
93 | family = title_font),
94 | # Subtitle
95 | plot.subtitle = element_textbox(family=body_font,
96 | face = "plain",
97 | width = unit(60, "lines"),
98 | size = 20,
99 | color = "grey20",
100 | margin = margin(10,0,20,0)),
101 | # Caption
102 | plot.caption = element_text(family=body_font,
103 | face="plain",
104 | size=14,
105 | color="grey40",
106 | hjust=.5,
107 | margin=margin(20,0,0,0)),
108 |
109 | plot.background = element_rect(color=bg, fill=bg),
110 | plot.margin = margin(30, 50, 30, 50)
111 | )
112 |
113 | # ------ Save Plot ------
114 |
115 | showtext_opts(dpi = 320)
116 | ggsave("week-48.png",dpi=320,
117 | width = 10, height = 12)
118 | showtext_auto(FALSE)
119 |
120 |
--------------------------------------------------------------------------------
/2022/Week_35/week_35.R:
--------------------------------------------------------------------------------
1 | library(tidyverse)
2 | library(showtext)
3 | library(usmap)
4 | library(patchwork)
5 |
6 | # Set the Stage ------------------------------------------------
7 |
8 | font <- "Ubuntu"
9 | font_add_google(font, font)
10 | showtext_auto(enable = TRUE)
11 | theme_set(theme_minimal(base_family = font))
12 | bg <- "#F0EBE3"
13 | txt_col <- 'black'
14 | color_low <- "#FFFFFF"
15 | color_high <- "#3a0ca3"
16 |
17 | # read data-----------------------------------------------------
18 |
19 | tt_data <- tidytuesdayR::tt_load(2022, week=35)
20 | pell <- tt_data$pell
21 |
22 | # data wrangling-------------------------------------------------
23 |
24 | df <- pell |>
25 | group_by(STATE, YEAR) |>
26 | summarise(total_award = sum(AWARD),
27 | total_recipients = sum(RECIPIENT))|>
28 | mutate(award_per_recipient = total_award / total_recipients )|>
29 | janitor::clean_names()|> ungroup()
30 |
31 | df2000 <- df |> filter(year==2000)
32 | df2017 <- df |> filter(year==2017)
33 |
34 | # plot 1 ----------------------------------------------------------
35 |
36 | p1 <- plot_usmap(data = df2000, values = "award_per_recipient")+
37 | scale_fill_continuous(low = color_low, high = color_high, limits=c(1000,6000),
38 | name="Award per Recipient ($)",
39 | guide=guide_legend(
40 | keyheight = unit(3, units = "mm"),
41 | keywidth=unit(12, units = "mm"),
42 | label.position = "bottom",
43 | title.position = 'top', title.hjust=0.5, nrow=1))+
44 | labs(title = "2000")+
45 | theme(
46 | panel.grid = element_blank(),
47 | axis.title = element_blank(),
48 | axis.text = element_blank(),
49 | plot.title = element_text(size=20, color=txt_col, hjust=.5,
50 | face="bold", margin=margin(0,0,30,0)),
51 | plot.background = element_rect(color=bg, fill=bg),
52 | plot.margin = margin(0,0,0,0),
53 | legend.title = element_text(size=14),
54 | legend.text = element_text(size=10))
55 | p1
56 | # plot 2 ----------------------------------------
57 |
58 | p2 <- plot_usmap(data = df2017, values = "award_per_recipient")+
59 | scale_fill_continuous(low = color_low, high = color_high, limits=c(1000,6000),
60 | name="Award per Recipient ($)",
61 | guide=guide_legend(
62 | keyheight = unit(3, units = "mm"),
63 | keywidth=unit(12, units = "mm"),
64 | label.position = "bottom",
65 | title.position = 'top', title.hjust=0.5, nrow=1))+
66 | labs(title = "2017")+
67 | theme(
68 | panel.grid = element_blank(),
69 | axis.title = element_blank(),
70 | axis.text = element_blank(),
71 | plot.title = element_text(size=20, color=txt_col, hjust=.5,
72 | face="bold", margin=margin(0,0,30,0)),
73 | plot.background = element_rect(color=bg, fill=bg),
74 | plot.margin = margin(0,0,0,0),
75 | legend.title = element_text(size=14),
76 | legend.text = element_text(size=10))
77 |
78 | # PATCHWORK ----------------------------------------------------------
79 |
80 | p1 + p2 + plot_annotation(
81 | title = 'PELL GRANTS',
82 | subtitle = 'The amount awarded to each recipient more than doubled.',
83 | caption = "Muhammad Azhar | #TidyTuesday Week 35 | Data: U.S. Department of Education",
84 | ) + plot_layout(guides = "collect") &
85 | theme(legend.position = 'bottom',
86 | legend.background = element_rect(color = bg, fill=bg),
87 | legend.justification = "center",
88 | plot.title = element_text(size=34, color=txt_col,lineheight=1,
89 | hjust=0.5,face="bold",
90 | margin=margin(10,0,10,0)),
91 | plot.subtitle = element_text(size=20, color="grey50", face='bold',
92 | hjust=0.5, margin=margin(0,0,15,0)),
93 | plot.caption = element_text(hjust=.5, margin=margin(0,0,0,0),
94 | size=12, color=txt_col, face="bold"),
95 | plot.margin = margin(20,20,20,20),
96 | plot.background = element_rect(color=bg, fill=bg)
97 | )
98 |
99 |
100 | # Save plot ----------------------------------------------------------
101 |
102 | showtext_opts(dpi = 320)
103 |
104 | ggsave("week_35.png", height = 8, width = 10, dpi=320)
105 |
106 | showtext_auto(FALSE)
107 |
--------------------------------------------------------------------------------
/2023/Week-09/week-09.R:
--------------------------------------------------------------------------------
1 | library(tidyverse)
2 | library(ggforce)
3 | library(ggtext)
4 | library(glue)
5 | library(prismatic)
6 | library(showtext)
7 |
8 | # ------ Get Data ------
9 |
10 | sentiment <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-02-28/afrisenti.csv')
11 | languages <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-02-28/languages.csv')
12 |
13 |
14 | # ------ Data Wrangling ------
15 |
16 | df <- left_join(sentiment, languages, by="language_iso_code")|>
17 | select(language_iso_code, label, language)|>
18 | group_by(language_iso_code, label, language)|>
19 | summarise(n=n())|>
20 | ungroup()|>
21 | group_by(language_iso_code)|>
22 | mutate(total=sum(n),
23 | perc_of_total= round(100*n/total, 1),
24 | category_nr = row_number())|>
25 | ungroup()
26 |
27 | f = 0.5 # change to change shape of the "balloon"
28 |
29 | df_shapes <- df |>
30 | rowwise()|>
31 | mutate(
32 | # Calculate points on circle for the "balloons", we need 4 x-y pairs for geom_bspline_closed
33 | x = list(c(0,
34 | f * perc_of_total * sin(category_nr * 2 * pi / 9 - pi/4),
35 | perc_of_total * sin(category_nr * 2 * pi / 9), # real percentage for main "radius"
36 | f * perc_of_total * sin(category_nr * 2 * pi / 9 + pi/5),
37 | 0
38 | )),
39 | y = list(c(0,
40 | f * perc_of_total * cos(category_nr * 2 * pi / 9 - pi/5),
41 | perc_of_total * cos(category_nr * 2 * pi / 9), # real percentage for main "radius"
42 | f * perc_of_total * cos(category_nr * 2 * pi / 9 + pi/4),
43 | 0
44 | ))
45 | )|>
46 | ungroup()|>
47 | pivot_wider(id_cols = c(language_iso_code, language), names_from = category_nr,
48 | values_from = c(x,y))|>
49 | unnest(x_1:y_3)
50 |
51 | # Category colors
52 | pal <- c("#EB455F","#BAD7E9","#469990")
53 |
54 | # Pull categories from the dataset
55 | cat <- df|>
56 | distinct(label) |>
57 | pull()
58 |
59 | # Join colors with categories
60 | pal_df <- data.frame(c = pal, l = cat)
61 |
62 |
63 | # ------ Typography ------
64 |
65 | font_add_google("Fraunces", "title_font")
66 | font_add_google("Chivo", "body_font")
67 | showtext_auto()
68 |
69 | title_font <- "title_font"
70 | body_font <- "body_font"
71 |
72 | # ------ Set theme ------
73 |
74 | bg <- "#FFFFFF"
75 | txt_col <- "black"
76 |
77 | # ------ Texts ------
78 |
79 | title_text <- "Sentiment of African Languages"
80 | subtitle_text <- "Showing percentage of tweets classified as positive, negative, or neutral."
81 | caption_text <- "Graphic: Muhammad Azhar | Twitter: @imagineazhar | Data: AfriSenti"
82 |
83 | # ------ Plot ------
84 |
85 | df_shapes |> ggplot()+
86 | geom_bspline_closed(aes(x_1, y_1, group = language, fill = pal[1]), alpha = 0.8)+
87 | geom_bspline_closed(aes(x_2, y_2, group = language, fill = pal[2]), alpha = 0.8)+
88 | geom_bspline_closed(aes(x_3, y_3, group = language, fill = pal[3]), alpha = 0.8)+
89 | scale_fill_identity(guide = guide_legend(title = "", nrow = 2,
90 | override.aes = list(alpha = 0.7, shape = 2)),
91 | breaks = pal, labels = pal_df$l) +
92 | guides(fill = guide_legend(nrow = 1,
93 | label.position = "top"))+
94 | coord_fixed() +
95 | facet_wrap(vars(language), ncol = 5,
96 | labeller = labeller(language = label_wrap_gen(width = 10)))+
97 | labs(title = title_text,
98 | subtitle = subtitle_text,
99 | caption = caption_text,
100 | x="",
101 | y="")+
102 | hrbrthemes::theme_ipsum()+
103 | theme(
104 | strip.text.x = element_text(family = title_font,
105 | face = 'bold',
106 | size = 15, colour = "grey20"),
107 | axis.text.y = element_blank(),
108 | axis.ticks.y = element_blank(),
109 | axis.text.x = element_blank(),
110 | axis.ticks.x = element_blank(),
111 |
112 | # Legend
113 | legend.position = "top",
114 | legend.title = element_blank(),
115 | legend.key.height= unit(0.5, 'cm'),
116 | legend.key.width= unit(2.5, 'cm'),
117 | legend.spacing = unit(1, 'cm'),
118 | legend.text = element_text(family = body_font,
119 | size=15,
120 | face = 'plain',
121 | color = txt_col),
122 |
123 |
124 | # TITLE
125 | plot.title.position = "panel",
126 | plot.title = element_text(margin = margin(20, 0, 10, 0),
127 | size = 40,
128 | family = title_font),
129 | # Subtitle
130 | plot.subtitle = element_textbox(family=body_font,
131 | face = "plain",
132 | width = unit(60, "lines"),
133 | size = 20,
134 | color = "grey20",
135 | margin = margin(10,0,20,0)),
136 | # Caption
137 | plot.caption = element_text(family=body_font,
138 | face="plain",
139 | size=14,
140 | color="grey40",
141 | hjust=.5,
142 | margin=margin(20,0,0,0)),
143 |
144 | plot.background = element_rect(color=bg, fill=bg),
145 | plot.margin = margin(30, 50, 30, 50)
146 | )
147 |
148 | # ------ Save Plot ------
149 |
150 | showtext_opts(dpi = 320)
151 | ggsave("week-09.png",dpi=320,
152 | width = 14, height = 15)
153 | showtext_auto(FALSE)
154 |
155 |
--------------------------------------------------------------------------------
/2022/Week_42/week_42.R:
--------------------------------------------------------------------------------
1 | library(tidyverse)
2 | library(tidytuesdayR)
3 | library(showtext)
4 | library(MetBrewer)
5 | library(ggtext)
6 | library(tidytext)
7 | library(ggraph)
8 | library(tidygraph)
9 |
10 | # read data-----------------------------------------------------
11 |
12 | tt <- tt_load(2022, week=42)
13 | #readme(tt)
14 | all_dialogue <- tt$stranger_things_all_dialogue
15 |
16 | # data wrangling-------------------------------------------------
17 |
18 | # get bigrams from the text
19 | df <- all_dialogue|>
20 | unnest_tokens(output=bigram,
21 | input=dialogue,
22 | token = "ngrams",
23 | n=2)
24 |
25 | # remove stop words
26 | df_bigram <- df|>
27 | tidyr::separate(col = bigram,
28 | into = c("word_1", "word_2"),
29 | sep = " ")|>
30 | filter(!word_1 %in% stop_words$word,
31 | !word_2 %in% stop_words$word)
32 |
33 | # filter by Adjectives (at least one)
34 |
35 | vec_idx_adjectives <- which(parts_of_speech$pos == "Adjective")
36 | vec_words_adjectives <- parts_of_speech$word[vec_idx_adjectives]
37 |
38 | df_bigram_adj <- df_bigram|>
39 | filter(word_1 %in% vec_words_adjectives | word_2 %in% vec_words_adjectives)
40 |
41 | df_bigram_adj_count <- df_bigram_adj|>
42 | count(word_1, word_2, sort = T)
43 |
44 | df_viz <- df_bigram_adj_count|>
45 | rename(freq = n)|>
46 | filter(freq >= 5)|>
47 | mutate(color_encoding = case_when( freq <= quantile(freq, 0.7) ~ 1,
48 | freq > quantile(freq, 0.7) & freq <= quantile(freq, 0.85) ~ 2,
49 | freq > quantile(freq, 0.85) & freq <= quantile(freq, 0.95) ~ 3,
50 | freq > quantile(freq, 0.95) & freq <= quantile(freq, 0.97) ~ 4,
51 | T ~ 5))
52 |
53 | # ------ CREATE NETWORK ------
54 |
55 | graph_bigram <- df_viz |>
56 | igraph::graph_from_data_frame()|>
57 | as_tbl_graph()|>
58 | activate(nodes)|>
59 | mutate(degree_score = centrality_degree())|>
60 | rename(word = name)|>
61 | activate(edges) |>
62 | mutate(color_encoding = ordered(color_encoding,
63 | levels=c(1,2,3,4,5),
64 | labels= c("frequency < 10",
65 | "frequency >= 10 and < 12",
66 | "frequency >= 12 and < 20",
67 | "frequency >= 20 and < 50",
68 | "frequency > 50")))
69 | # ------ Texts ------
70 |
71 | title_text <- "Stranger Things"
72 | subtitle_text <- "Stranger Things is an American sci-fi television series created by the Duffer Brothers. It was first released as a Netflix original series on July 15, 2016.
Each dot represents a word, and when they are connected by a line, a bigram (a pair of words) is formed.The entire network displays the most frequently used bigrams throughout all seasons of the show.
"
73 | caption_text <- "Muhammad Azhar | Twitter: @imagineazhar | Data: 8flix.com"
74 |
75 |
76 | # ------ Typography ------
77 |
78 | font_add_google("Vollkorn", "title_font")
79 | font_add_google("Chivo", "body_font")
80 | showtext_auto()
81 |
82 | title_font <- "title_font"
83 | body_font <- "body_font"
84 |
85 | # ------ Set theme ------
86 |
87 | bg <- "#F0EBE3"
88 | txt_col <- "black"
89 |
90 |
91 | # ------ Plot ------
92 |
93 | network<- graph_bigram|>
94 | ggraph(layout = 'fr')+
95 | geom_edge_arc(aes(edge_color=color_encoding),
96 | edge_width = 1.5,
97 | strength = 0.3,
98 | end_cap = circle(5, 'pt'))+
99 | geom_node_point(aes(size=degree_score),
100 | colour = "#E0144C")+
101 | ggrepel::geom_label_repel(aes(x=x, y=y, label=word),
102 | force = 10,
103 | force_pull = 0.01,
104 | colour = "#42032C",
105 | family = body_font,
106 | fontface = "plain",
107 | size = 5)+
108 | scale_alpha_identity(guide = "none")+
109 | scale_edge_color_manual(values = met.brewer("OKeeffe2", 5, direction = 1),
110 | labels = ~ stringr::str_wrap(.x, width = 15),
111 | guide = guide_legend(title = NULL,
112 | label.position = "top"))+
113 | guides(size="none")+
114 | coord_fixed()+
115 | labs(title = title_text,
116 | subtitle = subtitle_text,
117 | caption = caption_text)+
118 | theme_void()+
119 | theme(
120 | # Legend
121 | legend.position = "none",
122 | legend.key.width = unit(0.5, 'in'),
123 | legend.text = element_text(family = body_font,
124 | face = 'plain',
125 | color = txt_col,
126 | size = 10,
127 | hjust = 0.5,
128 | margin = margin(20,0,20,0)),
129 | # TITLE
130 | plot.title.position = "plot",
131 | plot.title = element_text(family = title_font,
132 | face = "bold",
133 | color = txt_col,
134 | size = 40,
135 | lineheight = 1,
136 | margin = margin(20,0,20,0)),
137 | # Subtitle
138 | plot.subtitle = element_textbox(family=body_font,
139 | face = "plain",
140 | width = unit(32, "lines"),
141 | size = 14,
142 | lineheight = 1,
143 | margin = margin(0,0,10,0)),
144 | # Caption
145 | plot.caption = element_text(family=body_font,
146 | face="plain",
147 | size=12,
148 | color=txt_col,
149 | hjust=.5,
150 | margin=margin(20,0,0,0)),
151 |
152 | plot.background = element_rect(color=bg, fill=bg),
153 | plot.margin = margin(30,30,30,30)
154 | )
155 | network
156 |
157 |
158 | # ------ Save Plot ------
159 |
160 | showtext_opts(dpi = 320)
161 | ggsave("week_42.png", height = 10,
162 | width = 8, dpi=320)
163 | showtext_auto(FALSE)
164 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |