Skip to content

Instantly share code, notes, and snippets.

@McCartneyAC
Last active May 28, 2020 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McCartneyAC/123386fdc51c355e672dc1af6c0bd2af to your computer and use it in GitHub Desktop.
Save McCartneyAC/123386fdc51c355e672dc1af6c0bd2af to your computer and use it in GitHub Desktop.
Pull data on elements, then generate colorful periodic tables based on element properties
library(readr)
library(ggplot2)
library(dplyr)
elements<-read_csv("https://gist.githubusercontent.com/GoodmanSciences/c2dd862cd38f21b0ad36b8f96b4bf1ee/raw/1d92663004489a5b6926e944c1b3d9ec5c40900e/Periodic%2520Table%2520of%2520Elements.csv")
# correct mistaken Type Assignments
elements<-elements %>%
mutate(Type = if_else(Element == "Actinium", "Actinide", Type),
Type = if_else(Element == "Francium", "Alkali Metal", Type),
Type = if_else(Element == "Radium", "Alkaline Earth Metal", Type),
Type = if_else(Element == "Radon", "Noble Gas", Type),
Type = if_else(Element == "Astatine", "Halogen", Type),
Type = if_else(Element == "Polonium", "Metal", Type),
Type = if_else(AtomicNumber %in% 57:71, "Lanthanide", Type),
Type = if_else(AtomicNumber %in% 89:103, "Actinide", Type),
)
# empirically FALSE lanthanide and actinide period / group values:
lanth_act <- elements %>%
filter(Type %in% c("Lanthanide", "Actinide")) %>%
mutate(Period = if_else(Type == "Lanthanide", 9, 10)) %>%
group_by(Type) %>%
mutate(Group = row_number() + 2)
# function to generate p-table colored by variable given:
# function to generate p-table colored by variable given:
periodic_table<-function(quality){
p<-bind_rows(elements, lanth_act) %>%
ggplot(aes(x = Group, y = Period, label = Symbol)) +
geom_tile(color = "white", aes_string(fill = quality)) +
scale_y_reverse(breaks = 1:7) +
scale_x_continuous(breaks = 1:18, position = "top") +
theme(panel.background = element_blank())+
geom_text() +
labs(title = "The Periodic Table of the Elements")+
theme(
legend.position = "bottom",
legend.justification = "center",
legend.text = element_text(size = 9),
legend.box.spacing = unit(0, "pt")
)
if (quality == "Type"){
p + guides(fill = guide_legend(nrow = 2))
} else {
return(p)
}
}
# for any available variable, color the periodic table:
periodic_table("Type")
periodic_table("Electronegativity")
@McCartneyAC
Copy link
Author

McCartneyAC commented May 28, 2020

cleaner version of the function:

# function to generate p-table colored by variable given: 
periodic_table<-function(quality){
p<-bind_rows(elements, lanth_act) %>% 
  ggplot(aes(x = Group, y = Period, label = Symbol)) + 
  geom_tile(color = "white", aes_string(fill = quality)) + 
  scale_y_reverse(breaks = 1:7) + 
  scale_x_continuous(breaks = 1:18, position = "top") + 
  theme(panel.background = element_blank())+ 
  geom_text() + 
  labs(title = "The Periodic Table of the Elements")+
  theme(
    legend.position = "bottom",
    legend.justification = "center",
    legend.text = element_text(size = 9),
    legend.box.spacing = unit(0, "pt")
  )  
  if (quality == "Type"){
    p + guides(fill = guide_legend(nrow = 2))
  } else {
    return(p)
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment