script5.R 5.03 KB
Newer Older
virginiemarques's avatar
virginiemarques committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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,
           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'))

virginiemarques's avatar
virginiemarques committed
137
map
virginiemarques's avatar
virginiemarques committed
138
139
140

# Arrange all plots
all_plots <- ggarrange(g3,  g2,map,
virginiemarques's avatar
virginiemarques committed
141
                       ncol = 3, nrow = 1,widths= c(.8,1.5,.7) )+
virginiemarques's avatar
virginiemarques committed
142
143
144
145
146
147
148
149
150
151
152
153
  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

virginiemarques's avatar
virginiemarques committed
154
155
156
157
# All 
all_plots_2 <- ggarrange(g1, all_plots, ncol=1)
all_plots_2

virginiemarques's avatar
virginiemarques committed
158
# Save
virginiemarques's avatar
virginiemarques committed
159
ggsave("expected_plots/script5_correction.png", all_plots_2, width = 15, height=18)
virginiemarques's avatar
virginiemarques committed
160
161