Help on plotting probability density for calendar dates

gianmarco

TS Contributor
#1
Hi All,
I would like to have some hints on how to plot in R a chart similar to the one attached.
It is a figure from a Journal article of mine (JOURNAL OF QUATERNARY SCIENCE (2013) 28(6) 630–640; ISSN 0267-8179. DOI: 10.1002/jqs.2659). I made it with JMP.

The problem is that I would like to make something similar in R (ggplot2?), by the main issue for me is the fact that I am dealing with calendar dates, and I do not know how to tackle this issue in R.

The chart is actually plotting the probability density for the radiocarbon calendar dates for 4 different archaeological sites. The data on which is based are provided by a program used for Bayesian radiocarbon dating.

The program returns a file with two columns: one for the calendar date, one for the probability of each date. A third column could be used for a grouping variable, in case of more than 1 site.

In JMP it is quite straightforward to make the chart; but with R I am lost...
How is it possible to chart calendar dates on the x axis ?

Any help is appreciated.

Regards
Gm

p.s.
sample dataset
Code:
dates	prob	site
-1669,5	0,000006592	A
-1664,5	0,000008077	A
-1659,5	0,000010774	A
-1654,5	0,000013171	A
-1649,5	0,000013089	A
-1644,5	0,000014846	A
-1639,5	0,000020908	A
-1634,5	0,000025552	A
-1629,5	0,000030524	A
-1624,5	0,0000333	A
-1619,5	0,00003883	A
-1614,5	0,0000498	A
-1609,5	0,00006008	A
-1604,5	0,0000779	A
-1599,5	0,00009127	A
-1594,5	0,00011045	A
-1589,5	0,00013803	A
-1584,5	0,00016183	A
-1579,5	0,00020962	A
-1574,5	0,00024032	A
-1569,5	0,00029564	A
-1564,5	0,0003831	A
-1559,5	0,0005092	A
-1554,5	0,0006705	A
-1549,5	0,0008538	A
-1544,5	0,0011448	A
-1539,5	0,0014985	A
-1534,5	0,0020447	A
-1529,5	0,0027548	A
-1524,5	0,003599	A
-1519,5	0,004628	A
-1514,5	0,005829	A
-1509,5	0,007203	A
-1504,5	0,0088	A
-1499,5	0,010631	A
-1494,5	0,012035	A
-1489,5	0,012699	A
-1484,5	0,012967	A
-1479,5	0,01296	A
-1474,5	0,01316	A
-1469,5	0,013552	A
-1464,5	0,013621	A
-1459,5	0,013222	A
-1454,5	0,011972	A
-1449,5	0,010188	A
-1444,5	0,007949	A
-1439,5	0,005573	A
-1434,5	0,003478	A
-1429,5	0,0019968	A
-1424,5	0,0011756	A
-1419,5	0,0006661	A
-1414,5	0,0003989	A
-1409,5	0,00018489	A
-2824,5	2,07E-03	B
-2819,5	5,23E-04	B
-2814,5	1,03E-03	B
-2809,5	3,10E-03	B
-2804,5	6,26E-07	B
-2799,5	2,07E-03	B
-2794,5	3,10E-03	B
-2789,5	2,07E-03	B
-2784,5	8,33E-04	B
-2779,5	3,10E-03	B
-2774,5	2,07E-03	B
-2769,5	1,03E-03	B
-2764,5	4,19E-04	B
-2759,5	5,23E-04	B
-2754,5	1,03E-03	B
-2749,5	1,03E-03	B
-2744,5	1,03E-03	B
-2739,5	3,10E-03	B
-2734,5	3,10E-03	B
-2729,5	0	B
-2724,5	1,03E-03	B
-2719,5	0	B
-2714,5	4,19E-04	B
-2709,5	3,10E-03	B
-2704,5	2,07E-03	B
-2699,5	3,10E-03	B
-2694,5	3,10E-03	B
-2689,5	3,10E-03	B
-2684,5	2,07E-03	B
-2679,5	4,19E-04	B
-2674,5	2,07E-03	B
-2669,5	3,10E-03	B
-2664,5	4,19E-04	B
-2659,5	6,26E-07	B
-2654,5	0	B
-2649,5	4,19E-04	B
-2644,5	3,10E-03	B
-2639,5	4,19E-04	B
-2634,5	4,19E-04	B
-2629,5	1,03E-03	B
-2624,5	3,10E-03	B
-2619,5	5,23E-04	B
-2614,5	6,26E-07	B
-2609,5	2,07E-03	B
-2604,5	5,23E-04	B
-2599,5	2,07E-03	B
-2594,5	1,03E-03	B
-2589,5	8,33E-04	B
-2584,5	3,10E-03	B
-2579,5	5,23E-04	B
-2574,5	5,23E-04	B
-2569,5	1,03E-03	B
-2564,5	8,33E-04	B
-2559,5	7,30E-04	B
-2554,5	6,26E-07	B
-2549,5	3,10E-03	B
-2544,5	4,19E-04	B
-2539,5	5,23E-04	B
-2534,5	4,19E-04	B
-2529,5	5,23E-04	B
-2524,5	3,10E-03	B
-2519,5	5,23E-04	B
-2514,5	4,19E-04	B
-2509,5	9,36E-04	B
-2504,5	4,19E-04	B
-2499,5	4,19E-04	B
-2494,5	3,10E-03	B
-2489,5	8,33E-04	B
-2484,5	4,19E-04	B
-2479,5	8,33E-04	B
-2474,5	4,19E-04	B
-2469,5	1,03E-03	B
-2464,5	3,10E-03	B
-2459,5	4,19E-04	B
-2454,5	5,23E-04	B
-2449,5	2,07E-03	B
-2444,5	5,23E-04	B
-2439,5	5,23E-04	B
-2434,5	9,36E-04	B
-2429,5	4,19E-04	B
-2424,5	4,19E-04	B
-2419,5	6,26E-07	B
-2414,5	5,23E-04	B
-2409,5	6,26E-07	B
-2404,5	6,26E-07	B
-2399,5	6,26E-07	B
-2394,5	4,19E-04	B
-2389,5	1,0399E-006	B
-2384,5	1,0399E-006	B
-2379,5	4,19E-04	B
-2374,5	7,30E-04	B
-2369,5	7,30E-04	B
-2364,5	5,23E-04	B
-2359,5	8,33E-04	B
-2354,5	8,33E-04	B
-2349,5	1,3555E-006	B
-2344,5	6,26E-07	B
-2339,5	1,1485E-006	B
-2334,5	7,30E-04	B
-2329,5	0,000001252	B
-2324,5	8,33E-04	B
-2319,5	9,36E-04	B
-2314,5	1,1433E-006	B
-2309,5	1,0399E-006	B
-2304,5	0,000001252	B
-2299,5	0,000001252	B
-2294,5	2,3953E-006	B
-2289,5	1,1485E-006	B
-2284,5	1,3555E-006	B
-2279,5	1,4589E-006	B
-2274,5	2,1884E-006	B
-2269,5	1,0399E-006	B
-2264,5	4,19E-04	B
-2259,5	2,3953E-006	B
-2254,5	9,36E-04	B
-2249,5	1,9815E-006	B
-2244,5	1,7693E-006	B
-2239,5	2,4988E-006	B
-2234,5	2,0849E-006	B
-2229,5	1,9815E-006	B
-2224,5	2,1884E-006	B
-2219,5	2,1884E-006	B
-2214,5	1,3555E-006	B
-2209,5	2,2919E-006	B
-2204,5	1,0399E-006	B
-2199,5	1,7693E-006	B
-2194,5	0,000003228	B
-2189,5	1,7693E-006	B
-2184,5	2,3953E-006	B
-2179,5	1,7693E-006	B
-2174,5	2,7109E-006	B
-2169,5	2,0849E-006	B
-2164,5	2,1884E-006	B
-2159,5	2,9179E-006	B
-2154,5	3,1248E-006	B
-2149,5	2,7109E-006	B
-2144,5	2,8144E-006	B
-2139,5	2,7109E-006	B
-2134,5	0,00000344	B
-2129,5	0,000003958	B
-2124,5	3,0213E-006	B
-2119,5	0,000003647	B
-2114,5	0,000003544	B
-2109,5	0,000003958	B
-2104,5	0,000005003	B
-2099,5	0,000003751	B
-2094,5	0,00000344	B
-2089,5	0,00000344	B
-2084,5	0,000003751	B
-2079,5	0,000005106	B
-2074,5	0,00000552	B
-2069,5	0,000006669	B
-2064,5	0,000004687	B
-2059,5	0,000006146	B
-2054,5	0,00000552	B
-2049,5	0,000005624	B
-2044,5	0,000003958	B
-2039,5	0,000004899	B
-2034,5	0,000004687	B
-2029,5	0,00000448	B
-2024,5	0,000005417	B
-2019,5	0,00000521	B
-2014,5	0,000006565	B
-2009,5	0,000005629	B
-2004,5	0,000006565	B
-1999,5	0,000006043	B
-1994,5	0,000008438	B
-1989,5	0,00000865	B
-1984,5	0,000008231	B
-1979,5	0,000009897	B
-1974,5	0,000007812	B
-1969,5	0,000009586	B
-1964,5	0,000009064	B
-1959,5	0,000011563	B
-1954,5	0,000011045	B
-1949,5	0,000009374	B
-1944,5	0,000008857	B
-1939,5	0,000010942	B
-1934,5	0,000010626	B
-1929,5	0,000013441	B
-1924,5	0,000014377	B
-1919,5	0,000014067	B
-1914,5	0,000013022	B
-1909,5	0,000013337	B
-1904,5	0,00001594	B
-1899,5	0,000014688	B
-1894,5	0,000015733	B
-1889,5	0,000015526	B
-1884,5	0,000017818	B
-1879,5	0,000022194	B
-1874,5	0,000021149	B
-1869,5	0,000019276	B
-1864,5	0,000020001	B
-1859,5	0,000021568	B
-1854,5	0,000023027	B
-1849,5	0,000026152	B
-1844,5	0,000026462	B
-1839,5	0,00003407	B
-1834,5	0,000030524	B
-1829,5	0,000035	B
-1824,5	0,00003167	B
-1819,5	0,00003522	B
-1814,5	0,00003646	B
-1809,5	0,00003855	B
-1804,5	0,00003959	B
-1799,5	0,00004418	B
-1794,5	0,0000473	B
-1789,5	0,00005126	B
-1784,5	0,0000522	B
-1779,5	0,0000548	B
-1774,5	0,00005657	B
-1769,5	0,00006074	B
-1764,5	0,0000573	B
-1759,5	0,00006772	B
-1754,5	0,00006657	B
-1749,5	0,00007793	B
-1744,5	0,00007168	B
-1739,5	0,00007689	B
-1734,5	0,00008648	B
-1729,5	0,0000848	B
-1724,5	0,00009679	B
-1719,5	0,00010085	B
-1714,5	0,00011648	B
-1709,5	0,00011387	B
-1704,5	0,00012752	B
-1699,5	0,00013367	B
-1694,5	0,00014638	B
-1689,5	0,00014461	B
-1684,5	0,00015117	B
-1679,5	0,00016607	B
-1674,5	0,00017003	B
-1669,5	0,00018649	B
-1664,5	0,00017888	B
-1659,5	0,00020837	B
-1654,5	0,00022014	B
-1649,5	0,00023264	B
-1644,5	0,00024056	B
-1639,5	0,00026161	B
-1634,5	0,00029442	B
-1629,5	0,0003231	B
-1624,5	0,0003187	B
-1619,5	0,0003336	B
-1614,5	0,0003764	B
-1609,5	0,0003985	B
-1604,5	0,0004431	B
-1599,5	0,0004504	B
-1594,5	0,0004956	B
-1589,5	0,0005266	B
-1584,5	0,0005724	B
-1579,5	0,0006123	B
-1574,5	0,0006592	B
-1569,5	0,0007033	B
-1564,5	0,0007509	B
-1559,5	0,0008106	B
-1554,5	0,0008934	B
-1549,5	0,0009358	B
-1544,5	0,0009987	B
-1539,5	0,0011179	B
-1534,5	0,0012024	B
-1529,5	0,0013187	B
-1524,5	0,0014093	B
-1519,5	0,0015659	B
-1514,5	0,0016589	B
-1509,5	0,0017676	B
-1504,5	0,0019496	B
-1499,5	0,0021323	B
-1494,5	0,0022801	B
-1489,5	0,0023411	B
-1484,5	0,002507	B
-1479,5	0,0027229	B
-1474,5	0,0028387	B
-1469,5	0,0030357	B
-1464,5	0,003263	B
-1459,5	0,003509	B
-1454,5	0,003735	B
-1449,5	0,004069	B
-1444,5	0,004223	B
-1439,5	0,004461	B
-1434,5	0,004785	B
-1429,5	0,004897	B
-1424,5	0,004994	B
-1419,5	0,005062	B
-1414,5	0,005134	B
-1409,5	0,005106	B
-1404,5	0,005174	B
-1399,5	0,005048	B
-1394,5	0,004985	B
-1389,5	0,004902	B
-1384,5	0,004888	B
-1379,5	0,004844	B
-1374,5	0,004814	B
-1369,5	0,00474	B
-1364,5	0,004588	B
-1359,5	0,004551	B
-1354,5	0,004453	B
-1349,5	0,004202	B
-1344,5	0,004184	B
-1339,5	0,003915	B
-1334,5	0,003701	B
-1329,5	0,00346	B
-1324,5	0,003254	B
-1319,5	0,0029803	B
-1314,5	0,0027911	B
-1309,5	0,0025808	B
-1304,5	0,0025583	B
-1299,5	0,0023674	B
-1294,5	0,0020454	B
-1289,5	0,0017832	B
-1284,5	0,0016215	B
-1279,5	0,0013817	B
-1274,5	0,001173	B
-1269,5	0,0008482	B
-1264,5	0,0007464	B
-1259,5	0,0004251	B
-1254,5	0,00031495	B
-1249,5	0,00024087	B
-1244,5	0,00022628	B
-1239,5	0,00027098	B
-1234,5	0,0003194	B
-1229,5	0,00027858	B
-1224,5	0,00020951	B
-1219,5	0,00011721	B
-1214,5	0,00006886	B
-1209,5	0,000017192	B
-1204,5	0,000013022	B
 
Last edited:

vinux

Dark Knight
#2
Initially I got confued with the comma (decimal point). You could plot this easily in ggplot. Since dates are in years, you don't need a special treatment.
Code:
ggplot(data=yourdata)+geom_line(aes(x=dates,y=prob,color=site))
 

gianmarco

TS Contributor
#3
Hi Vinux,
thanks for putting me on the right track.
As you can see from the attached .pdf, I managed to make the plot.

Now, I would like to:
1) zoom-in, i.e. setting the limits for the x axis between -1700 and -1300
2) if possible, to fill the area under the curves

Any idea?

Thanks again.
Gm
 

vinux

Dark Knight
#4
1. Add
Code:
scale_x_continuous(limits =c(-1700,-1300))
to the ggplot
2. You could try
Code:
geom_area(aes(x=dates,y=prob,fill=site))
instead of geom_line. You need to be careful on the intersection of two curves.
 

gianmarco

TS Contributor
#5
Thanks Vinux,
(2) works fine, even though I have to search further on how one can set some transparency.

As for (1), I do not get where the piece of code must be put within the main command???
Code:
scale_x_continuous(limits =c(-1700,-1300))
Gm
 

vinux

Dark Knight
#6
This way
Code:
ggplot(data=yourdata)+ geom_line(aes(x=dates,y=prob,color=site)) + scale_x_continuous(limits =c(-1700,-1300))
 

gianmarco

TS Contributor
#7
Unfortunately, upon further inspection, the following code does not produce the right plot.
Compare the previously attached plot to the new one. The dataset is, quite obviously, the same.
In the second plot, I notice that the curves are all the same (i.e., the first repeated 3 times).
I do not get why?

Code:
ggplot(data=data)+geom_area(aes(x=dates,y=prob,fill=site))
 

gianmarco

TS Contributor
#8
Vinux,
thanks.
Just to summarize the achievements:
Code:
ggplot(data=yourdata)+ geom_line(aes(x=dates,y=prob,color=site)) + scale_x_continuous(limits =c(-1700,-1300))
This works fine.

What remains to do is to fill the area under the curve.
 

vinux

Dark Knight
#9
You can do the transparency by adding alpha parameters. Here is an example.
Code:
ggplot(data=yourdata)+geom_area(aes(x=dates,y=prob,fill=site), alpha=0.3) +
  scale_x_continuous(limits =c(-1700,-1300))
 

gianmarco

TS Contributor
#10
Vinux,
thanks.
But there is an issue: I do not get why the plot made with code (1) is correct (=JPG_1), while the plot done with code (2) is wrong (=JPG_2).
code 1
Code:
ggplot(data=data)+ geom_line(aes(x=dates,y=prob,color=site)) + scale_x_continuous(limits =c(-1700,-1200))
code 2
Code:
ggplot(data=data)+geom_area(aes(x=dates,y=prob,fill=site), alpha=0.5) + scale_x_continuous(limits =c(-1700,-1200))
(dataset attached)

Any idea?

Gm
 

vinux

Dark Knight
#11
Sorry. I guess the default option in geom_area is "stack". Good observation.

Change the position you can get the graph you want. I have added position="identity" in the geom_area.
Try this
Code:
  ggplot(data=yourdata)+  geom_area(position="identity", aes(x=dates,y=prob,fill=site), alpha=0.3) +  scale_x_continuous(limits =c(-1700,-1300))
or
Code:
 ggplot(data=yourdata)+geom_line(aes(x=dates,y=prob,color=site)) + geom_area(position="identity",aes(x=dates,y=prob,fill=site), alpha=0.3) +
  scale_x_continuous(limits =c(-1700,-1300))