library(tidyverse)
library(tidytuesdayR)Where Rubbish Ends Up
For this week’s #tidytuesday, I visualise the vast dataset of rubbish collected on the beaches all over the world.
The technical details can be viewed below the embedded tweet.
The data shows the headquarters for each company identified, and the lines show where the trash ended up. The more dots that move along the line, the more junk found in those selected companies’ locations.
You can play around with the final result at the bottom of this page.
The post can be seen below.
TidyTuesday
Join the R4DS Online Learning Community in the weekly #TidyTuesday event! Every week we post a raw dataset, a chart or article related to that dataset, and ask you to explore the data. While the dataset will be “tamed”, it will not always be tidy! As such you might need to apply various R for Data Science techniques to wrangle the data into a true tidy format. The goal of TidyTuesday is to apply your R skills, get feedback, explore other’s work, and connect with the greater #RStats community! As such we encourage everyone of all skills to participate!
Load the weekly Data
Dowload the weekly data and make available in the tt object.
tt <- tt_load("2021-01-26")Readme
Take a look at the readme for the weekly data to get insight on the dataset. This includes a data dictionary, source, and a link to an article on the data.
ttGlimpse Data
Take an initial look at the format of the data available.
tt %>%
map(glimpse) tuesdata <- tt
tuesdata$plastics %>% dplyr::filter()
library(stringi)
plastics <- tuesdata$plastics %>%
filter(
!(parent_company %in% c("Grand Total", "null", "Null", "Unbranded"))
) %>%
mutate(
parent_company = parent_company %>%
tolower() %>%
stri_trans_general("Latin-ASCII") %>%
stri_trans_totitle(),
country = stringi::stri_trans_totitle(country)
) Then, we can get the top five polluters for the three years of analysis (2019-2020)
top_Co <- plastics %>%
group_by(parent_company) %>%
summarise(
country_count = n_distinct(country),
grand_total_sum = sum(grand_total, na.rm = T)
) %>%
arrange(-country_count, -grand_total_sum) %>%
head(5)
top_CoThen, we can manually obtain headquarters locations from https://www.crunchbase.com/lists/companies-search-with-headquarters
hq <- c(
"Atlanta, Georgia, United States", #Coca-Cola
"New York, New York, United States", #PepsiCo
"Vevey, Vaud, Switzerland", #Nestle
"Mclean, Virginia, United States", #Mars
"London, England, United Kingdom" #Unilever
)And we merge the data, and add a geocoded lat, long using tidygeocoder to finally transform into an sf.
library(tidygeocoder)
coords = geo(hq, method = "osm")countries = plastics %>%
filter(parent_company %in% top_Co$parent_company) %>%
group_by(country) %>%
summarise(count = n())coords_countries = geo(countries$country, method = "osm")
# Get Taiwan coordinates, which was not recognized
coords_taiwan = geo("Taiwan", method = "osm")
coords_country = coords_countries %>%
mutate(
lat = ifelse(address == "Taiwan_ Republic Of China (Roc)", coords_taiwan$lat, lat),
long = ifelse(address == "Taiwan_ Republic Of China (Roc)", coords_taiwan$long, long)
) Matching the countries with their lat / long.
countries <- countries %>%
left_join(coords_country, by = c("country" = "address"))
countriesMatching the companies with their lat / long
top_parent_companies = top_Co %>%
mutate(hq = hq, lat = coords$lat, long = coords$long) %>%
# st_as_sf(crs = 4326, coords = c("long", "lat")) %>%
mutate(parent_company = case_when(
parent_company == "The Coca-Cola Company" ~ "Coca-Cola",
parent_company == "Mars, Incorporated" ~ "Mars, Inc.",
TRUE ~ parent_company
)) %>%
select(name = parent_company) %>%
mutate(type = "Parent Company")
companies <- top_Co %>%
mutate(hq = hq, com_lat = coords$lat, com_long = coords$long)Joining the countries and the companies into one df.
com_countries <- plastics %>%
select(country, parent_company, grand_total)
# companies
plotting <- com_countries %>%
left_join(countries) %>%
left_join(companies) %>%
filter(parent_company %in% companies$parent_company)
plottingUsing the joined data for the final plot.
# remotes::install_github("JohnCoene/echarts4r.assets")
library(echarts4r)
library(echarts4r.assets)
plotting %>%
group_by(parent_company) %>%
e_charts() %>%
e_globe(
environment = gray(0.1),
base_texture = ea_asset("world"),
shading = 'lambert',
light.ambient = list(intensity = 10)
) %>%
e_lines_3d(
com_long,
com_lat,
long,
lat,
value = count,
source_name = parent_company,
target_name = country,
effect = list(show = TRUE)
) %>%
e_legend_toggle_select(name = "Company") %>%
e_legend(textStyle = list(color = gray(0.9)))