How to make spiked rather than rectangular columns with ggplot - r

I'm using ggplot to display some data. The machine that generates it produces a plot that looks like this. You'll notice each individual blue peak is a spike, with a sharp top.
I've recreated this with ggplot, but is there a way to get geom_col, or another function, to produce a spike for each peak?
data <- data.frame(rpt = c(100.803333333333, 101.783333333333, 102.733333333333, 103.653333333333,
104.64, 105.566666666667, 106.526666666667, 107.46, 108.36, 109.33,
110.303333333333, 111.276666666667, 112.223333333333, 113.136666666667,
114.186666666667, 115.21, 116.2, 117.153333333333, 118.18, 119.17,
120.16, 121.153333333333, 122.163333333333, 123.086666666667,
124.046666666667, 125.003333333333, 125.956666666667, 126.946666666667,
127.866666666667, 128.82, 129.736666666667, 130.7, 131.633333333333,
132.6, 133.573333333333, 134.51, 135.453333333333, 136.43, 137.376666666667,
138.356666666667, 139.306666666667, 140.256666666667, 141.213333333333,
142.166666666667, 143.16, 144.086666666667, 145.046666666667,
146.01, 147.013333333333, 147.946666666667, 148.913333333333,
149.853333333333, 150.86, 151.803333333333, 152.746666666667,
153.693333333333, 154.673333333333, 155.66, 156.576666666667,
157.53, 158.486666666667, 159.443333333333, 160.403333333333,
161.403333333333, 162.33, 164.223333333333, 165.226666666667,
166.123333333333, 167.023333333333, 168.103333333333, 168.936666666667,
170.026666666667, 170.9, 171.85, 172.62, 173.61, 174.716666666667,
175.713333333333, 176.64, 177.57, 178.466666666667, 180.913333333333,
181.256666666667, 182.286666666667, 183.32, 184.283333333333,
185.246666666667, 186.136666666667, 187.146666666667, 188.16,
188.823333333333, 190.036666666667, 190.983333333333, 191.613333333333,
191.93, 192.323333333333, 192.84, 193.91, 194.866666666667, 195.706666666667,
199.523333333333, 200.563333333333, 201.326666666667, 202.293333333333,
203.346666666667, 204.893333333333, 206.943333333333, 208.223333333333,
208.846666666667, 209.68, 210.723333333333, 211.476666666667,
212.023333333333, 212.363333333333, 214.71, 216.576666666667,
216.97, 219.883333333333, 221.806666666667, 222.62, 223.526666666667,
224.436666666667, 225.35, 226.273333333333, 227.056666666667,
228.22, 229.013333333333, 232.076666666667, 238.52, 240.806666666667,
245.48, 248.49, 251.136666666667, 256.523333333333, 258.646666666667,
260.856666666667, 265.93, 268.88, 270.963333333333, 283.38, 285.2,
286.223333333333, 288.49, 294.926666666667),
height = c(119, 127, 132, 139, 136, 136, 140, 161, 162, 194, 239, 278,
370, 288, 434, 361, 286, 232, 213, 221, 238, 244, 266, 295, 306,
325, 358, 420, 497, 670, 838, 1104, 1451, 1743, 2018, 2170, 2226,
2058, 1777, 1464, 1158, 916, 702, 604, 540, 535, 554, 543, 517,
490, 434, 365, 322, 315, 312, 293, 272, 281, 279, 293, 297, 286,
253, 222, 170, 111, 69, 49, 29, 39, 33, 25, 24, 23, 16, 19, 18,
17, 24, 16, 19, 12, 20, 16, 19, 17, 19, 13, 16, 16, 17, 17, 16,
11, 19, 11, 18, 19, 17, 16, 14, 18, 14, 14, 11, 15, 10, 10, 11,
15, 11, 10, 10, 10, 11, 10, 10, 12, 10, 15, 14, 24, 27, 30, 22,
11, 12, 10, 10, 10, 10, 11, 10, 11, 10, 10, 12, 12, 15, 11, 10,
11, 10, 12))
ggplot(data, aes(x=rpt, y=height)) +
geom_col(position="identity")

Here's an approach where I add a zero value halfway in between each original value.
library(tidyverse)
data %>%
uncount(2, .id = "id") %>% # repeat each row
mutate(rpt = if_else(id == 2, (rpt + lead(rpt))/2, rpt), # space in between
height = if_else(id == 2, 0, height)) %>% # zero out every other row
ggplot(aes(x=rpt, y=height)) +
geom_line()

Related

How to plot spatial data when grid cell size varies

I am trying to make a map of global model predictions with the outline of the world on it.
The data I have cover most of the worlds oceans and the grid cells have a resolution of 4 degrees. This is my desired output: Map of global predictions
This is the code I have tried using:
# Packages
library(rnaturalearth)
library(ggplot2)
library(sp)
library(ggspatial)
# Subset of data (500 rows, 4 columns; Longitude, Latitude, Prediction, Grid cell area)
data <- structure(list(Longitude = c(-10.16956752, 126.1689468, -61.99876686,
-94.00000289, 126.0007703, 170.000033, 82.55726146, -6.495772811,
-149.9999879, -61.99897757, -89.99998854, 2.000011459, 122.1211726,
-2.593090202, -17.99998854, -170.0001433, 121.8262874, 10.21256204,
166.0000115, 158.3111356, 134.3679695, -13.73648415, 170.0000423,
92.91243971, 5.996191823, 62.00001146, -53.99998854, -21.99998854,
125.9910976, 154.0000115, 149.9995576, -77.88731531, -45.99998854,
-33.99998854, -5.878402842, -157.9999885, -169.9929349, 66.00001146,
-138.5853402, -17.99998854, -69.99998854, -56.79830902, 110.0502773,
50.00001146, 39.56890984, -146.0004371, -5.999521688, 18.72192101,
-69.77517581, 21.3289268, 42.00001146, 162.0000115, 165.9995769,
-162.0365539, 170.0000138, 142.0000446, -45.99998854, -165.9999885,
110.0000115, -25.99998854, 6.000011459, -89.99998854, -53.99998854,
50.00187439, 146.0000115, -134.3123896, 165.9543502, -169.9999885,
-81.89972865, 72.42284406, 134.0000115, -170.0197871, -96.6556298,
17.92812, -152.9139565, -9.999988541, -73.99998854, 158.0000115,
54.20603343, 157.9995854, 74.00034763, -157.9999902, 102.4769658,
-73.99191424, 6.000011459, 162.0000177, -10.07971318, -93.99998854,
90.00001146, 153.9999475, 162.0000115, 117.9915582, -33.99998854,
38.00001146, -77.99998854, -81.99998854, 74.00022314, 114.1420482,
-173.9974719, 118.4796001, 178.0206217, 42.00001146, 178, -138.000174,
146.6226982, 165.999998, -49.73916727, -37.99998854, 137.9999465,
38.00001146, -82.04904949, 119.0280587, -169.9999946, 162.855343,
113.9999563, -61.9672714, 50.12191729, -173.9999885, 41.85382837,
130.0000115, 126.5154259, -37.93913378, 62.00001146, 129.9747567,
-161.9999885, -137.9999787, -1.958606869, 165.9999802, 150.1568838,
13.72012633, -37.59668977, 162.0000061, -29.99998854, -177.9999885,
117.9999058, -149.9999885, 114.0000063, 96.91839389, -29.99998854,
-25.99998854, -93.99998854, -29.99998854, 170.0000115, -173.9999885,
142.0105738, -37.9593374, 9.937003613, 145.9958965, -178.0133745,
-78.74777464, -122.0023764, -178.0000115, 162.0000115, -169.9999885,
-141.9999885, 158.0000115, -85.99998854, -15.44057437, 30.01655738,
-161.9999885, 35.50200234, 173.9999943, 5.999766664, 58.00001146,
-14.27262379, 53.59629681, 146.0999455, 53.97807051, 178.3714871,
174.0000115, -173.9999885, 46.05658123, 154.0000115, -150.0013408,
130.3202993, 5.888053059, 70.00001146, -137.9971843, -57.99998854,
-153.9999885, 38.51576452, 5.356846647, -177.9999885, 38.21244542,
22.06404301, -85.99990114, 165.9999621, 42.06833294, 82.38551366,
-69.26776454, -149.9999885, 105.7885139, -162.0000326, 17.01965058,
-74.66124649, -61.84333457, 154.0000125, 170.0162654, 1.824314695,
13.82919973, -166.2490329, 2.000011459, 58.00381545, 162.269909,
70.00001146, 126.2275842, -85.99069255, -161.9999885, -145.9998563,
-110.8963681, -45.6718642, 106.1072479, 150.0124763, 173.9999974,
129.7397288, -53.99998854, -174.0231047, 142.4679548, 6.084793515,
165.951299, 174.0000115, 58.00001146, 54.00001146, -78.41571925,
22.04138072, -87.07057574, -105.9999885, -109.9999885, -37.99998854,
9.440334846, -74.33053694, -65.97004746, -49.99998854, -73.95814747,
-74.74355347, 117.7688064, 78.00001146, 170.0004612, -126.0474866,
-10.14156626, 41.44639846, -65.99998854, 150.0007478, 138.1284291,
166.0000115, -177.9999885, 165.9998751, 122.5803857, -1.832865108,
-78.22084003, 145.9777733, 154.0040729, -57.99998854, -73.99998854,
-17.99998854, 97.66893601, -73.99998854, 58.00448, 158.0000123,
-161.9999831, 10.00001146, -70.11943583, 90.00001146, -93.90742733,
122.0381743, 90.02820747, 114.0000115, 178.0002266, 94.03546334,
-13.99998854, 2.026556417, 170.0816676, 170.0000105, 174.0000115,
-177.9999885, -157.9999885, 134.0000243, -74.01378632, 14.07846074,
4.926681381, 166.0000115, -178.0000102, -18.22132114, -41.76260639,
-165.9999885, -173.9999848, 117.8546849, 13.97253564, 138.0000115,
2.000011459, -57.86039422, -162.000012, -169.9967526, -60.85299577,
-37.53072648, -81.99781314, 138.0875793, 169.8994304, 107.4007518,
-73.99998854, 53.99801704, 46.00001146, 25.98144338, 66.00001146,
114.0000316, 110.0000115, 112.9024546, 177.9564699, -1.999988541,
-82.00000426, 162.0082822, -69.24184951, 101.9020192, -173.9995473,
-49.27150176, 21.92369036, 138.4587056, -66.29757086, -26.0078251,
174.0000131, 133.9889887, 38.16885146, -5.999988541, 133.9391315,
-61.99998854, 8.706431568, -49.8624759, 130.0106889, -13.99998854,
-177.9999885, -1.969664455, 113.1977475, 94.00376755, 82.00293912,
-61.49274336, 78.05701813, 109.8622452, 151.2237393, 154.0000115,
166.0000115, 13.64867252, -90.74983985, -54.5837941, 150.0593517,
-30.00737278, 10.14403385, 165.9999579, 141.99997, -71.49830857,
-57.99636593, 126.0072243, -21.94802182, -109.8907217, -9.999988541,
-61.96780638, -137.9996316, 106.0499776, 158.0000115, -65.08399512,
178, 65.83642764, 125.9426387, -157.9992733, 50.00001146, 38.06053256,
69.99999724, 129.6863334, -166.3675674, -11.16300072, 53.62409287,
137.7173457, 126.0031256, 118.9525785, 154.0010114, -162.0000077,
34.00001146, -5.999988541, -94.32614301, 25.43306958, 142.2242709,
37.21917664, 138.0000115, -49.99998854, -109.999023, 173.6014161,
40.58655549, 14.30990335, -65.58554854, -153.9096668, 121.8307417,
174.0000115, -74.05492837, -17.99998854, -96.82143996, 10.00001146,
165.989779, 54.00001146, 9.648264537, -153.9996769, -1.379131047,
-33.99967232, -13.99998854, -13.99998863, -29.99998854, -157.9993798,
130.3986089, -149.9540973, -66.05141375, -117.9995487, -69.99998854,
-10.08258389, 113.9349012, 50.88704952, -48.62667494, -49.99998854,
-114.0201716, -133.999895, -102.1160738, -174.0044266, 145.728285,
-21.99998854, 138.0034532, -93.99998854, 125.1209634, 133.9987429,
-141.9999885, 133.94514, -75.16942252, 13.30257806, -57.91461701,
58.00001146, 169.9995982, 38.92821472, 70.00001146, 12.76082336,
-77.99998854, -78.00228912, -81.799125, 130.0491847, 46.00091358,
154.117492, -82.0003813, 102.1876905, 50.11824049, 6.000011459,
-41.75455025, -57.99998854, -18.15791357, -53.94267298, -33.99998854,
146.0000115, 142.0000115, 58.78666498, -45.99998854, 174.0000115,
-153.999987, 102.0000115, 125.9624191, 157.9999468, 170.027281,
154.0000115, -57.99998854, -177.994726, 46.00001146, 153.9999931,
-9.999988541, -165.9999925, -89.9340042, -86.0160541, -25.8730291,
58.00001146, 118.0000115, -122.5807138, 46.00001146, 110.0000115,
121.829682, 46.59514055, -157.9999885, -153.9999885, 62.00001146,
-169.9997623, 126.103807, -10.52623514, 173.8637862, 130.0192184,
-18.02951352, -10.00000412, 121.9939471, 154.2592125, -69.35640587,
142.6960182, -153.9999943, -57.89841693, 70.00001146, 101.5055217,
170.0000115, 14.00001146, -86.04945349, 113.4322028, -158.0211136
), Latitude = c(3.820825091, 12.08126747, -55.97816596, 0.022599112,
16.02343874, 0.022652324, 8.210545822, 35.69956109, -7.977383247,
20.02440074, -3.977389136, 68.02261086, 0.21132112, 48.20584876,
56.02261086, -19.97848897, -15.77200491, 39.96397297, -55.97738914,
51.31485551, 42.7205338, 63.73313502, -11.97738132, 19.14158715,
0.020484685, -11.97738914, 56.02261086, 12.02261086, 28.03092594,
-15.97738914, 8.022784025, 31.90009355, 4.022610864, -19.97738914,
3.470593567, 52.02261086, -11.96980917, -19.97738914, 58.63570686,
60.02261086, 36.02261086, -36.03332986, 19.86853597, -7.977389136,
-5.079794602, -15.97762273, 72.02282038, 60.02451001, 20.43051724,
56.88863607, -27.97738914, -51.97738914, -51.98251101, 56.0994785,
12.02263258, 32.02264073, 48.02261086, -39.97738914, -23.97738914,
12.02261086, 72.02261086, 8.022610864, -39.97738914, -23.97920822,
52.02261086, 55.56880819, -15.99570073, 0.022610864, 16.10672797,
19.81627886, 40.02261086, 52.00303707, 27.1872715, -36.06367709,
59.08122925, 64.02261086, -31.97738914, 48.02261086, 15.52636519,
-11.9775079, -7.977528124, -7.97730525, 0.334377082, 24.03404499,
68.02261086, 8.022600292, 56.12108887, 24.02261086, 8.022610864,
8.022581727, -23.97738914, -7.848129941, -31.97738914, 72.02261086,
-51.97738914, -11.97738914, 4.022581755, -8.010086532, -19.9761759,
23.37409625, -35.91559755, -31.97738914, 60.02261086, -19.97753078,
-15.74140829, 20.02262634, 4.188582753, -27.97738914, 8.02189791,
76.02261086, -8.031710615, 38.78875047, 16.02260157, 55.55599452,
12.02268181, 48.082086, 28.08551543, 56.02261086, -24.05829182,
8.022610864, 8.196719853, -20.01786497, -15.97738914, -7.986975327,
-19.97738914, -15.97735921, 60.07070103, 8.022585242, -7.833875605,
39.59151568, -3.200384655, 12.02261958, -7.977389136, -31.97738914,
-11.97749469, 56.02261086, 8.022591881, 15.30078904, 4.022610864,
60.02261086, 4.022610864, 64.02261086, -55.97738914, 0.022610864,
56.02527798, 63.87882091, -15.94729501, -11.98168222, 63.80828291,
4.17958317, 32.02014727, -23.97741025, -27.97738914, 60.02261086,
-11.97738914, -15.97738914, 0.022610864, 25.12307726, 35.30667652,
52.02261086, -20.53611794, -3.977401906, 56.02228724, 0.022610864,
28.3079482, 25.16859735, 44.05525808, -19.96113513, -40.28569378,
-15.97738914, -23.97738914, -0.037988625, 16.02261086, -15.96585368,
39.81030641, 64.19614498, 12.02261086, -7.973236555, 68.02261086,
16.02261086, 20.02052366, 53.42403026, -47.97738914, 68.94567437,
-36.10729177, 4.022489321, 12.02268765, -4.044358874, 15.51366591,
-52.17439793, -19.97738914, -8.252118386, 4.022523768, -32.17125459,
-52.30061977, 68.25610808, 24.02260363, 59.26701564, 37.27072488,
-28.09811868, 68.08853997, 64.02261086, 16.01865467, 60.12532688,
0.022610864, -33.20386499, 16.93217855, -23.97738914, -23.97739795,
27.03071622, -24.85901806, 0.086841557, 43.99971799, 0.022528258,
31.83167869, 8.022610864, 63.3986452, -2.559698337, 42.73623655,
-48.0292721, -31.97738914, -3.977389136, -27.97738914, 8.131263575,
33.25599506, 11.25544918, 16.02261086, 16.02261086, -31.97738914,
43.07891697, -35.77986134, 12.2126783, -35.97738914, 36.00374653,
-39.94361536, -4.13230838, 4.022610864, 56.02295289, 40.00081489,
44.13941424, 16.06104742, 24.02261086, 0.026048965, 43.88233783,
-31.97738914, -35.97738914, 0.02269025, 15.93016831, 36.33475991,
64.08957191, 58.69065802, -11.9821079, -43.97738914, -19.97738914,
8.022610864, 7.886221494, 28.02261086, -19.97425468, 4.022609044,
8.022632834, 76.02261086, -56.35045724, 12.02261086, 27.76247933,
20.11429594, 19.94635242, -11.97738914, 0.022800625, 12.01506492,
52.02261086, 3.990144293, -48.119873, -23.97739031, -47.97738914,
-7.977389136, -15.97738914, 4.02260161, -55.9950546, 55.17829816,
59.78172479, -39.97738914, -27.97709912, 19.91987466, 59.82242042,
-7.977389136, 28.0226185, -19.22686648, 35.75467029, 28.02261086,
60.02261086, 56.24693609, 0.022615635, -15.98239129, 56.91608985,
-16.00820732, 19.99353474, -11.93590796, -43.44797102, 17.39564775,
-27.97738914, -3.97660846, -27.97738914, 72.3771239, 20.02261086,
16.02260062, 8.022610864, -23.92922118, -15.87608439, 0.022610864,
4.02261266, -12.00190948, 43.02433176, 3.940700587, 52.02051602,
0.453746804, 35.80665248, 55.46846217, 60.44655603, 64.01596898,
-23.97739048, -8.021036568, 41.55396512, 0.022610864, -36.03540447,
-47.97738914, 0.18180871, -32.12616329, 28.02527243, 4.022610864,
60.02261086, 3.585780138, -28.19444665, 8.026970975, 4.01869224,
-40.6633516, 7.641169912, -4.189248799, -22.79978441, -35.97738914,
-35.97738914, 68.39801011, 20.72995388, 71.34060756, -39.94398235,
40.03156039, 56.27043197, -7.977302797, 0.022696615, -27.65147886,
12.01992286, -11.98018915, 68.03301665, 23.77432249, 60.02261086,
-44.00407701, -11.9781651, 7.869716893, 24.02261086, -44.63876721,
-43.97738914, 23.57520672, -0.00367899, -19.9766619, -27.97738914,
-20.03795661, -3.977378548, -11.69278291, 63.64955575, 29.33031037,
26.56177022, -36.35002015, 24.02124134, 3.824165017, 48.01687813,
-11.97742276, -31.97738914, 68.02261086, 14.9567938, 39.49419331,
52.14461318, 23.59920469, -39.97738914, 44.02261086, 20.02378979,
-39.82478245, 18.89622815, 33.19261424, 64.03632422, 20.04775234,
7.962805467, -11.97738914, 20.14118871, 36.02261086, 23.88593283,
-7.977389136, -24.0052275, 0.022610864, -4.304016102, -3.977239696,
56.33643088, 64.02229033, 36.02261086, 56.0226104, 36.02261086,
4.02276095, 35.97473352, 59.19366534, 43.43616188, 28.02104594,
28.02261086, 35.94314023, -19.90137354, 7.666766431, -28.51031591,
52.02261086, 24.00874176, -23.97747135, 15.87276871, 60.02116361,
-8.698681751, 60.02261086, 32.01724937, 12.02261086, 39.03817517,
8.023956758, -27.97738914, 36.52433482, -47.86022133, -24.05345441,
51.29963345, 12.02261086, 8.0230886, -17.44438098, 4.022610864,
-11.64166378, -47.97738914, 28.04187363, 12.02387396, -32.82633622,
-11.97100486, -23.86540067, -31.97684268, 8.220758027, 12.50957099,
-7.977389136, -2.362973126, 64.02261086, 11.93486197, 52.02216857,
4.022610864, 56.02261086, -43.97738914, 19.46370663, -27.97738914,
-7.977389136, -19.97738511, -7.977389136, -4.012441622, -3.97733509,
-15.96734551, -19.97738662, 40.02261086, -15.9747231, -31.97738914,
4.022573507, 68.02261086, -11.97740656, 27.83155686, 27.99326446,
67.48803929, -23.97738914, 20.02261086, 35.66604315, -3.977389136,
-27.97738914, 11.9231427, -25.17266991, -11.97738914, -23.97738914,
-23.97738914, -3.977488904, -7.815115494, 32.26364968, -35.8672365,
4.044041471, 68.08721908, -39.97720998, -12.03217659, -32.12480347,
60.72642244, 58.57913449, -15.97736476, -40.14518463, 16.02261086,
-4.507175688, 52.02261086, -31.97738914, 7.959477669, 4.883317505,
19.98977949), y = c(224, 277, 48, 33, 224, 14, 372, 205, 10,
110, 24, 10, 234, 105, 14, 43, 358, 186, 5, 5, 48, 38, 33, 353,
153, 24, 10, 10, 305, 76, 38, 167, 167, 62, 238, 10, 52, 10,
14, 14, 10, 210, 372, 81, 205, 57, 10, 5, 110, 5, 57, 5, 19,
14, 33, 48, 10, 5, 43, 10, 5, 5, 176, 157, 10, 33, 62, 14, 153,
343, 29, 14, 181, 167, 14, 38, 76, 5, 262, 91, 24, 33, 381, 119,
24, 33, 91, 134, 52, 38, 10, 348, 5, 10, 52, 10, 124, 401, 48,
501, 62, 14, 5, 52, 296, 10, 181, 5, 29, 5, 176, 134, 24, 5,
129, 24, 253, 5, 167, 5, 277, 162, 19, 291, 29, 52, 76, 33, 195,
176, 176, 29, 5, 33, 205, 19, 396, 329, 5, 10, 14, 5, 5, 14,
10, 10, 195, 319, 5, 210, 134, 43, 5, 5, 24, 5, 10, 176, 167,
14, 238, 24, 76, 5, 200, 248, 43, 72, 62, 33, 38, 172, 10, 57,
86, 48, 172, 38, 5, 19, 195, 62, 14, 5, 186, 38, 33, 191, 367,
76, 19, 401, 14, 143, 62, 5, 5, 5, 181, 114, 5, 43, 234, 5, 5,
181, 143, 10, 38, 262, 253, 396, 10, 33, 310, 186, 5, 176, 176,
48, 52, 105, 5, 348, 167, 215, 33, 10, 5, 176, 76, 186, 277,
162, 72, 343, 114, 5, 62, 138, 205, 5, 129, 57, 14, 5, 29, 253,
186, 5, 5, 153, 134, 10, 238, 429, 24, 72, 38, 29, 5, 52, 52,
186, 462, 396, 5, 33, 257, 43, 234, 48, 62, 24, 14, 14, 38, 48,
38, 76, 14, 33, 253, 10, 10, 24, 386, 186, 5, 72, 10, 10, 52,
10, 153, 105, 343, 52, 343, 10, 105, 157, 14, 114, 124, 367,
377, 57, 5, 33, 100, 67, 381, 14, 181, 172, 5, 10, 29, 14, 305,
10, 5, 176, 95, 238, 277, 267, 10, 5, 238, 324, 200, 324, 162,
420, 377, 296, 134, 10, 33, 181, 5, 191, 33, 72, 38, 124, 76,
124, 353, 19, 267, 81, 153, 52, 415, 10, 138, 33, 362, 224, 38,
14, 238, 24, 358, 5, 181, 248, 191, 215, 401, 5, 33, 62, 5, 210,
157, 19, 195, 119, 19, 105, 62, 191, 186, 5, 57, 300, 33, 129,
5, 186, 215, 91, 10, 234, 29, 81, 10, 14, 38, 19, 29, 267, 19,
52, 114, 5, 176, 358, 195, 272, 10, 224, 48, 195, 5, 305, 5,
229, 124, 157, 43, 19, 257, 57, 95, 19, 5, 38, 224, 72, 200,
29, 181, 157, 181, 167, 300, 5, 420, 267, 5, 176, 5, 253, 19,
5, 5, 43, 253, 253, 24, 14, 86, 224, 119, 52, 277, 5, 62, 10,
29, 10, 33, 191, 195, 19, 29, 339, 134, 5, 33, 324, 172, 10,
19, 5, 33, 286, 186, 57, 181, 19, 5, 329, 267, 5, 5, 48, 181,
315, 257, 10, 81, 219, 424, 57), AreaKM2 = c(172379.8637, 167621.7823,
111118.7471, 196904.8, 189369.6807, 196896.989, 134153.5568,
71970.27812, 195050.3834, 185116.288, 196444.6159, 74544.74404,
135009.3945, 61775.80703, 111062.6969, 185126.4021, 164516.111,
124340.3488, 111191.639, 68700.83531, 43856.91162, 70108.90819,
192725.4358, 52368.79493, 195854.878, 192730.4345, 111062.6969,
192698.9311, 173434.9048, 189491.6891, 194962.3356, 155420.9957,
196434.0627, 185346.9753, 140635.0673, 122178.9993, 191978.3737,
185346.9753, 23658.60183, 99380.08521, 159993.6391, 42922.78337,
120817.5266, 195050.9669, 19360.96145, 189306.2305, 61499.9413,
56362.06723, 133811.001, 36208.14872, 174407.0449, 122301.2318,
121784.3494, 97667.61516, 192695.2501, 167570.6517, 132676.5593,
151724.8938, 180312.2014, 192698.9312, 61515.16271, 195029.8994,
151724.8937, 180138.6045, 122178.9993, 70469.59767, 182207.8668,
196906.2159, 178430.7004, 35127.21608, 151626.0232, 120088.3123,
34967.02013, 152173.9234, 21689.94623, 87187.46134, 167655.0629,
132676.5593, 137692.4358, 192687.7134, 195008.928, 195034.9023,
37709.23621, 177760.0238, 74544.74403, 195028.7223, 104452.2905,
180250.2648, 195029.8994, 194985.5096, 180312.2015, 168803.3062,
167655.0629, 61515.16273, 122301.2318, 192730.4345, 196367.3912,
154833.4069, 184773.8213, 103635.3051, 154987.4071, 167655.0629,
99379.51582, 185295.6034, 124572.0368, 185291.0797, 166948.1787,
174407.0449, 194937.1264, 48164.88123, 189140.5621, 28961.24096,
189447.4124, 50911.15111, 192688.7448, 114247.0282, 105516.4578,
111062.6969, 166115.7123, 195029.8994, 117024.6708, 179379.4406,
189491.6891, 189395.9156, 185346.9753, 189487.5792, 94441.42754,
195024.8608, 166013.9648, 105637.9472, 106848.1233, 192695.752,
195050.9669, 167655.0629, 192720.1173, 111062.6969, 195027.5475,
56141.14169, 196434.0627, 99380.08521, 196434.0627, 87187.46136,
111191.639, 196906.2158, 109043.8054, 81089.10227, 183386.8841,
192302.1075, 77938.94147, 118711.3761, 167366.3676, 180309.8601,
174407.0449, 99380.08521, 192730.4345, 189491.6891, 196906.2159,
24874.86628, 102407.9819, 122178.9993, 30881.52396, 196442.9135,
111042.0063, 196906.2159, 135534.2336, 66050.24256, 126682.0198,
182764.5526, 114190.7689, 189491.6891, 180312.2014, 190137.6583,
189449.8681, 187971.0012, 121449.9258, 78154.6362, 192698.9312,
194568.6369, 74544.74404, 189449.8681, 108316.5476, 24898.16725,
132791.5122, 28777.04168, 148918.0853, 196418.3023, 192682.0629,
188515.4215, 126131.307, 33183.60174, 185346.9753, 159613.3784,
196421.2717, 82004.78959, 65309.18163, 63338.00515, 180245.5915,
62627.78768, 57720.84009, 157413.4092, 59702.44117, 87187.46136,
189061.1605, 42292.06498, 196906.2159, 61262.26161, 102237.7457,
180312.2015, 180298.0587, 42771.2155, 92334.99481, 182042.8242,
140490.4741, 196664.533, 126823.1072, 195029.8994, 60074.40944,
49109.46533, 50418.56398, 128501.5426, 167655.0629, 196444.6159,
174407.0449, 114886.8676, 59714.23481, 60944.55308, 189449.8681,
189449.8681, 167655.0628, 48354.20928, 130013.1393, 173375.0026,
160083.8017, 156356.2922, 93367.9965, 155830.9163, 196434.0627,
111036.1316, 147924.4383, 130094.4442, 110740.2619, 180250.2648,
196459.0132, 128571.3777, 167655.0629, 160083.8017, 196877.4489,
110073.9451, 72839.48411, 66658.72684, 34400.54341, 191366.9356,
142614.132, 185346.9753, 195029.8994, 153000.4548, 174335.3955,
183445.8281, 196433.8595, 195027.1862, 48164.88123, 86628.41888,
192698.9312, 150234.408, 175199.9653, 177924.5096, 192730.4345,
196878.3235, 187250.6512, 122178.9993, 193078.476, 122110.0691,
180312.0705, 132791.5122, 195050.9669, 189491.6891, 196432.6449,
110092.6632, 47544.9055, 39401.69691, 151724.8938, 174367.861,
163267.6496, 84959.82215, 195050.9669, 174334.3813, 114385.5926,
136121.5477, 174335.3954, 99380.08521, 96012.29654, 196903.8799,
188981.9462, 26900.33901, 144806.3775, 181896.5244, 182719.3605,
61102.65255, 19741.08644, 174407.0449, 196186.7084, 174407.0449,
49160.44088, 185294.9958, 189447.4007, 195029.8994, 78249.88294,
175443.5234, 196906.2159, 196426.4033, 188983.876, 48447.89584,
72293.81391, 120400.3747, 75707.85928, 137471.9858, 60187.19617,
68395.23939, 86832.80395, 180312.0461, 189797.7904, 33511.74556,
196906.2159, 154302.1127, 132791.5122, 66767.84315, 151141.734,
172547.7844, 196434.0627, 99380.08521, 152897.0658, 99664.00085,
193282.3168, 196044.3122, 78806.56556, 145347.3239, 171144.0818,
31242.41669, 160083.8016, 160083.8017, 51851.21776, 74403.31886,
22031.14274, 147071.9195, 150545.6883, 61023.94095, 195042.1262,
196896.4658, 40242.42874, 192254.7086, 190394.1262, 70406.2492,
137426.2258, 99380.08519, 140062.7414, 192633.0614, 176633.5859,
180250.2648, 53869.02935, 142613.3149, 135530.3915, 168519.8947,
185169.7176, 174407.0449, 178691.312, 196443.0624, 148079.4063,
57246.18123, 27932.81263, 40415.31582, 114466.1176, 179818.8947,
93706.47211, 131532.3781, 192726.5206, 167655.0628, 74544.74404,
79470.40371, 67332.54758, 54594.64538, 86527.34867, 151724.8937,
142506.9822, 185109.6548, 87288.26795, 26623.47308, 62365.95905,
40654.42647, 174837.3988, 166235.3165, 192730.4345, 146116.5008,
159993.6391, 72847.57489, 195050.9669, 177538.7456, 196906.2159,
149249.3871, 196397.3296, 57847.69421, 87174.39279, 159993.6391,
111062.6658, 159993.6391, 196334.4073, 113564.9231, 50944.96715,
92400.13042, 174069.8016, 174335.3954, 152177.3195, 177458.1197,
99127.75301, 41868.22708, 122178.9993, 178391.3338, 180294.9067,
173326.9519, 99041.17658, 84287.92399, 99380.08519, 167074.1011,
192698.9312, 23275.03706, 194567.4548, 174407.0449, 110640.4665,
38304.51098, 116760.5063, 32312.56115, 192698.9312, 194912.5765,
30074.00305, 196434.0627, 64801.13889, 132791.5122, 172063.1196,
172932.0266, 95204.73836, 191443.2656, 167294.3229, 167603.8976,
168460.7176, 137621.5109, 195050.9669, 35523.49542, 87187.46136,
176019.8707, 118457.6403, 196434.0627, 111062.6969, 142614.132,
90241.70834, 174407.0449, 195050.9669, 185346.5708, 195050.9669,
186066.4133, 196434.7768, 186583.1961, 185346.692, 151626.0232,
188760.7471, 167655.0629, 196419.9902, 74544.74403, 192727.0388,
156139.2832, 171684.6684, 55093.68422, 180312.2015, 185294.9959,
103656.4268, 196444.6159, 174407.0449, 138519.227, 46366.84254,
192730.4345, 180312.2015, 180312.2015, 196414.8666, 165251.9959,
116955.1283, 132045.4789, 193991.0508, 71828.64499, 151651.5674,
186625.3351, 143211.3547, 43983.46389, 20833.59855, 189482.4773,
137525.1996, 189449.8681, 120134.4853, 122178.9993, 167655.0629,
188336.9525, 89348.85526, 180033.6182)), row.names = c(NA, -500L
), class = "data.frame")
# X and Y limits
x_min <- min(data$Longitude)
x_max <- max(data$Longitude)
y_min <- min(data$Latitude)
y_max <- max(data$Latitude)
# World map
world <- ne_countries(scale = "medium", returnclass = "sf")
# CRF
crf <- CRS(projargs = "+proj=longlat +datum=NAD83 +no_defs")
# Plot
ggplot() +
geom_raster(data = data, aes(x = Longitude, y = Latitude, fill = pred)) +
scale_fill_viridis_c(option = "plasma") +
geom_sf(data = world, fill = alpha("lightgrey", 1), color = "lightgrey") +
coord_sf(xlim=c(x_min, x_max), ylim=c(y_min, y_max), crs = crf, expand = FALSE)
I get this warning when I run the above code:
Error in matrix(NA_character_, nrow = nrow, ncol = ncol) :
invalid 'nrow' value (too large or NA)
In addition: Warning messages:
1: Raster pixels are placed at uneven horizontal intervals and will be shifted. Consider using geom_tile() instead.
2: Raster pixels are placed at uneven vertical intervals and will be shifted. Consider using geom_tile() instead.
3: In f(...) : NAs introduced by coercion to integer range
4: In f(...) : NAs introduced by coercion to integer range
I have tried using geom_tile / geom_stars and rounding my lat/lon values to no avail.
I think the issue might be arising due to the projection used which means the area of a grid cell decreases as you move away from the equator, hence I have also included grid cell area in the data (4th column).
Any help/suggestions would be greatly appreciated! Ideally without interpolating as these are already model predictions.
If you need the full dataset it can be found here: https://github.com/Rawizard/stack_overflow/blob/main/SO_question_data1.RDS
One approach to solve this would be to perform an interpolation on your data.
Here is an option based on the practical guide found here
Code
Starting by rounding the longitude and latitude with 0 digit. (unique() is to get rid of the duplicate data only 2 rows here)
library(dplyr)
df = data
df = df %>% dplyr::select(c(Longitude, Latitude, prediction))
df = df %>% mutate(across(c(Latitude, Longitude), round, digit = 0)) %>% unique()
Then creating a grid which will be used to interpolate the data:
df_template = expand.grid(Longitude= seq(min(df3$Longitude), max(df3$Longitude), 1), Latitude = seq(min(df3$Latitude), max(df3$Latitude), 1))
> head(df_template)
Longitude Latitude
1 -178 -83
2 -177 -83
3 -176 -83
4 -175 -83
5 -174 -83
6 -173 -83
Rasterizing the grid:
grd_template_raster <- df_template %>% dplyr::mutate(Z = 0) %>% raster::rasterFromXYZ(crs = crf)
Transformation of the data into simple feature:
sf_df <- st_as_sf(df, coords = c("Longitude", "Latitude"), crs = crf)
Model fitting in this case Nearest Neighbour
library(gstat)
fit_NN <- gstat::gstat( # using package {gstat}
formula = prediction ~ 1, # The column `NH4` is what we are interested in
data = as(sf_df, "Spatial"), # using {sf} and converting to {sp}, which is expected
nmax = 10, nmin = 5 # Number of neighboring observations used for the fit
)
Interpolate by running the model the data:
library(raster)
interp_NN = interpolate(grd_template_raster, fit_NN)
Extract the data to build the plot with ggplot:
df_final <- rasterToPoints(interp_NN) %>% as_tibble()
colnames(df_final) <- c("Longitude", "Latitude", "prediction")
Plotting the data:
ggplot() +
geom_raster(data = df_final, aes(x = Longitude, y = Latitude, fill = prediction)) +
scale_fill_viridis_c(option = "plasma") +
geom_sf(data = world, fill = alpha("lightgrey", 1), color = "lightgrey") +
coord_sf(xlim=c(x_min, x_max), ylim=c(y_min, y_max), crs = crf, expand = FALSE)

Plotly animation in R; frames are not in correct order, mix up in frames

The Data:
dput(LifeExpCH$Age)
c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
111)
> dput(LifeExpCH$Die)
c(380, 16, 11, 9, 9, 8, 7, 7, 7, 8, 7, 7, 8, 8, 8, 10, 11, 13,
16, 19, 20, 20, 21, 20, 19, 21, 20, 21, 23, 24, 25, 27, 30, 31,
35, 37, 41, 44, 48, 52, 57, 63, 70, 76, 84, 94, 104, 115, 129,
143, 159, 176, 195, 215, 237, 258, 283, 307, 334, 363, 392, 424,
458, 495, 534, 578, 624, 677, 734, 798, 869, 952, 1044, 1149,
1271, 1411, 1569, 1750, 1955, 2184, 2440, 2723, 3032, 3363, 3711,
4064, 4404, 4710, 4952, 5106, 5143, 5041, 4795, 4408, 3908, 3342,
2753, 2190, 1679, 1243, 888, 612, 406, 259, 158, 93, 51, 27,
13, 5, 2, 1)
I want to create a plotly animation in R. The animation is not working as intended. There is a mix up in the frames. Frames 100:109 are at the start. May I ask for some help, how to get the frames in the right order?
Here is the code:
library(plotly)
library(dplyr)
library(purrr)
LifeExpCH %>%
split(.$Age) %>% accumulate(~bind_rows(.x, .y)) %>%
set_names(0:111) %>%
bind_rows(.id = "frame") %>%
plot_ly(x = ~Age, y = ~Die) %>%
add_lines(frame = ~frame, showlegend = FALSE)

Scaling and centering a circular distribution with conversion.circular function

Using the circular package, I have a circular data object:
x <- structure(c(44, 95, 88, NA, 317, 16, NA, NA, 85, NA, 31, 14, 323, 336, 345, 338, 10, 3, 342, 325, 30, 311, 359, 335, 331, 4, 347, 9, 9, 33, 335, 345, 355, 346, 18, 9, 341, 24, 352, 343, 3, 22, NA, 356, 14, 5, 8, NA, 352, 36, NA, 312, 352, 8, 351, 338, 341, 316, 357, 331, 326, 8, 347, 342, 315, 336, 9, 355, 0, 227, 14, 292, NA, 342, 352, 351, 1, 339, 358, 341, 1, 22, 20, 1, 285, 357, 279, 13, 25, 16, 353, 348, 2, 354, 357, 32, 7, 351, 349, 24, 354, 352, 352, 357, 356, 355, 332, 348, 30, 351, 128, 348, 358, 337, 177, 11, 314, 282, 342, 285, 168, 14, 26, 32, 21, 13, 11, 19, 17, 29, 285, 22, 0, 35, 8, 7, 25, 357, 354, 17, 46, 14, 6, 350, 355, 324, NA, 298, 356, 6), class = c("circular", "numeric"), circularp = structure(list(type = "angles", units = "degrees", template = "none", modulo = "2pi", zero = 1.5707963267949, rotation = "clock"), .Names = c("type", "units", "template", "modulo", "zero", "rotation")))
hist(as.numeric(x)) looks like this:
How can I scale and re-center this data so that the range is [-pi:pi] and the centre is at 0? I have tried conversion.circular but can't get the parameters right.

SOM map - kohonen package - numerical result

I try SOM map analyze with package Kohonen. I used this tutorial : https://www.shanelynn.ie/self-organising-maps-for-customer-segmentation-using-r/ . This is my code :
require(kohonen)
data = matrix(
c(6, 6, 80, 280, 404, 0, 158, 158197, 158197233,
6, 13, 80, 280, 404, 0, 158, 158197, 158197233,
6, 13, 80, 283, 404, 0, 158, 158197, 158197233,
6, 35, 80, 321, 301, 0, 158, 158197, 158197233,
6, 35, 80, 3131, 200, 0, 158, 158197, 158197233,
6, 35, 80, 20073, 200, 0, 158, 158197, 158197233,
6, 35, 80, 183, 200, 0, 158, 158197, 158197233,
6, 35, 80, 25, 302, 0, 158, 158197, 158197233,
6, 35, 80, 13744, 200, 0, 158, 158197, 158197233,
6, 35, 80, 280, 404, 0, 158, 158197, 158197233,
9, 33, 80, 2859, 200, 0, 158, 158197, 15819736,
9, 33, 80, 463, 200, 0, 158, 158197, 15819736,
9, 33, 80, 2065, 200, 0, 158, 158197, 15819736,
9, 33, 80, 1298, 200, 0, 158, 158197, 15819736,
9, 33, 80, 86659, 200, 0, 158, 158197, 15819736,
9, 33, 80, 942, 200, 0, 158, 158197, 15819736,
9, 33, 80, 2027, 200, 0, 158, 158197, 15819736,
9, 33, 80, 2979, 200, 0, 158, 158197, 15819736,
9, 33, 80, 127390, 200, 0, 158, 158197, 15819736,
9, 33, 80, 1150, 200, 0, 158, 158197, 15819736,
9, 33, 80, 1150, 200, 0, 158, 158197, 15819736,
9, 33, 80, 942, 200, 0, 158, 158197, 15819736,
9, 40, 80, 287, 200, 0, 158, 158197, 15819736,
9, 40, 80, 90, 200, 0, 158, 158197, 15819736,
9, 40, 80, 2, 200, 0, 158, 158197, 15819736,
9, 40, 80, 3314, 200, 0, 158, 158197, 15819736,
9, 40, 80, 483, 200, 0, 158, 158197, 15819736,
9, 40, 80, 2, 200, 0, 158, 158197, 15819736,
9, 40, 80, 543, 200, 0, 158, 158197, 15819736,
9, 40, 80, 439, 200, 0, 158, 158197, 15819736,
9, 40, 80, 689, 200, 0, 158, 158197, 15819736,
9, 40, 80, 797, 200, 0, 158, 158197, 15819736,
9, 40, 80, 732, 200, 0, 158, 158197, 15819736,
9, 40, 80, 5403, 200, 0, 158, 158197, 15819736,
9, 40, 80, 496, 200, 0, 158, 158197, 15819736,
9, 40, 80, 743, 200, 0, 158, 158197, 15819736,
9, 40, 80, 3049, 200, 0, 158, 158197, 15819736,
9, 40, 80, 3064, 200, 0, 158, 158197, 15819736,
9, 40, 80, 3042, 200, 0, 158, 158197, 15819736,
9, 40, 80, 3077, 200, 0, 158, 158197, 15819736,
12, 8, 80,305 ,301 , 0, 142, 1424, 14245,
12, 8, 80, 10531,200 , 0, 142, 1424, 14245,
12, 8, 80, 445,400 , 0, 142, 1424, 14245,
12, 8, 80, 445,400 , 0, 142, 1424, 14245,
12, 8, 80, 445,400 , 0, 142, 1424, 14245,
12, 8, 80, 324,400 , 0, 142, 1424, 14245,
12, 8, 80, 445,400 , 0, 142, 1424, 14245,
0, 0, 80,0 ,0 , 0, 42, 424, 4245,
12, 25, 80, 171,200 , 0, 42, 424, 4245,
12, 25, 80, 2970,200 , 0,42, 424, 4245,
12, 25, 80, 171,200 , 0, 42, 424, 4245,
12, 25, 80, 2970,200 , 0, 42, 424, 4245,
12, 25, 80, 171,200 , 0, 42, 424, 4245,
12, 25, 80, 2970,200 , 0, 42, 424, 4245,
15, 32, 80, 2860,200 , 0, 88, 88212, 8821237,
15, 32, 80, 2859,200 , 0, 88, 88212, 8821237,
15, 32, 80, 86659,200 , 0, 88, 88212, 8821237,
15, 32, 80, 22495,200 , 0, 88, 88212, 8821237,
15, 32, 80, 949,200 , 0, 88, 88212, 8821237,
15, 32, 80, 1298,200 , 0, 88, 88212, 8821237,
15, 32, 80, 2027,200 , 0, 88, 88212, 8821237,
15, 32, 80, 2979,200 , 0, 88, 88212, 8821237,
15, 32, 80, 103646,200 , 0, 88, 88212, 8821237,
15, 32, 80, 406,200 , 0, 88, 88212, 8821237,
9, 34, 80, 1169,200 , 0, 88, 88212, 8821237,
9, 34, 80, 212,200 , 0, 88, 88212, 8821237,
9, 34, 80, 2250,200 , 0, 88, 88212, 8821237,
9, 34, 80, 8496,200 , 0, 88, 88212, 8821237,
9, 34, 80, 0,200 , 0, 88, 88212, 8821237,
9, 34, 80, 946,200 , 0, 88, 88212, 8821237,
9, 34, 80, 716263,200 , 0, 88, 88212, 8821237,
9, 34, 80, 63,200 , 0, 88, 88212, 8821237,
9, 34, 80, 63,200 , 0, 88, 88212, 8821237,
9, 34, 80, 1573,200 , 0, 88, 88212, 8821237,
9, 34, 80, 63,200 , 0, 88, 88212, 8821237,
9, 34, 80, 974,200 , 0, 88, 88212, 8821237,
9, 34, 80, 63,200 , 0, 88, 88212, 8821237,
9, 34, 80, 926,200 , 0, 88, 88212, 8821237,
9, 34, 80, 1150,200 , 0, 88, 88212, 8821237,
9, 34, 80, 800,200 , 0, 88, 88212, 8821237,
9, 34, 80, 225,200 , 0, 88, 88212, 8821237,
9, 34, 80, 79,200 , 0, 88, 88212, 8821237,
9, 34, 80, 408,200 , 0, 88, 88212, 8821237,
9, 34, 80, 535,200 , 0, 88, 88212, 8821237,
3, 21, 80, 208,404 , 0, 207, 20746, 2074613,
3, 49, 80, 1298,200 , 0, 207, 20746, 2074613,
5, 17, 80, 302,230 , 0, 207, 20746, 2074613,
8, 28, 80, 2857,200 , 0, 5, 5188, 518810,
13, 08, 80, 2860,200 , 0, 5, 5188, 518810,
14, 08, 80, 2860,200 , 0, 5, 5188, 518810,
15, 58, 80, 208,404 , 0, 66, 66249, 6624966,
15, 58, 80, 463,200 , 0, 66, 66249, 6624966,
15, 58, 80, 2065,200 , 0, 66, 66249, 6624966,
15, 58, 80, 2065,200 , 0, 66, 66249, 6624966,
13, 05, 80, 608,200 , 0, 88, 88212, 8821240,
13, 08, 80, 608,200 , 0, 88, 88212, 8821240,
13, 11, 80, 608,200 , 0, 88, 88212, 8821240,
13, 14, 80, 608,200 , 0, 88, 88212, 8821240,
13, 17, 80, 608,200 , 0, 88, 88212, 8821240,
13, 20, 80, 608,200 , 0, 88, 88212, 8821240 ),
nrow=100,
ncol=9,
byrow = TRUE)
data_train <- data[, c(1,2,4,5,7,8,9)]
#data_train <- data[, c(2,4,5,8)]
data_train_matrix <- as.matrix(scale(data_train))
som_grid <- somgrid(xdim = 3, ydim=4, topo="hexagonal")
som_model <- som(data_train_matrix,
grid=som_grid,
rlen=500,
alpha=c(0.05,0.01),
keep.data = TRUE )
#training proces
plot(som_model, type="changes")
#nodes
plot(som_model, type="count", main="Node Counts")
#distance
#plot(som_model, type="dist.neighbours", main = "SOM neighbour distances")
#codes and weight vectors
#plot(som_model, type="codes")
#heatmap
plot(som_model, type = "property", property = getCodes(som_model)[,4], main="Heat map - parameter 4")
And this is my map visualization :
Map is OK. My question is: is there some way how to find which data was in white node ? I look at getCodes(som_model) but there is just map numbers. So I known that white node is V4. But what data was in node V4 ? I look into all som_model values, but it dont help. Any ideas ?
> getCodes(som_model)[,4]
V1 V2 V3 V4 V5 V6 V7 V8
-0.727734454 -0.183272487 -0.342681905 2.361366190 -0.343764866 -0.343764866 1.298987948 -0.343532184
V9 V10 V11 V12
-0.343764307 1.350552793 -0.003492471 -0.343764866
I want result for example like this :
> inV4
[1] 2 25 0
Is possible do this in Kohonen package?
UPDATE :
It look it is not possible do it easy in Kohonen pack. So I try this :
# find which node is white
q <- getCodes(som_model)[,4]
for (i in 1:length(q)){
if(q[i]>2){
t<- q[i]
}
}
# find name od node
node <- names(t)
#remove "V" letter from node name
mynode <- gsub("V","",node)
#find which node has which input ???
mydata2 <- som_model$unit.classif
print (myadat2)
#choose just imputs which go to right node
result <- vector('list',length(mydata2))
for (i in 1:length(mydata2)){
result <- som_model$unit.classif== mynode
}
#remove FALSE results
result2 <- which(result == TRUE)
#write all input line
for (i in 1:length(result2)){
print (data[result2[i],])
}
But I am not sure if this is right way. And I am not sure if this give me right result inputs. Is there any way how to check it ?
Sorry if this is too late to help you. It may help someone else.
Because you did not set the random seed, we cannot exactly reproduce your result. When I ran your code, I got the plot
and I got the codes:
getCodes(som_model)[,4]
V1 V2 V3 V4 V5 V6 V7
-0.3437649 -0.3437649 2.3146262 0.4037323 -0.3437649 -0.6034393 -0.3434484
V8 V9 V10 V11 V12
0.6730257 0.2089917 -0.3437649 -0.1038754 2.6302823
For me, the white node is V12. You can get the data points that are in V12 by looking at the unit classifications that are part of the som object.
som_model$unit.classif
[1] 12 12 12 9 9 9 9 9 9 12 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
[26] 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 3 2 3 3 3 3 3 6 6 6
[51] 6 6 6 6 1 1 1 1 1 1 1 1 1 1 5 5 5 5 5 5 7 5 5 5 5
[76] 5 5 5 5 5 5 5 5 5 8 8 8 6 6 6 4 4 4 4 2 2 2 2 2 2
To get which points are in V12, you can simply use:
which(som_model$unit.classif == 12)
[1] 1 2 3 10

Getting the error "level sets of factors are different" when running a for loop

I have the following 3 tables:
AggData <- structure(list(Path = c("NonBrand", "Brand", "NonBrand,NonBrand",
"Brand,Brand", "NonBrand,NonBrand,NonBrand", "Brand,Brand,Brand",
"Brand,NonBrand", "NonBrand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,Brand,Brand,Brand,Brand", "Brand,Brand,NonBrand", "NonBrand,Brand,Brand",
"Brand,NonBrand,NonBrand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"NonBrand,NonBrand,Brand", "Brand,NonBrand,Brand", "NonBrand,Brand,NonBrand",
"NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,Brand,Brand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"NonBrand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,Brand",
"Brand,Brand,Brand,NonBrand", "Brand,Brand,Brand,Brand,Brand,Brand,Brand",
"Brand,NonBrand,NonBrand,NonBrand", "NonBrand,NonBrand,Brand,Brand",
"Brand,Brand,NonBrand,NonBrand", "Brand,NonBrand,Brand,Brand",
"NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,Brand,NonBrand,Brand", "NonBrand,Brand,NonBrand,NonBrand",
"Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"NonBrand,NonBrand,Brand,NonBrand", "Brand,NonBrand,NonBrand,Brand",
"NonBrand,Brand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand,Brand",
"Brand,NonBrand,Brand,NonBrand", "NonBrand,Brand,Brand,NonBrand",
"Brand,Brand,Brand,Brand,NonBrand", "Brand,NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,NonBrand,Brand,Brand,Brand", "NonBrand,Brand,NonBrand,Brand",
"Brand,Brand,Brand,NonBrand,Brand", "NonBrand,NonBrand,Brand,Brand,Brand",
"NonBrand,NonBrand,NonBrand,Brand,Brand", "Brand,Brand,NonBrand,Brand,Brand",
"Brand,Brand,Brand,NonBrand,NonBrand", "Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand",
"NonBrand,NonBrand,NonBrand,Brand,NonBrand", "Brand,Brand,NonBrand,NonBrand,NonBrand",
"NonBrand,Brand,Brand,Brand,Brand,Brand", "NonBrand,Brand,NonBrand,NonBrand,NonBrand",
"NonBrand,NonBrand,Brand,NonBrand,NonBrand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,Brand",
"Brand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand", "Brand,Brand,Brand,Brand,Brand,NonBrand",
"NonBrand,Brand,Brand,NonBrand,NonBrand", "Brand,NonBrand,NonBrand,Brand,Brand",
"NonBrand,NonBrand,NonBrand,NonBrand,Brand,Brand", "NonBrand,NonBrand,Brand,Brand,Brand,Brand",
"NonBrand,NonBrand,NonBrand,NonBrand,Brand,NonBrand", "NonBrand,NonBrand,Brand,NonBrand,Brand",
"Brand,NonBrand,NonBrand,Brand,NonBrand", "NonBrand,NonBrand,NonBrand,Brand,Brand,Brand",
"NonBrand,Brand,Brand,NonBrand,Brand", "Brand,NonBrand,NonBrand,NonBrand,NonBrand,Brand",
"Brand,Brand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand", "Brand,Brand,Brand,Brand,NonBrand,NonBrand,NonBrand"
), click_count = c(1799265, 874478, 198657, 128159, 45728, 30172,
20520, 17815, 16718, 9479, 6554, 3722, 3561, 3408, 3391, 3366,
3256, 2526, 1846, 1708, 1682, 1013, 951, 899, 881, 782, 780,
703, 642, 625, 615, 601, 453, 442, 414, 407, 362, 343, 313, 284,
281, 281, 271, 269, 268, 229, 223, 218, 215, 212, 204, 162, 161,
158, 155, 145, 132, 130, 115, 103, 102, 86, 77, 77, 72, 68, 68,
67, 58, 52, 32, 18, 18), conv_count = c(30938, 19652, 7401, 3803,
2014, 1072, 1084, 981, 652, 379, 230, 166, 205, 246, 254, 93,
239, 104, 112, 51, 76, 23, 66, 81, 55, 29, 62, 57, 50, 37, 17,
33, 38, 17, 8, 41, 33, 30, 24, 16, 26, 18, 16, 17, 7, 21, 10,
8, 27, 23, 11, 13, 6, 15, 14, 16, 8, 10, 6, 6, 11, 11, 8, 9,
8, 8, 9, 7, 7, 6, 6, 6, 7), CR = c(0.0171947989873643, 0.0224728352228415,
0.0372551684561833, 0.0296740767328085, 0.0440430370888733, 0.0355296301206417,
0.0528265107212476, 0.0550659556553466, 0.0389998803684651, 0.0399831205823399,
0.0350930729325603, 0.0445996775926921, 0.057568098848638, 0.0721830985915493,
0.0749041580654674, 0.0276292335115865, 0.0734029484029484, 0.0411718131433096,
0.0606717226435536, 0.0298594847775176, 0.0451843043995244, 0.0227048371174729,
0.0694006309148265, 0.0901001112347052, 0.0624290578887628, 0.0370843989769821,
0.0794871794871795, 0.0810810810810811, 0.0778816199376947, 0.0592,
0.0276422764227642, 0.0549084858569052, 0.0838852097130243, 0.0384615384615385,
0.0193236714975845, 0.100737100737101, 0.0911602209944751, 0.0874635568513119,
0.0766773162939297, 0.0563380281690141, 0.0925266903914591, 0.0640569395017794,
0.0590405904059041, 0.0631970260223048, 0.0261194029850746, 0.091703056768559,
0.0448430493273543, 0.036697247706422, 0.125581395348837, 0.108490566037736,
0.053921568627451, 0.0802469135802469, 0.0372670807453416, 0.0949367088607595,
0.0903225806451613, 0.110344827586207, 0.0606060606060606, 0.0769230769230769,
0.0521739130434783, 0.058252427184466, 0.107843137254902, 0.127906976744186,
0.103896103896104, 0.116883116883117, 0.111111111111111, 0.117647058823529,
0.132352941176471, 0.104477611940299, 0.120689655172414, 0.115384615384615,
0.1875, 0.333333333333333, 0.388888888888889)), .Names = c("Path",
"click_count", "conv_count", "CR"), row.names = c(NA, -73L), class = "data.frame")
another one here:
breakVector <- structure(list(breakVector = structure(c(1L, 1L), .Label = "NonBrand", class = "factor"),
CR = c(0.461541302855402, 0.538458697144598)), .Names = c("breakVector",
"CR"), row.names = c(NA, -2L), class = "data.frame")
and:
FinalTable <- structure(list(autribution_category = structure(c(2L, 1L), .Label = c("Brand",
"NonBrand"), class = "factor"), attributed_result = c(0, 0)), .Names = c("autribution_category",
"attributed_result"), row.names = 1:2, class = "data.frame")
when I run the following command:
if (FinalTable [2,1] == breakVector[1,1]) {
FinalTable$attributed_result[2] <- FinalTable$attributed_result[2] +
breakVector[1,2] * AggData$conv_count[3];
break}
I get the following error:
Error in Ops.factor(FinalTable[2, 1], breakVector[1, 1]) :
level sets of factors are different
This is pretty weird, since both values that im comparing are factors, I don't see any reason why R cant compare the two levels?
FinalTable[2,1] and breakVector[1,1] do not have the same levels:
> FinalTable[2,1]
[1] Brand
Levels: Brand NonBrand
> breakVector[1,1]
[1] NonBrand
Levels: NonBrand
This is easily fixed by using
breakVector[,1] <- factor(breakVector[,1], levels=c("Brand", "NonBrand"))
or, more generally
breakVector[,1] <- factor(breakVector[,1], levels=levels(FinalTable[,1]))
Perhaps, it will better compare both variables like a string:
if (as.character(FinalTable [2,1]) == as.character(breakVector[1,1])) {
FinalTable$attributed_result[2] <- FinalTable$attributed_result[2] +
breakVector[1,2] * AggData$conv_count[3];
break}

Resources