Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rainbow legend in R

Tags:

plot

r

enter image description hereI have a the death rate for ages 0-100 plotted in one graph. It's very color full but for a reader there is no explanation who is age 0, age 40 or age 100 for example. I have seen rainbow legends is pictures online, but gained no answer on how to write one out myself. The line I use to plot is

plot(m, series = "total", datatype="rate", plot.type="time", xlab="Years", main="Total death rate")

What can I change to get the rainbow legend? And is there a call for writing out the grid lines in the graph area?

Snippet of the data is

structure(c(0.107446, 0.02901, 0.015211, 0.010825, 0.008799, 
0.006939, 0.110958, 0.028299, 0.014543, 0.010939, 0.00893, 0.007335, 
0.091162, 0.026333, 0.013556, 0.009593, 0.007193, 0.005984, 0.097073, 
0.026088, 0.012508, 0.008976, 0.007057, 0.005734, 0.089595, 0.022413, 
0.011188, 0.008234, 0.006767, 0.005522, 0.093491, 0.02269, 0.011209, 
0.009104, 0.00724, 0.006003, 0.08592, 0.019631, 0.009459, 0.006521, 
0.005931, 0.004634, 0.080883, 0.019495, 0.009131, 0.006662, 0.004753, 
0.004184, 0.09061, 0.021713, 0.008999, 0.006332, 0.004717, 0.004221, 
0.076158, 0.016583, 0.008261, 0.005645, 0.004305, 0.003866, 0.078229, 
0.019572, 0.008195, 0.005844, 0.004747, 0.003715, 0.07538, 0.01746, 
0.008253, 0.006093, 0.004857, 0.003771, 0.074146, 0.01849, 0.008344, 
0.005719, 0.004848, 0.004174, 0.072209, 0.015411, 0.007567, 0.005166, 
0.004282, 0.003863, 0.076099, 0.016082, 0.006845, 0.005483, 0.004198, 
0.003673, 0.077503, 0.017572, 0.007786, 0.005492, 0.004817, 0.003448, 
0.072559, 0.018363, 0.007551, 0.005644, 0.004607, 0.003867, 0.067097, 
0.014814, 0.007276, 0.005583, 0.004591, 0.003869, 0.066543, 0.019021, 
0.011272, 0.009122, 0.007382, 0.00635, 0.071861, 0.016772, 0.009663, 
0.007277, 0.006925, 0.005022, 0.072476, 0.014029, 0.007611, 0.005485, 
0.004887, 0.003871, 0.064407, 0.012097, 0.005361, 0.004211, 0.003923, 
0.002647, 0.062224, 0.011398, 0.005304, 0.003305, 0.003032, 0.002257, 
0.057591, 0.010665, 0.004426, 0.002968, 0.002496, 0.002317, 0.061385, 
0.012738, 0.005614, 0.003438, 0.003094, 0.002173, 0.057092, 0.009833, 
0.004379, 0.003359, 0.002478, 0.002311, 0.056902, 0.009439, 0.004451, 
0.002798, 0.002614, 0.002104, 0.060825, 0.011348, 0.0047, 0.003323, 
0.002893, 0.002541, 0.061088, 0.011606, 0.004853, 0.002955, 0.002427, 
0.002494, 0.059242, 0.008986, 0.003939, 0.002715, 0.002199, 0.002218, 
0.057243, 0.007084, 0.003793, 0.002429, 0.00217, 0.001911, 0.057835, 
0.008646, 0.003879, 0.002627, 0.002119, 0.001695, 0.05217, 0.007877, 
0.003303, 0.002461, 0.001783, 0.001602, 0.049842, 0.007063, 0.003305, 
0.002104, 0.001544, 0.001379, 0.048754, 0.006322, 0.003006, 0.00226, 
0.001961, 0.001548, 0.047834, 0.006098, 0.002681, 0.002307, 0.001882, 
0.001623, 0.045679, 0.006773, 0.00329, 0.002311, 0.001794, 0.00182, 
0.046924, 0.006226, 0.003433, 0.002487, 0.001901, 0.001666, 0.044641, 
0.005054, 0.003138, 0.001933, 0.001878, 0.001403, 0.041354, 0.004579, 
0.0025, 0.001735, 0.001648, 0.001228, 0.040073, 0.004021, 0.002109, 
0.001469, 0.001155, 0.001245, 0.038778, 0.004742, 0.002258, 0.001759, 
0.001354, 0.001152, 0.031958, 0.002932, 0.001877, 0.001526, 0.001338, 
0.001155, 0.030943, 0.003213, 0.00218, 0.001858, 0.001429, 0.001144, 
0.032905, 0.003633, 0.002685, 0.001904, 0.001783, 0.001572, 0.030931, 
0.003345, 0.002049, 0.00173, 0.00147, 0.001261, 0.026955, 0.002292, 
0.001541, 0.001232, 0.001046, 0.001022, 0.025469, 0.002239, 0.001559, 
0.001314, 0.000903, 0.000907, 0.023388, 0.001881, 0.001435, 0.001149, 
0.000908, 0.000592, 0.023167, 0.002012, 0.001074, 0.001072, 0.00101, 
0.000708, 0.020801, 0.00185, 0.00138, 0.001153, 0.000802, 0.000886, 
0.021411, 0.001977, 0.001321, 0.000922, 0.000752, 0.000671, 0.020338, 
0.001702, 0.001157, 0.000908, 0.000866, 0.000721, 0.019017, 0.001688, 
0.001052, 0.000873, 0.000653, 0.000666, 0.018535, 0.001483, 0.001013, 
0.000808, 0.000684, 0.00048, 0.017848, 0.001449, 0.001057, 0.000808, 
0.000726, 0.00064, 0.017602, 0.001388, 0.001117, 0.000751, 0.000649, 
0.000526, 0.017921, 0.001424, 0.000889, 0.000784, 0.000703, 5e-04, 
0.01597, 0.001387, 0.00081, 0.00084, 0.000623, 0.000619, 0.016763, 
0.001251, 0.000897, 0.000707, 0.000544, 0.000613, 0.016616, 0.001419, 
0.000669, 0.000624, 0.000754, 0.000573, 0.01613, 0.001077, 0.000762, 
0.000688, 0.00068, 0.000546, 0.01582, 0.00105, 0.00088, 0.000703, 
0.000439, 0.000556, 0.015948, 0.000929, 0.000735, 0.000528, 0.000491, 
0.000534, 0.014965, 8e-04, 0.000593, 0.000656, 0.000517, 0.000519, 
0.013486, 0.000925, 0.00065, 6e-04, 0.00039, 0.000564, 0.012713, 
0.00092, 0.000597, 0.000437, 0.000664, 0.000505, 0.012879, 0.000723, 
0.000483, 0.000443, 0.000464, 0.00035, 0.012784, 0.000719, 0.000518, 
0.000508, 0.000426, 0.000391, 0.011642, 0.000668, 0.000535, 0.000449, 
0.000579, 0.00039, 0.011299, 0.000668, 0.000467, 0.000547, 0.000446, 
0.000511, 0.011514, 0.000598, 0.000458, 0.000355, 0.000285, 0.000356, 
0.010822, 0.000602, 0.00046, 0.000486, 0.000364, 4e-04, 0.009783, 
0.000588, 0.000414, 0.000388, 0.000298, 0.000391, 0.009622, 0.000725, 
0.000436, 0.000234, 0.00036, 0.000507, 0.008408, 0.000701, 0.000397, 
0.000311, 0.000351, 0.000378, 0.008115, 0.000522, 0.000407, 0.00027, 
0.000301, 0.000332, 0.007953, 0.000501, 0.000333, 0.000379, 0.000349, 
0.000238, 0.007689, 0.000531, 0.000274, 0.000314, 0.000315, 0.000393, 
0.007626, 0.000673, 0.000387, 0.000264, 0.000369, 0.000324, 0.006958, 
0.000567, 0.000283, 0.000295, 0.000303, 0.000221, 0.006842, 0.000412, 
0.000221, 0.000231, 0.000183, 0.000195, 0.006798, 0.000427, 0.000278, 
0.000294, 0.000315, 0.000183, 0.007002, 0.000352, 0.000271, 0.000237, 
2e-04, 0.000189, 0.006483, 0.000421, 0.00016, 0.000229, 0.000113, 
0.000137, 0.006938, 0.000472, 0.00029, 0.000244, 0.000166, 0.000216, 
0.00605, 0.000569, 0.000256, 0.000236, 0.000233, 0.000166, 0.006215, 
0.000426, 0.000329, 0.000266, 0.000192, 0.000169, 0.006023, 0.00051, 
0.000276, 0.000205, 0.000137, 0.000191, 0.00588, 0.000422, 0.000287, 
0.000157, 0.000285, 0.000168, 0.006158, 0.000392, 0.000282, 0.000266, 
0.000156, 0.000141, 0.006149, 0.000389, 0.000225, 0.000181, 0.000217, 
6.8e-05, 0.005339, 0.000281, 0.000256, 0.000259, 0.000144, 0.000169, 
0.004757, 3e-04, 0.000241, 0.000247, 0.00018, 0.000117, 0.004349, 
0.000324, 0.000161, 0.000127, 0.00022, 0.000136, 0.003988, 0.000261, 
0.000191, 8.8e-05, 0.000166, 0.000138, 0.003805, 0.000352, 0.000131, 
0.000141, 0.000168, 0.00015, 0.003541, 0.000443, 0.000139, 0.000174, 
0.000125, 0.000112, 0.003521, 0.000312, 0.000191, 0.000139, 0.000113, 
0.000125, 0.003343, 0.000233, 0.000172, 8e-05, 8.3e-05, 7.8e-05, 
0.00345, 0.000223, 0.00011, 6.4e-05, 1e-04, 6.5e-05, 0.003661, 
0.000276, 0.000211, 0.000154, 0.000106, 1e-04, 0.003334, 0.00026, 
0.000275, 0.00011, 0.000109, 9.5e-05, 0.003154, 0.000316, 0.000205, 
0.000153, 0.00011, 0.000109, 0.003134, 0.000213, 0.000126, 7.5e-05, 
8.7e-05, 8.7e-05, 0.002427, 0.000415, 0.000141, 0.000198, 8.6e-05, 
0.00013, 0.002857, 0.000293, 0.000137, 0.00015, 0.000114, 0.000107, 
0.002503, 0.000314, 0.000145, 8.8e-05, 0.000109, 0.000113, 0.002501, 
0.000222, 0.00016, 6.7e-05, 9.7e-05, 8.9e-05, 0.002506, 0.000327, 
0.000219, 0.000196, 4.8e-05, 7.7e-05, 0.002578, 0.00025, 0.000126, 
0.000109, 0.000111, 3.8e-05, 0.002061, 0.00026, 4.4e-05, 0.000117, 
0.000108, 5.5e-05, 0.002597, 0.000156, 0.000129, 0.000105, 0.000125, 
6.3e-05, 0.002689, 0.000123, 9.5e-05, 0.000136, 9.6e-05, 7.1e-05, 
0.002183, 0.000217, 4.3e-05, 0.000119, 5.9e-05, 9.5e-05), .Dim = c(6L, 
115L), .Dimnames = list(c("0", "1", "2", "3", "4", "5"), c("1900", 
"1901", "1902", "1903", "1904", "1905", "1906", "1907", "1908", 
"1909", "1910", "1911", "1912", "1913", "1914", "1915", "1916", 
"1917", "1918", "1919", "1920", "1921", "1922", "1923", "1924", 
"1925", "1926", "1927", "1928", "1929", "1930", "1931", "1932", 
"1933", "1934", "1935", "1936", "1937", "1938", "1939", "1940", 
"1941", "1942", "1943", "1944", "1945", "1946", "1947", "1948", 
"1949", "1950", "1951", "1952", "1953", "1954", "1955", "1956", 
"1957", "1958", "1959", "1960", "1961", "1962", "1963", "1964", 
"1965", "1966", "1967", "1968", "1969", "1970", "1971", "1972", 
"1973", "1974", "1975", "1976", "1977", "1978", "1979", "1980", 
"1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", 
"1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", 
"1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", 
"2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", 
"2013", "2014")))
like image 776
Orongo Avatar asked Apr 02 '17 13:04

Orongo


1 Answers

I am not sure how you went from the data to the plot. I suspect that the color was actually based on age which was not provided, but since that was not the point, I just made up some value. Also, because you used the rainbow color palette I did too, but as noted by @KonradRudolph there are problems with rainbow and you might want to consider a different palette.

To address your questions, the SDMTools package includes a legend.gradient function. The function grid will add the background grid that you want. I use all of that in the example below. It will have to be adjusted to match the way you actually plotted this, but this should be close enough for you to figure it out. I called the data structure that you provided for data DR.

library(SDMTools)
age = floor((log(DR[,1])+6)*17)
plot(colnames(DR), log(DR[1,]), type="l", ylim=c(-10,0),
    col=rainbow(102)[age[1]], xlab="Years", ylab="log Death Rate")
for(i in 2:6) { 
    lines(colnames(DR), log(DR[i,]), col=rainbow(102)[age[i]]) }

LegLoc   = cbind(x =c(1900,1903,1903,1900), y =c(-8,-8,-10,-10))
legend.gradient(pnts=LegLoc, cols=rainbow(102), 
    limits=c(1, 100), title = "age")
grid()

gradient.legend Example

like image 157
G5W Avatar answered Nov 14 '22 08:11

G5W