Commit 78b75823 authored by virginiemarques's avatar virginiemarques
Browse files

add scripts

parent c7c10632
plots/script3.png

440 KB | W: | H:

plots/script3.png

498 KB | W: | H:

plots/script3.png
plots/script3.png
plots/script3.png
plots/script3.png
  • 2-up
  • Swipe
  • Onion skin
# Second script: this produces a barplot displaying the most votes birds among the top5
# Lib
library(tidyverse)
library(here)
# Two feature on this script
# 1) Fix the bug with font type to display the plot
# 2) Increase caption size to 9
# Read data
# nz_bird <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-11-19/nz_bird.csv")
load("Rdata/get_data.Rdata")
# Format data
all_votes <- nz_bird %>%
filter(!is.na(bird_breed)) %>%
group_by(bird_breed) %>%
mutate(n = row_number()) %>%
ungroup() %>%
mutate(bird_breed = fct_reorder(bird_breed, n))
# Plot
all_votes %>%
ggplot() +
geom_bar(aes(bird_breed, fill = vote_rank), position = position_stack(reverse = TRUE)) +
scale_fill_manual(values = c("#D50000", "grey45", "grey40", "grey35", "grey30")) +
scale_y_continuous(breaks = seq(0, 12500, 2000)) +
coord_flip(expand = FALSE) +
labs(
title = "New Zealand Bird of the Year 2019",
subtitle = "Number of votes for every bird, by order of preference - first (red), second, third, fourth and fifth",
caption = "Source: New Zealand Forest and Bird Orginization | Graphic: Georgios Karamanis"
) +
theme_minimal() +
theme(
legend.position = "none",
plot.background = element_rect(fill = "black", color = NA),
axis.text.y = element_text(family = "IBM Plex Sans", size = 5, color = "grey95"),
axis.text.x = element_text(family = "IBM Plex Mono", size = 6, color = "white"),
axis.title = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(color = "grey30", size = 0.3),
panel.grid.minor.x = element_line(color = "grey20"),
plot.margin = margin(20, 20, 20, 20),
plot.title = element_text(family = "IBM Plex Sans Bold", size = 11, color = "white", margin = margin(0, 0, 0, 0)),
plot.subtitle = element_text(family = "IBM Plex Sans", size = 8, color = "white", margin = margin(2, 0, 10, 0)),
plot.caption = element_text(family = "IBM Plex Sans", size = 5, color = "grey50", margin = margin(10, 0, 0, 0))
)
# Save
ggsave("expected_plots/script2.png", dpi = 320, height = 8, width = 9)
\ No newline at end of file
# Lib
library(tidyverse)
library(lubridate)
# Features:
# 1) Change colors of birds names to the color of the line
# 2) Change point shape to triangle
# Import data
# nz_bird <- readr::read_csv(str_c(
# "https://raw.githubusercontent.com/",
# "rfordatascience/tidytuesday/master/",
# "data/2019/2019-11-19/nz_bird.csv"))
load("Rdata/get_data.Rdata")
# Format
vote_scores <- tibble(vote_rank =
c("vote_1", "vote_2", "vote_3", "vote_4", "vote_5"),
vote_score =
c(5:1)
)
# Format date
nz_bird$date <- ymd(nz_bird$date)
# Format data
dates <- tibble(date = unique(nz_bird$date))
birds <- tibble(bird_breed = sort(unique(nz_bird$bird_breed[!is.na(nz_bird$bird_breed)])))
matrix <- expand_grid(dates,birds)
# join data
bird_matrix <- matrix %>%
left_join(nz_bird)
# Format data
bird_scores <- bird_matrix %>%
left_join(vote_scores) %>%
select(date,bird_breed, vote_score) %>%
group_by(date, bird_breed) %>%
summarize(daily_score= sum(vote_score), daily_votes = n(),
daily_avg = daily_score/daily_votes) %>%
ungroup() %>%
select(date, bird_breed, daily_score, daily_votes) %>%
group_by(bird_breed) %>%
mutate(cum_score = cumsum(daily_score), cum_votes = cumsum(daily_votes)) %>%
ungroup()
# Format ranked data
ranked <- bird_scores %>%
group_by(date) %>%
arrange(desc(cum_score)) %>%
mutate(rank = row_number()) %>%
ungroup() %>%
arrange(date, bird_breed) %>%
filter(rank <= 10)
# Plot
ggplot(ranked, aes(x=date, y=rank)) +
geom_point(aes(color = bird_breed)) +
geom_line(aes(color = bird_breed)) +
geom_text(data = subset(ranked, ranked$date == min(ranked$date)),
aes(date, rank, label = bird_breed), size = 2,
nudge_y = 0.2, nudge_x = 0.5) +
geom_text(data = subset(ranked, ranked$date == max(ranked$date)),
aes(date, rank, label = bird_breed), size = 2,
nudge_y = 0.2, nudge_x = -0.5) +
scale_y_reverse(breaks = 1:10, labels = c("1st", "2nd", "3rd", str_c(4:10,"th"))) +
scale_x_date(breaks = seq(from = min(ranked$date), to = max(ranked$date), by = "day"),
labels = c("First Day", rep("",(length(unique(ranked$date))-2)), "Final Day")) +
labs(title = "New Zealand Bird of the Year",
subtitle = "Daily Progression of Top 10 Rankings",
y = "", x = "Voting Period",
color = "Bird Breed") +
theme(panel.background=element_rect(fill = NA), axis.ticks.y = element_line(size = NA))
# Save
ggsave('plots/script3.png', width=10, height=8)
# Lib
library(tidyverse)
library(lubridate)
library(here)
library(jkmisc)
library(glue)
library(ggforce)
library(ggtext)
# Source functions
source("scripts/functions.R")
# Features
# 1) Change background color to gray14
# 2) remove background lines
# Load Rdata
# nz_bird <- here('2019', 'week47', 'data', 'BOTY-votes-2019.csv') %>%
# read_csv()
load("Rdata/get_data.Rdata")
# Format data to long
bird_long <- boty %>%
select(-country) %>%
mutate(idx = row_number()) %>%
pivot_longer(cols = vote_1:vote_5, names_to = "vote_rank", values_to = "bird_breed") %>%
filter(!is.na(vote_rank), !is.na(bird_breed)) %>%
count(vote_rank, bird_breed)
# palette
gunmetal_pal <- c("#AAA9AD", "#848689", "#5B676D", "#2A3439", "#1F262A")
# Format data
bird_sets <- boty %>%
select(vote_1:vote_5) %>%
filter(complete.cases(.)) %>%
filter_at(.vars = vars(vote_1:vote_5), .vars_predicate = any_vars(str_detect(., "Yellow-eyed penguin"))) %>%
count(vote_1, vote_2, vote_3, vote_4, vote_5, sort = TRUE) %>%
filter(vote_1 == "Yellow-eyed penguin") %>%
gather_set_data(1:5) %>%
group_by(id) %>%
mutate(fill = case_when(vote_1 == "Yellow-eyed penguin" ~ "#FFDA29",
TRUE ~ gunmetal_pal[5])) %>%
mutate(alpha = case_when(vote_1 == "Yellow-eyed penguin" ~ 1,
TRUE ~ 0.5))
# Label1
labels_right <- bird_sets %>%
filter(x == "vote_5")
# Label2
labels_left <- bird_sets %>%
filter(x == "vote_1")
# Plot
plot <- ggplot(bird_sets, aes(x = x, id = id, split = y, value = n)) +
geom_parallel_sets(aes(fill = fill), axis.width = 0.05, sep = 0.2) +
geom_parallel_sets_axes(axis.width = 0.05, color = "#FFDA29", size = 0.1, sep = 0.2) +
geom_text(data = labels_left, aes(x = x, id = id, split = y, label = y), stat = "parallel_sets_axes", sep = 0.2, size = 3, hjust = 1, nudge_x = -0.1, color = "#E5E9F0") +
geom_text(data = labels_right, aes(x = x, id = id, split = y, label = y), stat = "parallel_sets_axes", sep = 0.2, size = 3, hjust = 0, nudge_x = 0.1, color = "#E5E9F0") +
scale_fill_identity() +
labs(x = NULL,
y = NULL,
title = "Voting Patterns Of Those Who Cast Their First Vote For The Yellow-eyed Penguin",
subtitle = str_wrap("Illustrated below is a parallel set of the five votes any one voter could cast, which illustrates perference patterns in the vote.
The data is limited to those that cast all five votes and to those that selected the Yellow-eyed Penguin as their first choice.
The heavier the line, the more votes cast with that particular combination.", 200),
caption = "Data: **NZ Forest & Bird** | Modified from: **@jakekaupp**") +
scale_x_discrete(labels = c("First Vote", "Second Vote", "Third Vote", "Fourth Vote", "Fifth Vote")) +
theme_jk()
plot
# Save plot
ggsave("plots/script4.png", plot, width = 16, height = 12, dpi = 150)
# Lib
library(ggrepel)
library(tidyverse)
library(emojifont)
library(grid)
library(ggpubr)
library(emo)
# Load data
# nz_bird <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-11-19/nz_bird.csv")
load('Rdata/get_data.Rdata')
# Features
# 1) remove the fill parameter causing a warning in plot1
# 2) assemble all plots in one
# --------------------------------------------------- #
# PLOT 1 #
# --------------------------------------------------- #
# data
df <- nz_bird%>%
filter(vote_rank=='vote_1')%>%
count(bird_breed)%>%
top_n(5,n)
# NZ image
imgage <- png::readPNG('img/newzealand.png')
a <- grid::rasterGrob(imgage, interpolate = T)
# Plot 1
g1 <- ggplot(df,aes(x=bird_breed,y=n,fill=bird_breed))+
geom_col(position = 'dodge',color='#2a2a2a',
show.legend = F)+
geom_text(aes(label=n),
position=position_dodge(width=0.9),
vjust=-0.25,
color='white',
size=10 ) +
annotation_custom(a, xmin =0, xmax = 3,
ymin=4000 ,ymax=7000) +
ylim(0,7000)+
theme_bw()+
annotate("text",
label = 'Bird of the Year',
x = 3, y = 6000, size = 10,
fill ='#fffeea',
colour ="white")+
theme(plot.background = element_rect(color='black',fill='black'),
panel.background = element_rect(color='black',fill='black'),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
axis.text.x = element_text(size=14,face = 'bold',
color = 'white'))
g1
# --------------------------------------------------- #
# PLOT 2 #
# --------------------------------------------------- #
# Format data for plot
df_ye <- nz_bird %>%
filter(bird_breed=='Yellow-eyed penguin') %>%
count(vote_rank) %>%
mutate(emoji=emojifont::emoji('star'))
# Add measures
df_ye$percentage =
df_ye$n / sum(df_ye$n)* 100
df_ye = df_ye[rev(order(df_ye$percentage)), ]
df_ye$ymax = cumsum(df_ye$percentage)
df_ye$ymin = c(0, head(df_ye$ymax, n = -1))
df_ye
# Plot
g2 <- ggplot(df_ye,
aes(fill = vote_rank, ymax = ymax, ymin = ymin, xmax = 100, xmin = 50)) +
geom_rect(colour = "black",show.legend = F) +
coord_polar(theta = "y") +
xlim(c(0, 100)) +
geom_label_repel(
aes(label = paste(vote_rank,"\n",round(percentage,2),"%"),
x = 100,
y = (ymin + ymax)/2),
inherit.aes = F,
show.legend = F, size = 4,
fill ='#fffeea',
box.padding = unit(0.35, "lines"),
point.padding = unit(0.3, "lines"), fontface = 'bold',
hjust=0, vjust=1, segment.size = 0) +
annotate("text", x = 0, y = 0, size = 6,
label = "Yellow Eyed \n penguin",color='white') +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.text.y = element_blank(),
axis.text.x =element_blank(),
axis.ticks = element_blank(),
panel.background = element_rect(color='black',fill='black'),
plot.background = element_rect(color='black',fill='black'))
g2
# --------------------------------------------------- #
# PLOT 3 #
# --------------------------------------------------- #
imgage_b <- png::readPNG('img/yellow-eyed-penguin.png')
g3 <- ggplot()+
annotation_custom(rasterGrob(imgage_b,
width = unit(1,"npc"),
height = unit(1,"npc")),
-Inf, Inf, -Inf, Inf) +
theme(plot.background = element_rect(colour = 'black',fill='black'),
panel.background = element_rect(colour = 'black',fill='black'),
plot.caption = element_text(color = 'white',size=10)
)
g3
# --------------------------------------------------- #
# PLOT ALL #
# --------------------------------------------------- #
map <- ggplot()+
geom_sf(data=spData::nz,color='black',fill='gray')+
coord_sf() +
theme_void()+
theme(plot.background = element_rect(color='black',fill='black'),
panel.background = element_rect(color='black',fill='black'))
# Arrange all plots
all_plots <- ggarrange(g3, g2,map,
ncol = 3, nrow = 1,widths= c(.8,1.5,.7) )+
labs(caption=paste0("Source: New Zealand Forest and Bird Orginization | by @r0mymendez", emo::ji("heart")),
subtitle = ' ',
title='New Zealand Bird of the Year') +
theme(plot.background = element_rect(fill='black',color ='#2a2a2a'),
plot.caption = element_text(colour = 'white',size = 13,hjust = 1,
face = 'bold'),
plot.title = element_text(colour = 'white',size = 40,hjust = 0.5,
face = 'bold'),
plot.margin = unit(c(1,0,0.1,1), "cm"))
all_plots
# Save
ggsave("plots/script5.png", all_plots, width = 12, height=8)
# Lib
library(tidyverse)
library(cowplot)
# install.packages("waffle", repos = "https://cinc.rud.is")
library(waffle)
# data
# d <- read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-11-19/nz_bird.csv')
load("Rdata/get_data.Rdata")
d <- nz_bird
# Features
# 1) color of strip text to white
# 2) color of background to black + remove lines
d2 <- d %>%
mutate(vote_rank = str_sub(vote_rank, -1)) %>%
filter(!is.na(bird_breed)) %>%
# tally votes
count(bird_breed, vote_rank, name = 'votes') %>%
group_by(bird_breed) %>%
mutate(all_votes = sum(votes)) %>%
ungroup %>%
# top5 birds
filter(dense_rank(-all_votes) <= 5) %>%
mutate(bird_breed = reorder(bird_breed, -votes))
# Recode bird names
d2$bird_breed <- fct_recode(d2$bird_breed, Kakapo = "Kākāpō", Kaka = "Kākā")
d2$bird_breed
# waffle chart
p <- ggplot(d2, aes(values = votes/25, x = votes, fill = vote_rank)) +
facet_wrap(~ bird_breed, ncol = 1, strip.position = 'left') +
waffle::geom_waffle(n_rows = 10, col = 'grey20', size = .15) +
scale_x_continuous(breaks = seq(0, 1000, 500)/25, labels = seq(0, 1000, 500)*10, expand = expand_scale(mult = c(.1, .05))) +
scale_y_continuous(expand = expand_scale(mult = .2)) +
scale_fill_viridis_d('Vote Rank', option = 'plasma') +
coord_equal() +
labs(xlab = 'Votes', ylab = '',
title = 'New Zealand • Bird of the Year 2019',
subtitle = 'Top 5 • 1 square = 25 votes',
caption = 'Data by New Zealand Forest and Bird Organization\n#TidyTuesday • @watzoever') +
#btheme_minimal() +
theme(
text = element_text(color = 'grey85'),
plot.margin = unit(c(1, 1, 2, 1), 'cm'),
strip.text.y = element_text(angle = 180, hjust = 0, margin = margin(r = 10)),
panel.spacing.y = unit(0, 'line'),
panel.grid.major.x = element_line(unit(.5, 'line'), color = 'grey60'),
axis.text = element_text(size = 10, color = 'grey60'),
axis.text.y = element_blank(),
plot.caption = element_text(size = 10, color = 'grey60', hjust = 0, margin = margin(t = 20)),
legend.key.size = unit(1, 'line'),
legend.title = element_text(size = 12), legend.text = element_text(size = 10),
plot.background = element_rect(fill = 'grey20', color = 'grey20'),
legend.background = element_blank(),panel.border = element_blank(),
strip.background = element_blank()
)
p
ggsave('plots/script6.png', p, width = 8, height = 7)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment