{"id":42,"date":"2019-06-07T08:47:58","date_gmt":"2019-06-07T06:47:58","guid":{"rendered":"https:\/\/www.benscoat.eu\/?p=42"},"modified":"2020-11-27T18:54:47","modified_gmt":"2020-11-27T17:54:47","slug":"how-to-produce-a-stacked-plot-of-radiocarbon-date-density-probabilities","status":"publish","type":"post","link":"https:\/\/www.benscoat.eu\/index.php\/2019\/06\/07\/how-to-produce-a-stacked-plot-of-radiocarbon-date-density-probabilities\/","title":{"rendered":"How to produce a stacked plot of radiocarbon date density probabilities"},"content":{"rendered":"\n<p>Since introduced by the middle of 20th century, radiocarbon dating has been used in a large variety of disciplines. In palaeoecology, it has allowed for a major improvement in dating past phenomena. The ground principle is fairly simple: the isotope 14 of carbon (14C) is radioactive and decays following one criterion, namely the <em>half-life<\/em>. The half-life means the time necessary for the original quantity to reduce to half. <strong>For 14C, it is about 5730 years!<\/strong><\/p>\n\n\n\n<p>Living organisms, just like you, accumulate carbon in their tissues all life long. This includes a certain proportion of radioactive carbon as well, but don&#8217;t be afraid, you don&#8217;t bare any risk \ud83d\ude42 This proportion is kept similar to the atmospheric level of radioactive carbon, but by the death of an organism, it is not renewed anymore and starts to decay. Half of it, every 5730 years. Therefore, if you can measure how much of radiocarbon is <em>left in a sample<\/em> today and how much of non-radioactive carbon it contains, you can use its half-life, do the reverse maths and deduce the date of this (sad) event.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>However, the proportion of radioactive carbon against non-radioactive carbon is not constant. It has actually frankly oscillated during the last dozen of thousand years. This is why the result of your above maths need to be corrected. You can achieve this with a <em>calibration curve<\/em>. You will get a probable time span in calendar years, but some years are more likely than others. This is the <em>density probability<\/em>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"683\" src=\"https:\/\/www.benscoat.eu\/wp-content\/uploads\/2020\/11\/One-Fantaisist-Radiocarbon-Date-Calibration-Curve-1024x683.jpg\" alt=\"\" class=\"wp-image-43\" srcset=\"https:\/\/www.benscoat.eu\/wp-content\/uploads\/2020\/11\/One-Fantaisist-Radiocarbon-Date-Calibration-Curve-1024x683.jpg 1024w, https:\/\/www.benscoat.eu\/wp-content\/uploads\/2020\/11\/One-Fantaisist-Radiocarbon-Date-Calibration-Curve-300x200.jpg 300w, https:\/\/www.benscoat.eu\/wp-content\/uploads\/2020\/11\/One-Fantaisist-Radiocarbon-Date-Calibration-Curve-768x512.jpg 768w, https:\/\/www.benscoat.eu\/wp-content\/uploads\/2020\/11\/One-Fantaisist-Radiocarbon-Date-Calibration-Curve-1536x1024.jpg 1536w, https:\/\/www.benscoat.eu\/wp-content\/uploads\/2020\/11\/One-Fantaisist-Radiocarbon-Date-Calibration-Curve-272x182.jpg 272w, https:\/\/www.benscoat.eu\/wp-content\/uploads\/2020\/11\/One-Fantaisist-Radiocarbon-Date-Calibration-Curve.jpg 1771w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption><em>Calibration probability density of one fantaisist radiocarbon age: 900\u00b140<\/em>.<\/figcaption><\/figure>\n\n\n\n<p>Nowadays, radiocarbon dating is rather cheap and fast, and scientists use them intensively. For instance, archaeologists find thousands of remains and radiocarbon dating ten or 100 or them can help figure out which period they come from. But unfortunately they won&#8217;t give the exact same result. An interesting approach to help making a decision is to pay attention to all these probabilities from the calibration of each date. All you need is love, of course, and R.<\/p>\n\n\n\n<p>First, the packages. Always.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"r\" class=\"language-r\">library(tidyverse)\nlibrary(clam)<\/code><\/pre>\n\n\n\n<p><code>tidyverse<\/code> is used to handle data, and the function <code>calibrate()<\/code> from the package <code>clam<\/code> is used to&#8230; Well, to calibrate the dates.<\/p>\n\n\n\n<p>Then, I generate some fantasists radiocarbon date results from the Middle-Age, but of course here you can use your own results.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"r\" class=\"language-r\">(dates &lt;- tibble(\n  id = replicate(7, str_c(\"Ftsy-\", str_c(sample(0:9, 5, replace = TRUE), collapse = \"\"), collapse = \"\")),\n  c14_age = sample(seq(800, 1100, 10), 7),\n  c14_error = sample(seq(20, 50, 5), 7)\n))<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>See how an extra pair of parentheses both creates and displays the object <code>dates<\/code> at the same time.<\/p><\/blockquote>\n\n\n\n<p>Next is to create a function that will calibrate a date, using the values it finds in the data frame you provide to it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"r\" class=\"language-r\">bunch_calibrate &lt;- function(x){\n  id &lt;- x %>% pull(id)\n  cage &lt;- x %>% pull(c14_age)\n  error &lt;- x %>% pull(c14_error)\n  calibrate(cage, error, storedat = TRUE, graph = FALSE)\n  dat$calib %>% \n    as_tibble() %>% \n    select(age = V1, density = V2) %>% \n    mutate(id = id)\n}<\/code><\/pre>\n\n\n\n<p>Once you have it, you&#8217;re all set to do the fancy calibrations <em>all at once!<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"r\" class=\"language-r\">(output &lt;- dates %>% \n    split(dates$id) %>% \n    lapply(bunch_calibrate) %>% \n    bind_rows())<\/code><\/pre>\n\n\n\n<p>Here it is a bit tricky so let&#8217;s break it down. <code>split()<\/code> will create a <em>list<\/em> of data frames according to the values found in the variable <em>id<\/em>. Then, our previous function <code>bunch_calibrate()<\/code> is applied piecewise to each single date (since each date is now a single-line data frame in the list created with <code>split()<\/code>). And finally, the results of each call to <code>bunch_calibrate()<\/code> are bind together with <code>bind_rows()<\/code>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Note that the package <code>purrr<\/code> could make it easier to implement the <em>split-apply-combine<\/em> strategy here, but I haven&#8217;t learnt about this package yet \ud83d\ude42<\/p><\/blockquote>\n\n\n\n<p>To go even fancier (I know, you wouldn&#8217;t have expect, right?), we order the dates by their weighted calibrated age :sunglasses:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"r\" class=\"language-r\">(date_order &lt;- output %>% \n  group_by(id) %>% \n  summarise(wght_age = weighted.mean(age, density)) %>% \n  mutate(id = fct_reorder(id, wght_age)) %>% \n  arrange(id) %>%\n  pull(id))\n\nfinal_output &lt;- output %>% \n  mutate(id = parse_factor(id, levels = date_order))<\/code><\/pre>\n\n\n\n<p>Finally, last thing to do is a <del>nice<\/del> wonderful plot:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"r\" class=\"language-r\">ggplot(final_output, aes(1950 - age, density, fill = id)) +\n  geom_area(position = \"stack\") +\n  theme(legend.position = c(0, 1), legend.justification = c(0, 1), legend.background = element_blank(), legend.key = element_blank()) +\n  scale_fill_brewer(palette = \"Set2\") +\n  labs(x = \"Age (cal. AD)\", y = \"Stacked calibration probability density curves\", fill = NULL)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"683\" src=\"https:\/\/www.benscoat.eu\/wp-content\/uploads\/2020\/11\/Stacked-Fantaisist-Radiocarbon-Date-Calibration-Curves-1024x683.jpg\" alt=\"\" class=\"wp-image-44\" srcset=\"https:\/\/www.benscoat.eu\/wp-content\/uploads\/2020\/11\/Stacked-Fantaisist-Radiocarbon-Date-Calibration-Curves-1024x683.jpg 1024w, https:\/\/www.benscoat.eu\/wp-content\/uploads\/2020\/11\/Stacked-Fantaisist-Radiocarbon-Date-Calibration-Curves-300x200.jpg 300w, https:\/\/www.benscoat.eu\/wp-content\/uploads\/2020\/11\/Stacked-Fantaisist-Radiocarbon-Date-Calibration-Curves-768x512.jpg 768w, https:\/\/www.benscoat.eu\/wp-content\/uploads\/2020\/11\/Stacked-Fantaisist-Radiocarbon-Date-Calibration-Curves-1536x1024.jpg 1536w, https:\/\/www.benscoat.eu\/wp-content\/uploads\/2020\/11\/Stacked-Fantaisist-Radiocarbon-Date-Calibration-Curves-272x182.jpg 272w, https:\/\/www.benscoat.eu\/wp-content\/uploads\/2020\/11\/Stacked-Fantaisist-Radiocarbon-Date-Calibration-Curves.jpg 1771w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption><em>Stacked Fantaisist Radiocarbon Date Calibration Curves<\/em>.<\/figcaption><\/figure>\n\n\n\n<p>Isn&#8217;t it beauty?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since introduced by the middle of 20th century, radiocarbon dating has been used in a large variety of disciplines. In palaeoecology, it has allowed for a major improvement in dating past phenomena. The ground principle is fairly simple: the isotope 14 of carbon (14C) is radioactive and decays following one criterion, namely the half-life. TheContinue reading &rarr;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,5],"tags":[],"_links":{"self":[{"href":"https:\/\/www.benscoat.eu\/index.php\/wp-json\/wp\/v2\/posts\/42"}],"collection":[{"href":"https:\/\/www.benscoat.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.benscoat.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.benscoat.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.benscoat.eu\/index.php\/wp-json\/wp\/v2\/comments?post=42"}],"version-history":[{"count":2,"href":"https:\/\/www.benscoat.eu\/index.php\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"predecessor-version":[{"id":233,"href":"https:\/\/www.benscoat.eu\/index.php\/wp-json\/wp\/v2\/posts\/42\/revisions\/233"}],"wp:attachment":[{"href":"https:\/\/www.benscoat.eu\/index.php\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.benscoat.eu\/index.php\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.benscoat.eu\/index.php\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}