If we have data of different values of some element, we want showing it on the periodic table by colour.
such as the electrical conductivity of some elements:
ECond <- read.csv(file = "element_data.csv")
head(ECond)
## Symbol ElectricalConductivity
## 1 Li 0.11
## 2 Be 0.31
## 3 B 0.00
## 4 C 0.00
## 5 Na 0.21
## 6 Mg 0.23
summary(ECond)
## Symbol ElectricalConductivity
## Ac : 1 Min. :0.0000
## Ag : 1 1st Qu.:0.0100
## Al : 1 Median :0.0400
## Am : 1 Mean :0.0896
## Au : 1 3rd Qu.:0.1100
## B : 1 Max. :0.6300
## (Other):69
We can regard the plot as a special scatter: the point is a big rectangle and their position is x(Group) and y(Period).
So, we lack the position data, here load it:
ETable <- read.csv(file = "e_table_basic.csv")
head(ETable)
## Symbol Graph.Group Graph.Period
## 1 H 1 1
## 2 He 18 1
## 3 Li 1 2
## 4 Be 2 2
## 5 B 13 2
## 6 C 14 2
We make the data into a function and adjust the scatter plot
The plot function is inspired by CHEPEC’s work: https://chepec.se/2014/11/16/element-data/#
Library this two packages before you run it: library(ggplot2) library(dplyr)
############## Periodic table plot : width = 8.2,height = 4.7 ############################
ggptable <- function(data, ascolour, breaks, limits, label = label){
# load the basic data of element positions: symbol; x; y.
ETable <- read.csv(file = "e_table_basic.csv")
# join the as-plot data to the basic data and plot it
data[,"ascolour"] <- data[,ascolour]
left_join(ETable, data, by = "Symbol") %>%
ggplot() +
# set the gray background of each element
geom_point(aes(y = Graph.Period, x = Graph.Group),size = 14,shape = 15,color = "gray90") +
# color the element with the value
geom_point(aes(y = Graph.Period, x = Graph.Group, colour = ascolour),size = 13,shape = 15) +
# put all the symbol on the table
geom_text(colour = "black",size = 4,fontface = "bold",aes(label = Symbol, y = Graph.Period, x = Graph.Group)) +
# set the scale
scale_x_continuous(breaks = seq(min(ETable$Graph.Group),
max(ETable$Graph.Group)),
limits = c(min(ETable$Graph.Group) - 1,
max(ETable$Graph.Group) + 1),
expand = c(0,0)) +
scale_y_continuous(trans = "reverse", # y axis is reverse
breaks = seq(min(ETable$Graph.Period),
max(ETable$Graph.Period)),
limits = c(max(ETable$Graph.Period) + 1,
min(ETable$Graph.Period) - 1.5),
expand = c(0,0)) +
# set breaks and labels in the colourbar legend
scale_colour_gradientn( breaks = breaks,
limits = limits,
colours = cm.colors(20),
# colour if value is NA
na.value = "grey70") +
# plot title (usually property and unit)
annotate("text", x = 8, y = 0.6,
vjust = 0,
label = label,
# parse required if using superscripts or subscripts
parse = F) +
# detail adjust for the scitific theme
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.margin = unit(c(0, 0, -0.85, -0.85), "line"),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.background = element_blank(),
plot.background = element_rect(fill="transparent", colour=NA),
# center (roughly) over transition metal block
legend.position = c(0.42, 0.91),
legend.justification = c(0.5, 1),
legend.direction = "horizontal",
# make the legend colourbar a little longer
legend.key.width = unit(2.5, "line"),
legend.title = element_blank(),
legend.background = element_rect(fill = "transparent"))
}
Here we use the function to make the plot. Change the page size for looking better: fig.height=4.7 inch, fig.width=8.2 inch. The page size depends on the rectangle point size of the elements.
ggptable(data = ECond,
ascolour = "ElectricalConductivity",
breaks = c(0, 0.2, 0.4, 0.6),
limits = c(0,0.65),
label = "Electrical Conductivity"
)
You can adjust more detail if you learned the ggplot2 package of R.