Please visit https://github.com/arifpras/KelasData for the datasets and other relevant materials to be used during the sessions.
rm(list=ls())
ls()
## character(0)
#which directory at the moment
getwd()
## [1] "/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/WranglingViz"
#setting working directory
setwd("/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData")
#dir()
#sources:
##--https://www.kaggle.com/michau96
##--https://www.kaggle.com/aditya2803
##--https://www.opfanpage.com/2018/06/29/one-piece-power-ranking-chart/
##--https://listfist.com/list-of-one-piece-characters-by-age
#".csv" files - local
#op_chapters <- read.csv(file.choose())
#op_chapters <- read.csv("/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_chapters.csv")
#op_characters <- read.csv(file.choose())
#op_characters <- read.csv("/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_characters.csv")
#op_ratings <- read.csv(file.choose())
#op_ratings <- read.csv("/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_ratings.csv")
#op_powers <- read.csv(file.choose())
#op_powers <- read.csv("/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_powers.csv")
#op_ages <- read.csv(file.choose())
#op_ages <- read.csv("/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_ages.csv")
#".xlsx" files - local
#op_chapters <- read_excel(path = "/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_all.xlsx", sheet = "OP_chapters")
#op_characters <- read_excel(path = "/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_all.xlsx", sheet = "OP_characters")
#op_ratings <- read_excel(path = "/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_all.xlsx", sheet = "OP_ratings")
<- readxl::read_excel(path = "/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_all.xlsx", sheet = 4)
op_powers <- read_excel(path = "/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_all.xlsx", sheet = "OP_ages")
op_ages
#".csv" files - cloud
#op_chapters <- read.csv("https://www.dropbox.com/s/t1vrxsa4ad0m2c4/OP_chapters.csv?dl=1")
#op_characters <- read.csv("https://www.dropbox.com/s/e74njw4iw7qfzbl/OP_characters.csv?dl=1")
#op_ratings <- read.csv("https://www.dropbox.com/s/qvdhlnkyk02giuj/OP_ratings.csv?dl=1")
#op_powers <- read.csv("https://www.dropbox.com/s/t3gbzzscz438ecp/OP_powers.csv?dl=1")
#op_ages <- read.csv("https://www.dropbox.com/s/r24vzv7eyz35irr/OP_ages.csv?dl=1")
#require(devtools)
#install_github("Displayr/flipAPI")
#library(flipAPI)
#".xlsx" files - cloud
#op_chapters <- flipAPI::DownloadXLSX("https://www.dropbox.com/s/5mev3xo0cd3tc61/OP_all.xlsx?dl=0", sheet = 1)
#op_characters <- flipAPI::DownloadXLSX("https://www.dropbox.com/s/5mev3xo0cd3tc61/OP_all.xlsx?dl=0", sheet = "OP_characters")
#op_ratings <- flipAPI::DownloadXLSX("https://www.dropbox.com/s/5mev3xo0cd3tc61/OP_all.xlsx?dl=0", sheet = 3)
#op_powers <- flipAPI::DownloadXLSX("https://www.dropbox.com/s/5mev3xo0cd3tc61/OP_all.xlsx?dl=0", sheet = "OP_powers", want.data.frame = TRUE)
#op_ages <- flipAPI::DownloadXLSX("https://www.dropbox.com/s/5mev3xo0cd3tc61/OP_all.xlsx?dl=0", sheet = "OP_ages", want.data.frame = TRUE)
str(op_powers)
## tibble [56 × 4] (S3: tbl_df/tbl/data.frame)
## $ power : num [1:56] 100 100 100 100 99 95 95 94.5 94 93 ...
## $ character : chr [1:56] "Red Haired Shanks" "Marshall D. Teach" "Charlotte Linlin" "Kaido" ...
## $ level : chr [1:56] "Yonko" "Yonko" "Yonko" "Yonko" ...
## $ affiliations: chr [1:56] "Red Haired Pirates" "Blackbeard Pirates" "Big Mom Pirates" "Beasts Pirates" ...
str(op_ages)
## tibble [482 × 2] (S3: tbl_df/tbl/data.frame)
## $ name: chr [1:482] "Zunesha" "Jarul" "Jorul" "Hatcha" ...
## $ age : num [1:482] 1000 408 344 188 160 160 160 159 156 153 ...
#or
glimpse(op_powers)
## Rows: 56
## Columns: 4
## $ power <dbl> 100.0, 100.0, 100.0, 100.0, 99.0, 95.0, 95.0, 94.5, 94.0,…
## $ character <chr> "Red Haired Shanks", "Marshall D. Teach", "Charlotte Linl…
## $ level <chr> "Yonko", "Yonko", "Yonko", "Yonko", "Admiral", "Admiral",…
## $ affiliations <chr> "Red Haired Pirates", "Blackbeard Pirates", "Big Mom Pira…
glimpse(op_ages)
## Rows: 482
## Columns: 2
## $ name <chr> "Zunesha", "Jarul", "Jorul", "Hatcha", "Brogy", "Dorry", "Morley"…
## $ age <dbl> 1000, 408, 344, 188, 160, 160, 160, 159, 156, 153, 141, 120, 105,…
%>%
op_powers ::filter(affiliations == "Marine") dplyr
%>%
op_powers arrange(character) #ascending, alphabetical order
#arrange(desc(character)) #descending, alphabetical order
<- op_powers %>%
law mutate(character02 =
recode(character,
"Trafalgar D. Water Law" = "Trafalgar D. Law"))
%>% filter(affiliations == "Heart Pirates") %>%
law select(character, character02)
%>%
op_powers slice(18L) #variable number 18
%>%
op_powers slice(n()) #the last variable
%>%
op_powers slice(50:n())
%>%
op_powers slice_min(character, n = 5, with_ties = FALSE)
%>%
op_powers slice_min(character, prop = 0.5, with_ties = TRUE)
%>%
op_powers slice_tail(n = 5)
%>%
op_powers slice_tail(prop = 0.5)
%>%
op_powers slice_max(affiliations, n = 5, with_ties = FALSE)
%>%
op_powers slice_max(affiliations, prop = 0.5, with_ties = TRUE)
%>%
op_powers slice_head(n = 5)
%>%
op_powers slice_head(prop = 0.5)
<- op_powers %>%
db03 select(character, affiliations)
db03
<- op_powers %>%
db04 select(-level)
db04
<- op_powers %>%
db05 relocate(character, power, affiliations)
db05
<- op_powers %>%
status rename("status" = "level")
status
%>%
op_powers filter(affiliations == "Marine") %>%
count() %>%
::kbl() %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
n |
---|
10 |
<- op_powers %>%
db06 mutate(anime = "One Piece",
power_2 = power/2)
db06
<- op_powers %>%
db07 transmute(anime = "One Piece",
power_2 = power/2)
db07
<- op_powers %>%
db08 arrange(affiliations, desc(power)) %>%
group_by(affiliations) %>%
mutate(rank = row_number()) %>%
ungroup() %>% #close the group_by()
arrange(affiliations) %>%
select(character, affiliations, rank)
db08
<- op_powers %>%
db09 arrange(affiliations, desc(power)) %>%
group_by(affiliations) %>%
summarise(avg = mean(power, na.rm = TRUE),
n = n()) %>%
ungroup()
db09
<- op_powers %>%
db10a mutate(fase1 = power*0.25,
fase2 = power*0.75)
db10a
<- db10a %>%
db10b rowwise(character) %>%
summarise(total = sum(fase1, fase2))
## `summarise()` has grouped output by 'character'. You can override using the `.groups` argument.
db10b
<- data.frame(
villain_aff character = c("Marshall D. Teach", "Charlotte Linlin", "Kaido", "Sakazuki Akainu", "Borsalino Kizaru"),
affiliation = c("Blackbeard Pirates", "Big Mom Pirates", "Beasts Pirates", "Marine", "Marine")
)
<- data.frame(
character_power name = c("Marshall D. Teach", "Kaido", "Sakazuki Akainu", "Monkey D. Dragon", "Silvers Rayleigh"),
power = c(100, 100, 95, 99, 93)
)
#mutating joins
%>%
villain_aff left_join(character_power, by = c("character" = "name")) %>%
::kbl() %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
character | affiliation | power |
---|---|---|
Marshall D. Teach | Blackbeard Pirates | 100 |
Charlotte Linlin | Big Mom Pirates | NA |
Kaido | Beasts Pirates | 100 |
Sakazuki Akainu | Marine | 95 |
Borsalino Kizaru | Marine | NA |
#mutating joins
%>%
villain_aff right_join(character_power, by = c("character" = "name")) %>%
::kbl() %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
character | affiliation | power |
---|---|---|
Marshall D. Teach | Blackbeard Pirates | 100 |
Kaido | Beasts Pirates | 100 |
Sakazuki Akainu | Marine | 95 |
Monkey D. Dragon | NA | 99 |
Silvers Rayleigh | NA | 93 |
#mutating joins
%>%
villain_aff inner_join(character_power, by = c("character" = "name")) %>%
::kbl() %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
character | affiliation | power |
---|---|---|
Marshall D. Teach | Blackbeard Pirates | 100 |
Kaido | Beasts Pirates | 100 |
Sakazuki Akainu | Marine | 95 |
#mutating joins
%>%
villain_aff full_join(character_power, by = c("character" = "name")) %>%
::kbl() %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
character | affiliation | power |
---|---|---|
Marshall D. Teach | Blackbeard Pirates | 100 |
Charlotte Linlin | Big Mom Pirates | NA |
Kaido | Beasts Pirates | 100 |
Sakazuki Akainu | Marine | 95 |
Borsalino Kizaru | Marine | NA |
Monkey D. Dragon | NA | 99 |
Silvers Rayleigh | NA | 93 |
#filtering joins
%>%
villain_aff semi_join(character_power, by = c("character" = "name")) %>%
::kbl() %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
character | affiliation |
---|---|
Marshall D. Teach | Blackbeard Pirates |
Kaido | Beasts Pirates |
Sakazuki Akainu | Marine |
#filtering joins
%>%
villain_aff anti_join(character_power, by = c("character" = "name")) %>%
::kbl() %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
character | affiliation |
---|---|
Charlotte Linlin | Big Mom Pirates |
Borsalino Kizaru | Marine |
(<- op_powers %>%
db11 mutate(villain = ifelse(affiliations == "Blackbeard Pirates", "Villain",
"NA"))
)
<- tribble(
luffy00 ~arc, ~power,
"1-Punk Hazard", 75,
"2-Dressrosa", NA,
"3-Zou", 80,
"4-Whole Cake Island", 82,
"5-Levely", NA
)
::kbl(luffy00) %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc | power |
---|---|
1-Punk Hazard | 75 |
2-Dressrosa | NA |
3-Zou | 80 |
4-Whole Cake Island | 82 |
5-Levely | NA |
<- luffy00 %>%
luffy00 add_row(arc = "6- Wano Country",
power = 85)
::kbl(luffy00) %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc | power |
---|---|
1-Punk Hazard | 75 |
2-Dressrosa | NA |
3-Zou | 80 |
4-Whole Cake Island | 82 |
5-Levely | NA |
6- Wano Country | 85 |
is.na(luffy00)
## arc power
## [1,] FALSE FALSE
## [2,] FALSE TRUE
## [3,] FALSE FALSE
## [4,] FALSE FALSE
## [5,] FALSE TRUE
## [6,] FALSE FALSE
#data frame
colSums(is.na(luffy00))
## arc power
## 0 2
mean(luffy00$power)
## [1] NA
mean(luffy00$power, na.rm = TRUE)
## [1] 80.5
#vector
which(is.na(luffy00))
## [1] 8 11
sum(is.na(luffy00))
## [1] 2
<- luffy00 %>%
luffy01 fill(power, .direction = "up") %>%
mutate(method = "fill")
::kbl(luffy01) %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc | power | method |
---|---|---|
1-Punk Hazard | 75 | fill |
2-Dressrosa | 80 | fill |
3-Zou | 80 | fill |
4-Whole Cake Island | 82 | fill |
5-Levely | 85 | fill |
6- Wano Country | 85 | fill |
<- luffy00 %>%
luffy02 mutate(
power = replace(power, is.na(power), mean(power, na.rm = TRUE)),
method = "mean"
)
#luffy02$power[is.na(luffy02$power)] <- mean(luffy02$power, na.rm = TRUE)
::kbl(luffy02) %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc | power | method |
---|---|---|
1-Punk Hazard | 75.0 | mean |
2-Dressrosa | 80.5 | mean |
3-Zou | 80.0 | mean |
4-Whole Cake Island | 82.0 | mean |
5-Levely | 80.5 | mean |
6- Wano Country | 85.0 | mean |
<- luffy00 %>%
luffy03 mutate(power = zoo::na.approx(power),
method = "interpolate")
::kbl(luffy03) %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc | power | method |
---|---|---|
1-Punk Hazard | 75.0 | interpolate |
2-Dressrosa | 77.5 | interpolate |
3-Zou | 80.0 | interpolate |
4-Whole Cake Island | 82.0 | interpolate |
5-Levely | 83.5 | interpolate |
6- Wano Country | 85.0 | interpolate |
%>% drop_na() %>%
luffy00 ::kbl() %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc | power |
---|---|
1-Punk Hazard | 75 |
3-Zou | 80 |
4-Whole Cake Island | 82 |
6- Wano Country | 85 |
<- rbind(luffy01, luffy02, luffy03)
luffy04
<- luffy01 %>%
luffy04 bind_rows(luffy02, luffy03)
::kbl(luffy04) %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc | power | method |
---|---|---|
1-Punk Hazard | 75.0 | fill |
2-Dressrosa | 80.0 | fill |
3-Zou | 80.0 | fill |
4-Whole Cake Island | 82.0 | fill |
5-Levely | 85.0 | fill |
6- Wano Country | 85.0 | fill |
1-Punk Hazard | 75.0 | mean |
2-Dressrosa | 80.5 | mean |
3-Zou | 80.0 | mean |
4-Whole Cake Island | 82.0 | mean |
5-Levely | 80.5 | mean |
6- Wano Country | 85.0 | mean |
1-Punk Hazard | 75.0 | interpolate |
2-Dressrosa | 77.5 | interpolate |
3-Zou | 80.0 | interpolate |
4-Whole Cake Island | 82.0 | interpolate |
5-Levely | 83.5 | interpolate |
6- Wano Country | 85.0 | interpolate |
<- cbind(luffy01, luffy02, luffy03)
luffy05
<- luffy01 %>%
luffy05 bind_cols(luffy02, luffy03)
## New names:
## * arc -> arc...1
## * power -> power...2
## * method -> method...3
## * arc -> arc...4
## * power -> power...5
## * ...
::kbl(luffy05) %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc…1 | power…2 | method…3 | arc…4 | power…5 | method…6 | arc…7 | power…8 | method…9 |
---|---|---|---|---|---|---|---|---|
1-Punk Hazard | 75 | fill | 1-Punk Hazard | 75.0 | mean | 1-Punk Hazard | 75.0 | interpolate |
2-Dressrosa | 80 | fill | 2-Dressrosa | 80.5 | mean | 2-Dressrosa | 77.5 | interpolate |
3-Zou | 80 | fill | 3-Zou | 80.0 | mean | 3-Zou | 80.0 | interpolate |
4-Whole Cake Island | 82 | fill | 4-Whole Cake Island | 82.0 | mean | 4-Whole Cake Island | 82.0 | interpolate |
5-Levely | 85 | fill | 5-Levely | 80.5 | mean | 5-Levely | 83.5 | interpolate |
6- Wano Country | 85 | fill | 6- Wano Country | 85.0 | mean | 6- Wano Country | 85.0 | interpolate |
# selecting variables
(<- op_powers %>%
db01a left_join(op_ages, by = c("character" = "name")) %>%
select(-level) %>%
relocate(character, affiliations, power, age)
)
# pivoting
(<- db01a %>%
db01b pivot_longer(cols = power:age,
names_to = "variable",
values_to = "value")
)
(<- db01b %>%
db01c pivot_wider(names_from = variable,
values_from = value)
)
::write_xlsx(x = list(db01aSheet = db01a, db01bSheet = db01b, db01cSheet = db01c), path = "OnePiece.xlsx")
writexl
write.csv(db01a, file = "csv_db01a.csv")
<- luffy04 %>%
luffy06 mutate_each(method, funs = toupper)
## Warning: `mutate_each_()` was deprecated in dplyr 0.7.0.
## Please use `across()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
::kbl(luffy06) %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc | power | method |
---|---|---|
1-Punk Hazard | 75.0 | FILL |
2-Dressrosa | 80.0 | FILL |
3-Zou | 80.0 | FILL |
4-Whole Cake Island | 82.0 | FILL |
5-Levely | 85.0 | FILL |
6- Wano Country | 85.0 | FILL |
1-Punk Hazard | 75.0 | MEAN |
2-Dressrosa | 80.5 | MEAN |
3-Zou | 80.0 | MEAN |
4-Whole Cake Island | 82.0 | MEAN |
5-Levely | 80.5 | MEAN |
6- Wano Country | 85.0 | MEAN |
1-Punk Hazard | 75.0 | INTERPOLATE |
2-Dressrosa | 77.5 | INTERPOLATE |
3-Zou | 80.0 | INTERPOLATE |
4-Whole Cake Island | 82.0 | INTERPOLATE |
5-Levely | 83.5 | INTERPOLATE |
6- Wano Country | 85.0 | INTERPOLATE |
<- luffy06 %>%
luffy07 mutate_each(method, funs = tolower)
::kbl(luffy07) %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc | power | method |
---|---|---|
1-Punk Hazard | 75.0 | fill |
2-Dressrosa | 80.0 | fill |
3-Zou | 80.0 | fill |
4-Whole Cake Island | 82.0 | fill |
5-Levely | 85.0 | fill |
6- Wano Country | 85.0 | fill |
1-Punk Hazard | 75.0 | mean |
2-Dressrosa | 80.5 | mean |
3-Zou | 80.0 | mean |
4-Whole Cake Island | 82.0 | mean |
5-Levely | 80.5 | mean |
6- Wano Country | 85.0 | mean |
1-Punk Hazard | 75.0 | interpolate |
2-Dressrosa | 77.5 | interpolate |
3-Zou | 80.0 | interpolate |
4-Whole Cake Island | 82.0 | interpolate |
5-Levely | 83.5 | interpolate |
6- Wano Country | 85.0 | interpolate |
<- luffy07 %>%
luffy08 mutate_if(is.character, str_to_upper)
::kbl(luffy08) %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc | power | method |
---|---|---|
1-PUNK HAZARD | 75.0 | FILL |
2-DRESSROSA | 80.0 | FILL |
3-ZOU | 80.0 | FILL |
4-WHOLE CAKE ISLAND | 82.0 | FILL |
5-LEVELY | 85.0 | FILL |
6- WANO COUNTRY | 85.0 | FILL |
1-PUNK HAZARD | 75.0 | MEAN |
2-DRESSROSA | 80.5 | MEAN |
3-ZOU | 80.0 | MEAN |
4-WHOLE CAKE ISLAND | 82.0 | MEAN |
5-LEVELY | 80.5 | MEAN |
6- WANO COUNTRY | 85.0 | MEAN |
1-PUNK HAZARD | 75.0 | INTERPOLATE |
2-DRESSROSA | 77.5 | INTERPOLATE |
3-ZOU | 80.0 | INTERPOLATE |
4-WHOLE CAKE ISLAND | 82.0 | INTERPOLATE |
5-LEVELY | 83.5 | INTERPOLATE |
6- WANO COUNTRY | 85.0 | INTERPOLATE |
<- luffy08 %>%
luffy09 mutate_if(is.character, str_to_lower)
::kbl(luffy09) %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc | power | method |
---|---|---|
1-punk hazard | 75.0 | fill |
2-dressrosa | 80.0 | fill |
3-zou | 80.0 | fill |
4-whole cake island | 82.0 | fill |
5-levely | 85.0 | fill |
6- wano country | 85.0 | fill |
1-punk hazard | 75.0 | mean |
2-dressrosa | 80.5 | mean |
3-zou | 80.0 | mean |
4-whole cake island | 82.0 | mean |
5-levely | 80.5 | mean |
6- wano country | 85.0 | mean |
1-punk hazard | 75.0 | interpolate |
2-dressrosa | 77.5 | interpolate |
3-zou | 80.0 | interpolate |
4-whole cake island | 82.0 | interpolate |
5-levely | 83.5 | interpolate |
6- wano country | 85.0 | interpolate |
<- luffy09 %>%
luffy10 mutate_all(., .funs = toupper)
::kbl(luffy10) %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc | power | method |
---|---|---|
1-PUNK HAZARD | 75 | FILL |
2-DRESSROSA | 80 | FILL |
3-ZOU | 80 | FILL |
4-WHOLE CAKE ISLAND | 82 | FILL |
5-LEVELY | 85 | FILL |
6- WANO COUNTRY | 85 | FILL |
1-PUNK HAZARD | 75 | MEAN |
2-DRESSROSA | 80.5 | MEAN |
3-ZOU | 80 | MEAN |
4-WHOLE CAKE ISLAND | 82 | MEAN |
5-LEVELY | 80.5 | MEAN |
6- WANO COUNTRY | 85 | MEAN |
1-PUNK HAZARD | 75 | INTERPOLATE |
2-DRESSROSA | 77.5 | INTERPOLATE |
3-ZOU | 80 | INTERPOLATE |
4-WHOLE CAKE ISLAND | 82 | INTERPOLATE |
5-LEVELY | 83.5 | INTERPOLATE |
6- WANO COUNTRY | 85 | INTERPOLATE |
#drawback: converting numeric to character
<- luffy09 %>%
luffy11 mutate(across(where(is.character),
toupper))
::kbl(luffy11) %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc | power | method |
---|---|---|
1-PUNK HAZARD | 75.0 | FILL |
2-DRESSROSA | 80.0 | FILL |
3-ZOU | 80.0 | FILL |
4-WHOLE CAKE ISLAND | 82.0 | FILL |
5-LEVELY | 85.0 | FILL |
6- WANO COUNTRY | 85.0 | FILL |
1-PUNK HAZARD | 75.0 | MEAN |
2-DRESSROSA | 80.5 | MEAN |
3-ZOU | 80.0 | MEAN |
4-WHOLE CAKE ISLAND | 82.0 | MEAN |
5-LEVELY | 80.5 | MEAN |
6- WANO COUNTRY | 85.0 | MEAN |
1-PUNK HAZARD | 75.0 | INTERPOLATE |
2-DRESSROSA | 77.5 | INTERPOLATE |
3-ZOU | 80.0 | INTERPOLATE |
4-WHOLE CAKE ISLAND | 82.0 | INTERPOLATE |
5-LEVELY | 83.5 | INTERPOLATE |
6- WANO COUNTRY | 85.0 | INTERPOLATE |
<- luffy11 %>%
luffy12 mutate(across(where(is.character),
tolower))
::kbl(luffy12) %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc | power | method |
---|---|---|
1-punk hazard | 75.0 | fill |
2-dressrosa | 80.0 | fill |
3-zou | 80.0 | fill |
4-whole cake island | 82.0 | fill |
5-levely | 85.0 | fill |
6- wano country | 85.0 | fill |
1-punk hazard | 75.0 | mean |
2-dressrosa | 80.5 | mean |
3-zou | 80.0 | mean |
4-whole cake island | 82.0 | mean |
5-levely | 80.5 | mean |
6- wano country | 85.0 | mean |
1-punk hazard | 75.0 | interpolate |
2-dressrosa | 77.5 | interpolate |
3-zou | 80.0 | interpolate |
4-whole cake island | 82.0 | interpolate |
5-levely | 83.5 | interpolate |
6- wano country | 85.0 | interpolate |
<- luffy09 %>%
luffy13 mutate(across(arc, tolower),
across(method, toupper),
across(power, round)
)::kbl(luffy13) %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc | power | method |
---|---|---|
1-punk hazard | 75 | FILL |
2-dressrosa | 80 | FILL |
3-zou | 80 | FILL |
4-whole cake island | 82 | FILL |
5-levely | 85 | FILL |
6- wano country | 85 | FILL |
1-punk hazard | 75 | MEAN |
2-dressrosa | 80 | MEAN |
3-zou | 80 | MEAN |
4-whole cake island | 82 | MEAN |
5-levely | 80 | MEAN |
6- wano country | 85 | MEAN |
1-punk hazard | 75 | INTERPOLATE |
2-dressrosa | 78 | INTERPOLATE |
3-zou | 80 | INTERPOLATE |
4-whole cake island | 82 | INTERPOLATE |
5-levely | 84 | INTERPOLATE |
6- wano country | 85 | INTERPOLATE |
<- luffy13 %>%
luffy14 mutate(
power2 = gsub("[0-5]*", "", power), #taking out number 1 to 5
arc2 = gsub(".*^", "0", arc), #adding 0 before the first number
method2 = gsub("LL", "zz", method) #replacing LL to zz
)
::kbl(luffy14) %>%
kableExtra::kable_styling(bootstrap_options = "striped",
kableExtrafull_width = F,
position = "left")
arc | power | method | power2 | arc2 | method2 |
---|---|---|---|---|---|
1-punk hazard | 75 | FILL | 7 | 01-punk hazard | FIzz |
2-dressrosa | 80 | FILL | 8 | 02-dressrosa | FIzz |
3-zou | 80 | FILL | 8 | 03-zou | FIzz |
4-whole cake island | 82 | FILL | 8 | 04-whole cake island | FIzz |
5-levely | 85 | FILL | 8 | 05-levely | FIzz |
6- wano country | 85 | FILL | 8 | 06- wano country | FIzz |
1-punk hazard | 75 | MEAN | 7 | 01-punk hazard | MEAN |
2-dressrosa | 80 | MEAN | 8 | 02-dressrosa | MEAN |
3-zou | 80 | MEAN | 8 | 03-zou | MEAN |
4-whole cake island | 82 | MEAN | 8 | 04-whole cake island | MEAN |
5-levely | 80 | MEAN | 8 | 05-levely | MEAN |
6- wano country | 85 | MEAN | 8 | 06- wano country | MEAN |
1-punk hazard | 75 | INTERPOLATE | 7 | 01-punk hazard | INTERPOLATE |
2-dressrosa | 78 | INTERPOLATE | 78 | 02-dressrosa | INTERPOLATE |
3-zou | 80 | INTERPOLATE | 8 | 03-zou | INTERPOLATE |
4-whole cake island | 82 | INTERPOLATE | 8 | 04-whole cake island | INTERPOLATE |
5-levely | 84 | INTERPOLATE | 8 | 05-levely | INTERPOLATE |
6- wano country | 85 | INTERPOLATE | 8 | 06- wano country | INTERPOLATE |
<- read_excel(path = "/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_all.xlsx", sheet = "OP_ratings")
op_ratings
glimpse(op_ratings)
## Rows: 958
## Columns: 8
## $ rank <dbl> 24129, 29290, 32043, 28818, 37113, 36209, 37648, 38371,…
## $ trend <chr> "18", "11", "7", "8", "4", "4", "4", "6", "5", "4", "4"…
## $ season <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ episode <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, …
## $ name <chr> "I'm Luffy! The Man Who Will Become the Pirate King!", …
## $ start <dbl> 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 2000, 2…
## $ total_votes <dbl> 647, 473, 428, 449, 370, 364, 344, 335, 327, 314, 310, …
## $ average_rating <dbl> 7.6, 7.8, 7.7, 8.1, 7.5, 7.7, 7.7, 7.7, 7.3, 7.5, 7.4, …
(<- op_ratings %>%
op_ratings02 select(episode, start, total_votes, average_rating) %>%
pivot_longer(cols = total_votes:average_rating,
names_to = "variable",
values_to = "value")
)
::p_load(RColorBrewer)
pacman
#RColorBrewer
display.brewer.all(type = "qual")
display.brewer.all(type = "seq")
display.brewer.all(type = "div")
#display.brewer.pal(n = 12, name = "Set2")
#brewer.pal(n = 12, name = "Set3")
#display.brewer.pal(n = 11, name = "RdBu")
::p_load(nord)
pacman
nord_show_palette("snowstorm")
nord_show_palette("frost")
nord_show_palette("aurora")
nord_show_palette("lumina")
nord_show_palette("mountain_forms")
nord_show_palette("silver_mine")
nord_show_palette("lake_superior")
nord_show_palette("victory_bonds")
nord_show_palette("halifax_harbor")
nord_show_palette("moose_pond")
nord_show_palette("algoma_forest")
nord_show_palette("rocky_mountain")
nord_show_palette("red_mountain")
nord_show_palette("baie_mouton")
nord_show_palette("afternoon_prarie")
#hex-code: https://www.nordtheme.com/docs/colors-and-palettes
::p_load(wesanderson)
pacman
#names(wes_palettes)
wes_palette("BottleRocket1")
wes_palette("BottleRocket2")
wes_palette("Rushmore1")
wes_palette("Royal1")
wes_palette("Royal2")
wes_palette("Zissou1")
wes_palette("Darjeeling1")
wes_palette("Darjeeling2")
wes_palette("Chevalier1")
wes_palette("FantasticFox1")
wes_palette("Moonrise1")
wes_palette("Moonrise2")
wes_palette("Moonrise3")
wes_palette("Cavalcanti1")
wes_palette("GrandBudapest1")
wes_palette("GrandBudapest2")
wes_palette("IsleofDogs1")
wes_palette("IsleofDogs2")
#hex-code: https://github.com/karthik/wesanderson/blob/master/R/colors.R
#scales::show_col(wes_palette("IsleofDogs2"))
::p_load(viridis)
pacman
::show_col(viridis_pal(option = "viridis")(6)) scales
::show_col(viridis_pal(option = "plasma")(6)) scales
::show_col(viridis_pal(option = "rocket")(6)) scales
::show_col(viridis_pal(option = "magma")(6)) scales
::show_col(viridis_pal(option = "rocket")(6)) scales
::show_col(viridis_pal(option = "mako")(6)) scales
::show_col(viridis_pal(option = "inferno")(6)) scales
::hcl_palettes(type = "sequential", plot = TRUE) colorspace
::hcl_palettes(type = "diverging", plot = TRUE, n = 9) colorspace
#colorspace::divergingx_palettes(plot = TRUE, n = 9)
<- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2",
(OkabeIto "#D55E00", "#CC79A7", "#000000"))
## [1] "#E69F00" "#56B4E9" "#009E73" "#F0E442" "#0072B2" "#D55E00" "#CC79A7"
## [8] "#000000"
#https://colorschemedesigner.com/csd-3.5/
::pal.bands(OkabeIto) pals
#Source: https://jfly.uni-koeln.de/color/#pallet
#Source: https://davidmathlogic.com/colorblind/#%23D81B60-%231E88E5-%23FFC107-%23004D40
(<- op_ratings02 %>%
line01 ggplot(aes(x = episode, y = value)) +
geom_line()
)
(<- op_ratings02 %>%
line02 ggplot(aes(x = episode, y = value)) +
geom_line() +
facet_wrap(~variable, scales = "free", nrow = 2)
)
(<- op_ratings02 %>%
line02 #modify the dataset
filter(start > 2015 & start < 2021) %>%
mutate(variable = recode(variable,
average_rating = "Average Rating",
total_votes = "Total Votes")) %>%
#plot the dataset
ggplot(aes(x = episode, y = value)) +
facet_wrap(~variable, scales = "free", nrow = 2) +
geom_line(color = "#0072B2") + #colorblind-friendly
theme_test() #could be explored even further
)
(<- op_ratings02 %>%
line02 #modify the dataset
filter(start > 2015 & start < 2021) %>%
mutate(variable = recode(variable,
average_rating = "Average Rating",
total_votes = "Total Votes")) %>%
#plot the dataset
ggplot(aes(x = episode, y = value)) +
facet_wrap(~variable, scales = "free", nrow = 2) +
geom_line(aes(color = start)) +
theme_test() #could be explored even further
)
(<- op_ratings02 %>%
line02 #modify the dataset
filter(start > 2015 & start < 2021) %>%
mutate(variable = recode(variable,
average_rating = "Average Rating",
total_votes = "Total Votes")) %>%
#plot the dataset
ggplot(aes(x = episode, y = value)) +
facet_wrap(~variable, scales = "free", nrow = 2) +
geom_line(aes(color = factor(start))) +
::scale_color_colorblind() +
ggthemestheme_test() #could be explored even further
)
(<- op_ratings02 %>%
line02 #modify the dataset
filter(start > 2015 & start < 2021) %>%
mutate(variable = recode(variable,
average_rating = "Average Rating",
total_votes = "Total Votes")) %>%
#plot the dataset
ggplot(aes(x = episode, y = value)) +
facet_wrap(~variable, scales = "free", nrow = 2) +
geom_line(aes(color = factor(variable))) +
scale_color_manual(values = c("blue", "red")) +
theme_test() #could be explored even further
)
(<- op_ratings02 %>%
line03 filter(start > 2015 & start < 2021) %>%
mutate(
variable = recode(variable,
average_rating = "Average Rating",
total_votes = "Total Votes")
%>%
) ggplot(aes(x = episode, y = value)) +
geom_line(aes(color = factor(start))) +
#scale_color_brewer(palette = "Set1") +
::scale_color_nord("aurora", discrete = TRUE) +
nordfacet_wrap(
~ variable,
scales = "free",
nrow = 2,
strip.position = "right"
+
) theme_test() +
theme(
legend.position = "bottom",
plot.title = element_text(hjust = 0, size = 16, face = "bold"),
plot.subtitle = element_text(hjust = 0, size = 14),
plot.caption = element_text(hjust = 0, size = 8),
plot.title.position = "plot",
plot.caption.position = "plot"
+
) labs(
title = "One Piece",
subtitle = "Ratings and Votes",
caption = "\nSource: https://www.kaggle.com/aditya2803",
x = "Episode",
y = ""
+
) guides(
color = guide_legend(title = "Year release:"),
fill = guide_legend(title = "Year release:")
) )
(<- op_ratings02 %>%
line03 filter(start > 2015 & start < 2021) %>%
mutate(variable = recode(variable, average_rating = "Average Rating",
total_votes = "Total Votes")
%>%
) ggplot(aes(x = episode, y = value)) +
facet_wrap(vars(variable), nrow = 2, scales = "free",
as.table = TRUE, strip.position = "right") +
geom_line(aes(color = factor(start))) +
scale_color_brewer(palette = "Set1") +
theme_classic() +
#ggthemes::theme_economist() +
theme(legend.position = "bottom",
plot.title = element_text(hjust = 0, size = 16,
face = "bold"),
plot.subtitle = element_text(hjust = 0, size = 13),
plot.caption = element_text(hjust = 0, size = 8),
plot.title.position = "plot",
plot.caption.position = "plot",) +
labs(title = "One Piece",
subtitle = "Ratings and Votes",
y = "",
x = "Episode",
caption = "\nSource: https://www.kaggle.com/aditya2803") +
geom_point(aes(color = factor(start), fill = factor(start)),
shape = 21, size = 1, alpha = 0.5) +
scale_fill_brewer(palette = "Set1") +
guides(color = guide_legend(title = "Year release:"),
fill = guide_legend(title = "Year release:"))
)
(<- db01c %>%
db02 arrange(desc(power)) %>%
slice_max(power, n = 15, with_ties = TRUE)
)
(<- ggplot(db02, mapping = aes(x = character, y = age))
point01 )
## Point chart: Flipping
(+ coord_flip()
point01 )
## Point chart: Assigning the function
+
point01 coord_flip() +
geom_point()
## Point chart: Defining the color
(+
point01 coord_flip() +
geom_point(color = "blue",
shape = 0,
size = 5)
)
(+
point01 coord_flip() +
geom_point(color = "blue",
shape = 21,
size = 3)
)
## Point chart: Size
(<- db02 %>%
point02 ggplot(aes(character, age, size = power)) +
coord_flip() +
geom_point()
)
(<- db02 %>%
point03 ggplot(aes(reorder(character, power), age, size = power)) +
coord_flip() +
geom_point()
)
<- op_powers %>%
(point04 left_join(op_ages, by = c("character" = "name")) %>%
relocate(character, age, power) %>%
#group_by(level) %>%
slice_max(power, n = 15, with_ties = FALSE) %>%
#ungroup() %>%
ggplot(aes(x = reorder(character, power),
y = age)) +
geom_point(aes(color = factor(affiliations), size = power)) +
#coord_cartesian(expand = TRUE, clip = "on") +
coord_flip() +
::scale_color_nord("aurora") +
nordxlab("") +
ylab("Age") +
theme_test() +
theme(
axis.text.x = element_text(size = 8),
axis.ticks.x = element_blank(),
axis.line.x = element_blank(),
axis.title.x = element_text(size = 7),
axis.line.y = element_blank(),
axis.title.y = element_text(size = 7),
axis.text.y = element_text(size = 8),
plot.title.position = "plot",
plot.caption.position = "plot",
legend.title = element_text(size = 7),
legend.text = element_text(size = 7),
legend.key.size = unit(0.5, "cm"),
legend.spacing = unit(1, "cm"),
plot.title = element_text(hjust = 0, size = 13, face = "bold"),
plot.subtitle = element_text(hjust = 0, size = 12),
plot.caption = element_text(hjust = 0, size = 12),
# legend.title = element_blank(),
legend.position = "right",
strip.text.x = element_text(size = 8),
panel.grid.major.y = element_line(colour = "grey97"),
panel.ontop = FALSE,
panel.spacing = unit(1, "lines")
+
) guides(size = guide_legend("Power"), color = guide_legend("Affiliation")) +
labs(title = "The top 15-most powerful characters",
subtitle = "",
caption = "") +
scale_size_continuous(range = c(1, 5)) +
scale_y_continuous(limits = c(10, 90),
breaks = c(20, 50, 80))
)
<- op_powers %>%
(point05 left_join(op_ages, by = c("character" = "name")) %>%
relocate(character, age, power) %>%
#group_by(level) %>%
slice_max(power, n = 15, with_ties = FALSE) %>%
#ungroup() %>%
ggplot(aes(x = reorder(character, power),
y = age)) +
geom_point(aes(color = factor(affiliations), size = power)) +
coord_cartesian(expand = TRUE, clip = "on") +
coord_flip() +
::scale_color_nord("aurora") +
nordxlab("") +
ylab("Age") +
theme_test() +
theme(
axis.text.x = element_text(size = 8),
axis.ticks.x = element_blank(),
axis.line.x = element_blank(),
axis.title.x = element_text(size = 7),
axis.line.y = element_blank(),
axis.title.y = element_text(size = 7),
axis.text.y = element_text(size = 8),
plot.title.position = "plot",
plot.caption.position = "plot",
legend.title = element_text(size = 7),
legend.text = element_text(size = 7),
legend.key.size = unit(0.5, "cm"),
legend.spacing = unit(1, "cm"),
plot.title = element_text(hjust = 0, size = 13, face = "bold"),
plot.subtitle = element_text(hjust = 0, size = 12),
plot.caption = element_text(hjust = 0, size = 12),
# legend.title = element_blank(),
legend.position = "right",
strip.text.x = element_text(size = 8),
panel.grid.major.y = element_line(colour = "grey97"),
panel.ontop = FALSE,
panel.spacing = unit(1, "lines")
+
) guides(size = guide_legend("Power"), color = guide_legend("Affiliation")) +
labs(title = "The top 15-most powerful characters",
subtitle = "",
caption = "") +
scale_size_continuous(range = c(1, 5)) +
scale_y_continuous(limits = c(10, 90),
breaks = c(20, 50, 80)) +
facet_grid(. ~ level)
)
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
ggsave(
filename = "point05.png",
#filename = "point05.pdf",
plot = point05,
#device = cairo_pdf,
width = 297,
height = 210,
units = "mm"
)
<- op_powers %>%
point06 left_join(op_ages, by = c("character" = "name")) %>%
relocate(character, age, power) %>%
#group_by(level) %>%
slice_max(power, n = 15, with_ties = FALSE) %>%
#ungroup() %>%
mutate(text = paste(
"Character: ", reorder(character, power),
"\nAffiliation: ", factor(affiliations),
"\nAge: ", age,
"\nPower: ", power,
sep = "")) %>%
ggplot(aes(x = reorder(character, power),
y = factor(affiliations),
text = text)) +
geom_point(aes(color = age, size = power)) +
coord_cartesian(expand = TRUE, clip = "on") +
coord_flip() +
#nord::scale_color_nord("victory_bonds", discrete = FALSE) +
::scale_colour_viridis(option = "viridis", direction = -1) +
viridisxlab("") +
ylab("") +
theme_test() +
theme(
axis.text.x = element_text(
size = 8,
angle = 90,
vjust = 0.5,
hjust = 1
),axis.ticks.x = element_blank(),
axis.line.x = element_blank(),
axis.title.x = element_text(size = 7),
axis.line.y = element_blank(),
axis.title.y = element_text(size = 7),
axis.text.y = element_text(size = 8),
plot.title.position = "plot",
plot.caption.position = "plot",
legend.title = element_text(size = 7),
legend.text = element_text(size = 7),
legend.key.size = unit(0.5, "cm"),
legend.spacing = unit(1, "cm"),
plot.title = element_text(hjust = 0, size = 13, face = "bold"),
plot.subtitle = element_text(hjust = 0, size = 12),
plot.caption = element_text(hjust = 0, size = 12),
# legend.title = element_blank(),
legend.position = "none",
strip.text.x = element_text(size = 8),
panel.grid.major.y = element_line(colour = "grey97"),
panel.ontop = FALSE
+
) guides(size = guide_legend("Power"), color = guide_colorbar("Age")) +
labs(title = "",
subtitle = "",
caption = "") +
scale_size_continuous(range = c(1, 5))
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
ggplotly(point06, tooltip = "text")
Figure 3: The top 15-most powerful characters, without facet_grid(. ~ level)
<- op_powers %>%
point07 left_join(op_ages, by = c("character" = "name")) %>%
relocate(character, age, power) %>%
#group_by(level) %>%
slice_max(power, n = 15, with_ties = FALSE) %>%
#ungroup() %>%
mutate(text = paste(
"Character: ", reorder(character, power),
"\nAffiliation: ", factor(affiliations),
"\nAge: ", age,
"\nPower: ", power,
sep = "")) %>%
ggplot(aes(x = reorder(character, power),
y = factor(affiliations),
text = text)) +
geom_point(aes(color = age, size = power)) +
coord_cartesian(expand = TRUE, clip = "on") +
coord_flip() +
#nord::scale_color_nord("victory_bonds", discrete = FALSE) +
::scale_colour_viridis(option = "viridis", direction = -1) +
viridisxlab("") +
ylab("") +
theme_test() +
theme(
axis.text.x = element_text(size = 8, angle = 90, vjust = 0.5, hjust=1),
axis.ticks.x = element_blank(),
axis.line.x = element_blank(),
axis.title.x = element_text(size = 7),
axis.line.y = element_blank(),
axis.title.y = element_text(size = 7),
axis.text.y = element_text(size = 8),
plot.title.position = "plot",
plot.caption.position = "plot",
legend.title = element_text(size = 7),
legend.text = element_text(size = 7),
legend.key.size = unit(0.5, "cm"),
legend.spacing = unit(1, "cm"),
plot.title = element_text(
hjust = 0, size = 13, face = "bold"
),plot.subtitle = element_text(hjust = 0, size = 12),
plot.caption = element_text(hjust = 0, size = 12),
# legend.title = element_blank(),
legend.position = "none",
strip.text.x = element_text(size = 8),
panel.grid.major.y = element_line(colour = "grey97"),
panel.ontop = FALSE,
panel.spacing = unit(1, "lines")
+
) guides(size = guide_legend("Power"), color = guide_colorbar("Age")) +
labs(
title = "",
subtitle = "",
caption = ""
+
) scale_size_continuous(range = c(1,5)) +
facet_grid(.~level)
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
ggplotly(point07, tooltip = "text")
Figure 4: The top 15-most powerful characters, with facet_grid(. ~ level)
#clearing the environment
ls()
## [1] "character_power" "db01a" "db01b" "db01c"
## [5] "db02" "db03" "db04" "db05"
## [9] "db06" "db07" "db08" "db09"
## [13] "db10a" "db10b" "db11" "law"
## [17] "line01" "line02" "line03" "luffy00"
## [21] "luffy01" "luffy02" "luffy03" "luffy04"
## [25] "luffy05" "luffy06" "luffy07" "luffy08"
## [29] "luffy09" "luffy10" "luffy11" "luffy12"
## [33] "luffy13" "luffy14" "OkabeIto" "op_ages"
## [37] "op_powers" "op_ratings" "op_ratings02" "point01"
## [41] "point02" "point03" "point04" "point05"
## [45] "point06" "point07" "status" "villain_aff"
rm(list=ls())
Contact me at ap.sulistiono@gmail.com