We've been exploring the Tidy Tuesday datasets at our CaRdiff UseR group. It is good to look at a different types of data and the inspiration on Twitter. I tried to create a time line of volcano eruptions but it was a complete mess. Then I found this data visualisation by @ijeamaka_a which inspired me.
She very helpfully shared her code through Github so I was able to have a play with her code and use it to make some plots for other parts of the world and time frames.
Here is an example of a plot I have made which has eruptions in Indonesia between 1250 and 2020 with a volcano explosion index greater than 2.
Using this script it is possible to explore other places that have lots of earthquakes like Japan. This is a nice map of the volcanos in Japan.
START
## Inspiration from https://twitter.com/ijeamaka_a/status/1260660823229214724/photo/1
## https://github.com/Ijeamakaanyene/data_visualizations/blob/master/scripts/2020_10_volcanos.Rmd
library(dplyr)
library(tidyr)
library(ggplot2)
# download the data
volcano = readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-05-12/volcano.csv')
eruptions = readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-05-12/eruptions.csv')
# exploring the data a bit
## check countries with most eruptions...
erupt_by_country = left_join(eruptions, select(volcano, volcano_number, country, subregion),
by = c("volcano_number"="volcano_number")) %>%
group_by(country) %>%
count(sort = TRUE)
head(erupt_by_country)
## what size eruptions are most common...
count_erupt_size = left_join(eruptions, select(volcano, volcano_number, country, subregion),
by = c("volcano_number"="volcano_number")) %>%
group_by(vei) %>%
count(sort = TRUE)
## vei not available for lots of eruptions - historical I guess...
count_erupt_size
# create the bound data_set
all_eruptions_merged = left_join(eruptions, select(volcano, volcano_number,
country, subregion),
by = c("volcano_number"="volcano_number")) %>%
mutate(combo_year = as.numeric(paste(start_year, start_month, sep = ".")))
# allow some customization
## add your country, start year and end year
## and size of eruption from 1 to 7 or zero to see all.
my_country <- "Indonesia"
my_start_year <- 1250
my_end_year <- 2020
size <- 2
# filter merged data set...
eruptions_merged = all_eruptions_merged %>%
filter(country == my_country) %>%
filter(is.na(vei) == FALSE) %>%
filter(start_year > my_start_year) %>%
filter(start_year < my_end_year) %>%
filter(vei > size)
# Using for loop to create data needed to plot a geom_polygon
volcano_polygon_list = list()
years = unlist(eruptions_merged$combo_year)
volcano_ids = unlist(eruptions_merged$volcano_number)
veis = unlist(eruptions_merged$vei)
## There is most likely a better way of doing this.. but I cannot think of it!
## so for this to work on a wider than 20 year window we need to
## change the value for making the pyramid
## 0.25 is approx 1% of the number of decades
pyr_size <- (my_end_year - my_start_year)/100
for(i in 1:length(years)){
volcano_polygon_df = data.frame(
x = c(years[i], years[i] + pyr_size, years[i] + pyr_size*2),
y = c(0, veis[i], 0),
t = rep(volcano_ids[i], 3)
)
volcano_polygon_list[[i]] = volcano_polygon_df
}
# Converting into df and adding subregion information
volcano_polygon_df = volcano_polygon_list %>%
bind_rows() %>%
left_join(., select(eruptions_merged, volcano_number, subregion),
by = c("t" = "volcano_number"))
# create the plot
volcano_timeline = ggplot() +
geom_polygon(data = volcano_polygon_df, aes(x = x, y = y, group = t, fill = subregion),
alpha = 0.75, colour = "black") +
geom_segment(aes(y = 0, yend = 0, x = my_start_year, xend = my_end_year),
size = 1,
colour = "black",
arrow = arrow()) +
scale_x_continuous(limits = c(my_start_year, my_end_year),
expand = c(0.005, 0.005)) +
scale_y_continuous(expand = c(0, 0))
## add titles - based on the values put in...
volcano_timeline <- volcano_timeline +
labs(y = "Explosion Index", x = NULL, fill = NULL,
title = paste0("Volcanic Activity Timeline (vei>", size, ") ",
my_start_year, " to ", my_end_year, " within ", my_country),
subtitle = "Each triangle's height represents the erruptions volcanic explosion index.",
caption = paste0("Source: The Smithsonian Institution\n",
"Visualization: Paul Brennan | @brennanpcardiff adapted from Ijeamaka Anyene | @ijeamaka_a"))
## brown colour theme...
volcano_timeline + theme_bw() +
## separate out the colour scale to allow a change to this
scale_fill_manual(values = rcartocolor::carto_pal(n = 7, name = "BrwnYl"))
## Teal Green colour theme give the plot below...
volcano_timeline + theme_bw() +
## separate out the colour scale to allow a change to this
scale_fill_manual(values = rcartocolor::carto_pal(n = 7, name = "TealGrn"))
END
Resources
A site to help Biochemists learn R.
Starting points
Starting points for biochemists:
Showing posts with label Tidy Tuesday. Show all posts
Showing posts with label Tidy Tuesday. Show all posts
Monday, 18 May 2020
Volcano time line by Tidy Tuesday...
Labels:
geom_polygon,
ggplot,
Tidy Tuesday,
tidyverse,
volcano
Thursday, 30 April 2020
Exploring Phantom ticket prices with Tidy Tuesday...
Not biological data but an interesting Tidy Tuesday data set.
It is about Broadway Shows. Here is a plot that shows that the Phantom of the Opera seems to have become relative cheaper in a more diverse market.
START
# a script for Tidy Tuesday and Tidy Thursday at CaRdiff UseR group
# 30 April 2020
library(tidyverse)
# pull in the data
grosses <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-04-28/grosses.csv', guess_max = 40000)
# How many unique shows...
length(unique(grosses$show))
# 1122
# inspration from here: https://github.com/teunbrand/tidytuesdayscripts/blob/master/scripts/2020_04_28_Broadway_Grosses.R
# and image from here: https://twitter.com/TeunvandenBrand/status/1255253561535074306/photo/1
# first make basic graph = average ticket prices over time...
ggplot(grosses, aes(week_ending, avg_ticket_price)) +
geom_point(alpha = 0.25)
# $500 shows depresses the axis a bit...
grosses %>%
filter(avg_ticket_price > 400) -> expensive
# this show is Springsteen on Broadway at $500!!!
# can exclude by limiting the y-axis
# filter for the Phantom
grosses %>%
filter(show == "The Phantom of the Opera") %>%
ggplot(aes(week_ending, avg_ticket_price, colour=pct_capacity)) +
geom_point() +
geom_smooth() +
labs(x = "",
y = "Average Ticket Price ($)",
title = "Phantom of the Opera",
subtitle = "Data from Playbill via Tidy Tuesday")
# why does the show seem very expensive for some weeks rather than others
grosses %>%
filter(show == "Mamma Mia!") %>%
ggplot(aes(week_ending, avg_ticket_price, colour=pct_capacity)) +
geom_point() +
geom_smooth() +
labs(x = "",
y = "Average Ticket Price ($)",
title = "Phantom of the Opera",
subtitle = "Data from Playbill via Tidy Tuesday")
# how does Phantom of the Opera compare to the others?
# make one plot first
plot <- ggplot(grosses, aes(week_ending, avg_ticket_price)) +
geom_point(alpha = 0.25, size = 0.5) + ylim(0,300)
# filter Phantom
grosses %>%
filter(show == "The Phantom of the Opera") -> p_of_op
# add to the first plot...
plot2 <- plot +
geom_point(data = p_of_op, aes(week_ending, avg_ticket_price),
colour = "red")
# show with titles
plot2 + theme_bw() +
labs(x = "",
y = "Average Ticket Price ($)",
title = "Phantom of the Opera (red): relatively cleaper in a more diverse market",
subtitle = "More expensive some weeks!\nData from Playbill via Tidy Tuesday")
It is about Broadway Shows. Here is a plot that shows that the Phantom of the Opera seems to have become relative cheaper in a more diverse market.
START
# a script for Tidy Tuesday and Tidy Thursday at CaRdiff UseR group
# 30 April 2020
library(tidyverse)
# pull in the data
grosses <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-04-28/grosses.csv', guess_max = 40000)
# How many unique shows...
length(unique(grosses$show))
# 1122
# inspration from here: https://github.com/teunbrand/tidytuesdayscripts/blob/master/scripts/2020_04_28_Broadway_Grosses.R
# and image from here: https://twitter.com/TeunvandenBrand/status/1255253561535074306/photo/1
# first make basic graph = average ticket prices over time...
ggplot(grosses, aes(week_ending, avg_ticket_price)) +
geom_point(alpha = 0.25)
# $500 shows depresses the axis a bit...
grosses %>%
filter(avg_ticket_price > 400) -> expensive
# this show is Springsteen on Broadway at $500!!!
# can exclude by limiting the y-axis
# filter for the Phantom
grosses %>%
filter(show == "The Phantom of the Opera") %>%
ggplot(aes(week_ending, avg_ticket_price, colour=pct_capacity)) +
geom_point() +
geom_smooth() +
labs(x = "",
y = "Average Ticket Price ($)",
title = "Phantom of the Opera",
subtitle = "Data from Playbill via Tidy Tuesday")
# why does the show seem very expensive for some weeks rather than others
grosses %>%
filter(show == "Mamma Mia!") %>%
ggplot(aes(week_ending, avg_ticket_price, colour=pct_capacity)) +
geom_point() +
geom_smooth() +
labs(x = "",
y = "Average Ticket Price ($)",
title = "Phantom of the Opera",
subtitle = "Data from Playbill via Tidy Tuesday")
# how does Phantom of the Opera compare to the others?
# make one plot first
plot <- ggplot(grosses, aes(week_ending, avg_ticket_price)) +
geom_point(alpha = 0.25, size = 0.5) + ylim(0,300)
# filter Phantom
grosses %>%
filter(show == "The Phantom of the Opera") -> p_of_op
# add to the first plot...
plot2 <- plot +
geom_point(data = p_of_op, aes(week_ending, avg_ticket_price),
colour = "red")
# show with titles
plot2 + theme_bw() +
labs(x = "",
y = "Average Ticket Price ($)",
title = "Phantom of the Opera (red): relatively cleaper in a more diverse market",
subtitle = "More expensive some weeks!\nData from Playbill via Tidy Tuesday")
END
Resources:
- Official Repo for Tidy Tuesday
- Tidy Tuesday hash tag on twitter for inspiration
- Another Tidy Tuesday about Passwords
Wednesday, 15 January 2020
Numerate biochemists can make good passwords :-)
The great thing about programming skills is that they are very transferable. As part of my learning, I joined the Cardiff R User Group which allowed me to learn from professionals from other industries. For 2020, our user group is planning to host monthly workshops playing with the Tidy Tuesday datasets.
The dataset for this month is about passwords. Because my interest is data visualisation, I have created this graph. Basically my conclusion is that nerdy passwords with numbers are a good way to go. Other and better password advice is available.
Here is the code:
# START
library(tidyverse)
passwords <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-01-14/passwords.csv')
passwords <- passwords[1:500,]
# frequency of passwords
# interesting plot about the frequency of passwords...
ggplot(passwords, aes(str_length(password))) + geom_histogram()
# plot rank v strength
p <- ggplot(passwords, aes(rank, strength, colour = factor(category))) +
geom_point()
p
p + facet_wrap(~category) + theme(legend.position = "none")
# pays be nerdy and numeric...
# numbers are probably the key...
# want to see if they have numbers
# colour these by number
passwords$digit <- str_detect(passwords$password, "[1234567890]")
p1 <- ggplot(passwords, aes(rank, strength, colour = digit)) +
geom_point()
p1 + facet_wrap(~category)
# look at one category - pays to be a nerd?
passwords %>%
filter(category == "nerdy-pop") %>%
ggplot(aes(rank, strength, colour = digit)) + geom_point()
# what are the better passwords
# filter on nerdy-pop, simple-alphanumeric, sport and password-related
good_words_in <- c("nerdy-pop", "simple-alphanumeric", "sport", "password-related")
passwords %>%
filter(category %in% good_words_in) %>% # the %in% is important!!
ggplot(aes(rank, strength, colour = digit)) +
geom_point() + facet_wrap(~category)
# https://stackoverflow.com/questions/15624656/label-points-in-geom-point
passwords %>%
filter(category %in% good_words_in) %>% # the %in% is important!!
ggplot(aes(rank, strength, colour = digit)) +
geom_point() +
geom_text(aes(label=ifelse(strength>20,as.character(password),''),
vjust=1)) +
facet_wrap(~category)
# works but labels are not the best :-(
# try ggrepel
# https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html
library(ggrepel)
passwords %>%
filter(category %in% good_words_in) %>% # the %in% is important!!
ggplot(aes(rank, strength, colour = digit, label = password)) +
geom_point() + geom_text_repel()
# this works but is a mess!!!!
# need to remove labels for strength < 20.
# create a data set with strength greater than 20
passwords %>%
filter(strength >20) -> better_passwords
# overlay these labels with a different dataset...
passwords %>%
filter(category %in% good_words_in) %>%
ggplot(aes(rank, strength, colour = digit)) +
geom_point() -> graphs
graphs
graphs + facet_wrap(~category)
g_label <- graphs + geom_text_repel(data = better_passwords,
aes(rank, strength, label=password))
g_label
# works nicely
g_label + facet_wrap(~category)
# works nicely
link <- "https://github.com/rfordatascience/tidytuesday/blob/master/data/2020/2020-01-14/readme.md"
# add some styling...
g_label + facet_wrap(~category) +
labs(x = "Password Rank",
y = "Password Strength",
title = "Exploring Passwords for Tidy Tuesday",
subtitle = link) +
theme_bw()
# this is my final plot, I think!!
Some resources:
The dataset for this month is about passwords. Because my interest is data visualisation, I have created this graph. Basically my conclusion is that nerdy passwords with numbers are a good way to go. Other and better password advice is available.
Here is the code:
# START
library(tidyverse)
passwords <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-01-14/passwords.csv')
passwords <- passwords[1:500,]
# frequency of passwords
# interesting plot about the frequency of passwords...
ggplot(passwords, aes(str_length(password))) + geom_histogram()
# plot rank v strength
p <- ggplot(passwords, aes(rank, strength, colour = factor(category))) +
geom_point()
p
p + facet_wrap(~category) + theme(legend.position = "none")
# pays be nerdy and numeric...
# numbers are probably the key...
# want to see if they have numbers
# colour these by number
passwords$digit <- str_detect(passwords$password, "[1234567890]")
p1 <- ggplot(passwords, aes(rank, strength, colour = digit)) +
geom_point()
p1 + facet_wrap(~category)
# look at one category - pays to be a nerd?
passwords %>%
filter(category == "nerdy-pop") %>%
ggplot(aes(rank, strength, colour = digit)) + geom_point()
# what are the better passwords
# filter on nerdy-pop, simple-alphanumeric, sport and password-related
good_words_in <- c("nerdy-pop", "simple-alphanumeric", "sport", "password-related")
passwords %>%
filter(category %in% good_words_in) %>% # the %in% is important!!
ggplot(aes(rank, strength, colour = digit)) +
geom_point() + facet_wrap(~category)
# https://stackoverflow.com/questions/15624656/label-points-in-geom-point
passwords %>%
filter(category %in% good_words_in) %>% # the %in% is important!!
ggplot(aes(rank, strength, colour = digit)) +
geom_point() +
geom_text(aes(label=ifelse(strength>20,as.character(password),''),
vjust=1)) +
facet_wrap(~category)
# works but labels are not the best :-(
# try ggrepel
# https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html
library(ggrepel)
passwords %>%
filter(category %in% good_words_in) %>% # the %in% is important!!
ggplot(aes(rank, strength, colour = digit, label = password)) +
geom_point() + geom_text_repel()
# this works but is a mess!!!!
# need to remove labels for strength < 20.
# create a data set with strength greater than 20
passwords %>%
filter(strength >20) -> better_passwords
# overlay these labels with a different dataset...
passwords %>%
filter(category %in% good_words_in) %>%
ggplot(aes(rank, strength, colour = digit)) +
geom_point() -> graphs
graphs
graphs + facet_wrap(~category)
g_label <- graphs + geom_text_repel(data = better_passwords,
aes(rank, strength, label=password))
g_label
# works nicely
g_label + facet_wrap(~category)
# works nicely
link <- "https://github.com/rfordatascience/tidytuesday/blob/master/data/2020/2020-01-14/readme.md"
# add some styling...
g_label + facet_wrap(~category) +
labs(x = "Password Rank",
y = "Password Strength",
title = "Exploring Passwords for Tidy Tuesday",
subtitle = link) +
theme_bw()
# this is my final plot, I think!!
Some resources:
- Tidy Tuesday on Github
- Tidy Tuesday on Twitter
- Examples from ggrepel
- Another article using geom_point()
Subscribe to:
Posts (Atom)
























