script_jb.R 2.17 KB
Newer Older
jbjuhel's avatar
jbjuhel committed
1
2
3
# Feature 1 
# Simple barplot of the rank 1 among votes

eboulanger's avatar
eboulanger committed
4
5
# coucou

jbjuhel's avatar
jbjuhel committed
6
7
8
9
# List of 10 features to change in this script:
# 1) Change barplot colors by the provided palette 
# 2) Change x axis angle
# 3) Add contouring in barblot
Laetitia MATHON's avatar
Laetitia MATHON committed
10
# 4) Rename title to be more explicit: "Best ranked bird in New Zealand"  -> Done by Laetitia
jbjuhel's avatar
jbjuhel committed
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
# 5) Add a caption: "Data source: Rtidytuesday' 
# 6) Change theme to classic
# 7) Add the number of observations on top on each bar
# 8) Change the caption font to italic
# 9) Change the order of birds by sorted abundance in x axis
# 10) Change title size to 18

# Lib
library(readr)
library(dplyr)
library(viridisLite)
library(devtools)
devtools::install_github("katiejolly/nationalparkcolors", force=TRUE)
library(nationalparkcolors)
library(ggplot2)

# Data for reading special characters:
# Sys.setlocale("LC_ALL", "German")
# options(encoding = "UTF-8")

# Set color palette for plots:
my_palette <- park_palette('GeneralGrant')

# Read in 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")

# inspect data
str(nz_bird)
nz_bird$bird_breed <- as.factor(nz_bird$bird_breed)
levels(nz_bird$bird_breed)
# Remove NAs
nz_bird <- na.omit(nz_bird)

# Extract vote1
votes1 <- nz_bird %>%
  filter(vote_rank == 'vote_1') %>%
  group_by(bird_breed) %>%
  summarise(total = n())

# Inspect
str(votes1)
votes1 <- arrange(votes1, desc(total))
# Extract the first 8 ones
top_votes_1 <- votes1[1:8,]
str(top_votes)

# Plot first 8
votes_1_plot <- ggplot(top_votes_1, aes(x = bird_breed, y = total, fill = bird_breed))+
  geom_bar(stat = 'identity')+
eboulanger's avatar
eboulanger committed
61
  scale_fill_manual(values = my_palette)+ # done Emilie
jbjuhel's avatar
jbjuhel committed
62
63
64
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = 'black'),
smanel's avatar
axe 90    
smanel committed
65
66
        axis.text.x = element_text(angle = 90, hjust = 1),
        legend.position = 'none')+ # STéphanie a change l'axe X
jbjuhel's avatar
jbjuhel committed
67
  scale_y_continuous(expand = c(0,0))+
Laetitia MATHON's avatar
Laetitia MATHON committed
68
  ggtitle('Best ranked bird in New Zealand')+
jbjuhel's avatar
jbjuhel committed
69
70
71
72
73
74
75
  xlab('Bird Breed')+
  ylab('Number of Votes')
votes_1_plot

# Save the plot
ggsave(votes_1_plot, file="plots/script1.png", width=6, height=5)