Select specific area from a matrix with R - r
I have computed and image in R, and I have defined certain pixels as 0, I only want the "0" that are completely surrounded by pixels with quantities ranging:
inpixno0 <- filter(inx0, inx0$pixel!=0)
range(inpixno0$pixel)
# [1] 0.5000476 0.6998763`
For example, I only want the black area surrounded by the red area.
I have created a data frame containing the pixels mentioned and their indexes. It looks like this:
> inx0[326:333,]
# row col pixel
# 326 36 34 0.5141253
# 327 37 34 0.5039121
# 328 38 34 0.0000000
# 329 39 34 0.0000000
# 330 40 34 0.0000000
# 331 41 34 0.5376547
# 332 42 34 0.5866648
# 333 43 34 0.6188273
dim(inx0)
# [1] 12350 3
I have got a lot of these data frames, and my idea is to extract the row and col indexes for the values equal to 0 I am interested in and have a new data frame for every data frame that I have of these.
I like to know how it can be done with an efficient code so I can apply it to all the data frames that I have of these characteristics (over 1500).
Reproducible data set (500 observations only):
structure(list(row = c(87, 87, 92, 93, 94, 96, 101, 80, 91, 92,
93, 94, 95, 96, 97, 98, 99, 100, 102, 91, 92, 93, 94, 95, 96,
97, 98, 99, 100, 102, 104, 78, 80, 82, 83, 92, 93, 94, 95, 96,
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 79, 97, 98, 99,
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 102, 104, 105,
106, 107, 108, 109, 110, 105, 106, 107, 108, 109, 110, 111, 112,
113, 116, 108, 110, 111, 112, 113, 114, 115, 54, 55, 56, 57,
58, 59, 62, 112, 114, 115, 116, 117, 55, 56, 57, 58, 117, 53,
54, 55, 56, 57, 58, 59, 60, 47, 48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 121, 122, 45, 46, 47, 48, 49, 50, 51, 52,
53, 54, 55, 56, 57, 58, 59, 60, 122, 123, 124, 126, 44, 45, 46,
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 124, 125,
126, 127, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
55, 56, 125, 126, 127, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 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, 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, 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, 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, 145, 146, 147, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 49, 146, 25,
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
42, 43, 44, 45, 46, 47, 48, 145, 146, 147, 23, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 133, 145, 147, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 133, 134, 135, 150, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 51, 53, 62, 63, 64, 77, 134, 135, 136, 150, 151,
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 52, 53, 60, 62, 65, 83, 84, 135, 136, 137, 150, 151,
152, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 49, 51, 52, 53), col = c(13, 14, 14, 14, 14, 14,
14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19,
19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21,
21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23,
23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 33, 33,
33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33,
33, 33, 33, 33, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
35, 35, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, 36, 36,
36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
36, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38,
38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38,
38, 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39,
39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39,
39, 39, 39, 39, 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40,
40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40), pixel = c(0.692499524081477,
0.695745288406624, 0.694355606320198, 0.699857224443175, 0.692061679040548,
0.688235294117647, 0.696363982486199, 0.693023034456501, 0.686645726251666,
0.686769465067581, 0.691909385113268, 0.686398248619836, 0.683447553778793,
0.682352941176471, 0.675623453264802, 0.678555111364934, 0.680392156862745,
0.696449647820293, 0.698781648581763, 0.693318103940605, 0.695878545592994,
0.684751570531125, 0.667628022082619, 0.665705311250714, 0.679392727964972,
0.687235865219874, 0.65221778031601, 0.66082238720731, 0.69564058633162,
0.695478774033885, 0.666542927850752, 0.694669712545212, 0.685722444317533,
0.693565581572435, 0.69776318294308, 0.683761660003807, 0.687074052922141,
0.676470588235294, 0.675889967637541, 0.683704549781079, 0.67843137254902,
0.645040928992958, 0.666666666666667, 0.674538359032933, 0.683761660003807,
0.662440510184657, 0.66749476489625, 0.67395773843518, 0.696354464115744,
0.696906529602132, 0.684161431562916, 0.680192271083191, 0.68913953931087,
0.695926137445269, 0.67365315058062, 0.664705882352941, 0.671844660194175,
0.675414049114791, 0.685522558537979, 0.680944222349134, 0.688235294117647,
0.681496287835522, 0.679840091376357, 0.688939653531315, 0.686274509803922,
0.665810013325719, 0.696935084713497, 0.662745098039216, 0.671692366266895,
0.665134209023415, 0.66317342470969, 0.695402627070246, 0.699552636588616,
0.694060536836093, 0.658319055777651, 0.664258518941558, 0.677089282314869,
0.67848848277175, 0.685208452312963, 0.697087378640777, 0.690700552065487,
0.699495526365886, 0.699562154959071, 0.68271463925376, 0.682352941176471,
0.697096897011231, 0.681772320578717, 0.696659051970303, 0.69499333714068,
0.6954121454407, 0.69804873405673, 0.689548829240434, 0.699352750809062,
0.696078431372549, 0.686931277365315, 0.697363411383971, 0.696040357890728,
0.690196078431373, 0.677136874167143, 0.657490957548067, 0.686255473063011,
0.662250142775557, 0.676708547496669, 0.68063011612412, 0.695821435370265,
0.699743003997716, 0.688577955454026, 0.688235294117647, 0.651104130972778,
0.6613744526937, 0.670930896630497, 0.679706834189987, 0.693993908242908,
0.690538739767752, 0.681343993908242, 0.650856653340947, 0.666162193032553,
0.651361126975062, 0.640481629545022, 0.66324957167333, 0.655473063011612,
0.653826384922901, 0.661345897582336, 0.678117266324005, 0.668056348753094,
0.662678469446032, 0.672168284789644, 0.693232438606511, 0.688301922710831,
0.68026841804683, 0.695117075956596, 0.619760137064534, 0.631448695983247,
0.595155149438416, 0.605035217970683, 0.612726061298306, 0.598191509613554,
0.636217399581192, 0.64594517418618, 0.657900247477632, 0.643099181420141,
0.664667808871121, 0.676660955644393, 0.677317723205787, 0.670702455739577,
0.696078431372549, 0.694079573577004, 0.694193794022463, 0.693194365124691,
0.687312012183514, 0.638749286122216, 0.619493622691795, 0.573586521987436,
0.562859318484676, 0.555939463163906, 0.58927279649724, 0.597344374643061,
0.619493622691795, 0.644060536836094, 0.646135541595279, 0.629525985151342,
0.655939463163906, 0.681429659242338, 0.671397296782791, 0.679240434037693,
0.694698267656577, 0.694346087949743, 0.664011041309728, 0.698039215686274,
0.693080144679231, 0.668846373500857, 0.629944793451361, 0.578964401294499,
0.541442984960975, 0.532438606510566, 0.536093660765277, 0.547058823529412,
0.580972777460498, 0.603074433656958, 0.645050447363412, 0.631639063392347,
0.667780316009899, 0.659137635636779, 0.682667047401484, 0.672549019607843,
0.687654673519894, 0.67843137254902, 0.661897963068723, 0.654835332191128,
0.663801637159717, 0.684951456310678, 0.634941937940225, 0.678012564249,
0.652807919284217, 0.624367028364745, 0.605044736341138, 0.516105082809823,
0.565343613173423, 0.538663620788119, 0.511897963068724, 0.579973348562726,
0.614563106796117, 0.626965543498952, 0.641947458595088, 0.633333333333333,
0.673320007614696, 0.67556634304207, 0.693413287645156, 0.674186179326099,
0.681781838949172, 0.686521987435751, 0.676146963639825, 0.615810013325719,
0.623653150580621, 0.614667808871121, 0.630801446792309, 0.632467161621931,
0.626556253569389, 0.633009708737864, 0.609680182752713, 0.5368075385494,
0.529735389301352, 0.538397106415382, 0.56294498381877, 0.618465638682657,
0.614296592423377, 0.641871311631449, 0.622834570721493, 0.657062630877594,
0.662050256996002, 0.674833428517038, 0.68468494193794, 0.689053873976775,
0.682038834951455, 0.643755948981534, 0.651599086236436, 0.659956215495907,
0.671825623453265, 0.643137254901961, 0.597211117456691, 0.579259470778603,
0.602884066247857, 0.604330858557014, 0.607947839329907, 0.6,
0.570797639444128, 0.580696744717303, 0.545821435370265, 0.536017513801637,
0.578745478774035, 0.606396344945745, 0.625899486007995, 0.621463925375975,
0.651085094231868, 0.694327051208834, 0.691538168665525, 0.682762231106034,
0.684827717494765, 0.698239101465828, 0.669712545212259, 0.691614315629165,
0.627117837426232, 0.625823339044356, 0.614391776127928, 0.636169807728917,
0.622777460498763, 0.605672948791168, 0.580934703978679, 0.57592804111936,
0.590405482581381, 0.594450790024748, 0.56145059965734, 0.546183133447553,
0.525823339044356, 0.528869217589949, 0.539758233390443, 0.573091566723777,
0.603921568627451, 0.61927470017133, 0.633999619265183, 0.642804111936036,
0.699581191699982, 0.691614315629165, 0.689862935465448, 0.687692747001713,
0.691823719779173, 0.681420140871883, 0.651818008756901, 0.664144298496097,
0.623063011612412, 0.603455168475157, 0.603359984770607, 0.620074243289549,
0.612231106034647, 0.566009899105273, 0.559385113268609, 0.550980392156863,
0.576936988387588, 0.553788311441082, 0, 0, 0, 0.540243670283648,
0.609242337711784, 0.617180658671236, 0.618208642680373, 0.649952408147725,
0.657053112507139, 0.683380925185608, 0.687492861222159, 0.696544831524843,
0.695612031220255, 0.669931467732724, 0.68458975823339, 0.685884256615268,
0.661593375214165, 0.659984770607271, 0.654901960784314, 0.603150580620599,
0.57998286693318, 0.571749476489624, 0.589015800494955, 0.593346659051971,
0.543137254901961, 0.507433847325337, 0.514125261755188, 0.503912050256995,
0, 0, 0, 0.537654673519894, 0.586664762992576, 0.618827336759947,
0.643156291642872, 0.641566723776889, 0.662354844850562, 0.6694079573577,
0.689015800494955, 0.683932990671998, 0.688777841233584, 0.665257947839328,
0.638587473824482, 0.617494764896247, 0.590196078431373, 0.566590519703026,
0.575061869407956, 0.587921187892633, 0.569017704169047, 0.527450980392157,
0, 0, 0, 0, 0, 0.503997715591092, 0.581724728726442, 0.628545592994481,
0.61152674662098, 0.630192271083193, 0.670350276032745, 0.680468303826386,
0.655682467161624, 0.695526365886162, 0.683133447553781, 0.697020750047591,
0.695840472111176, 0.695069484104321, 0.65638682657529, 0.640995621549591,
0.620083761660004, 0.592156862745098, 0.566190748143918, 0.536541024176661,
0.560308395202741, 0.544146202170189, 0, 0, 0, 0, 0, 0, 0.582114981915096,
0.613725490196078, 0.617647058823529, 0.656148867313916, 0.676946506758043,
0.655853797829811, 0.693165810013326, 0.698039215686274, 0.690434037692747,
0.691680944222349, 0.686112697506187, 0.691024176660957, 0.691833238149629,
0.655549209975252, 0.626803731201219, 0.594117647058824, 0.571235484485056,
0.552132114981916, 0.528583666476298, 0.529411764705882, 0.50491147915477,
0, 0, 0, 0, 0.507519512659433, 0.596887492861221, 0.613240053302875,
0.610451170759565, 0.644269940986102, 0.672710831905578, 0.696402055968018,
0.690357890729107, 0.685627260612984, 0.688397106415382, 0.69181420140872,
0.68141062250143, 0.660955644393679, 0.66709499333714, 0.637426232628973,
0.610317913573195, 0.60204644964782, 0.564534551684752, 0.518846373500854,
0.502731772320577, 0, 0, 0, 0, 0, 0, 0.583038263849227, 0.625318865410242,
0.623529411764706, 0.660869979059585, 0.692670854749665, 0.698296211688558,
0.690538739767751, 0.695059965733864, 0.692499524081476, 0.693356177422422,
0.697867885018085, 0.674252807919285, 0.676556253569389, 0.669988577955456,
0.686702836474395, 0.639367980201788, 0.672549019607843, 0.641252617551874,
0.639187131163145, 0.607824100513992, 0.59600228440891, 0.537302493813059,
0.525461640967067, 0, 0, 0, 0, 0, 0, 0.551008947268227, 0.590177041690463,
0.63720731010851, 0.668598895869027, 0.688206739006282, 0.694155720540643,
0.68640776699029, 0.696040357890729, 0.688216257376737, 0.694155720540643,
0.680468303826384, 0.68045878545593, 0.696078431372549, 0.676461069864839,
0.692213972967827, 0.697934513611271, 0.68239101465829, 0.682381496287835,
0.653141062250143, 0.645231296402056, 0.640643441842755, 0.617780316009899,
0.596078431372549, 0.554968589377499, 0.534361317342469, 0, 0,
0, 0, 0, 0, 0.527784123358082, 0.593851132686084, 0.630972777460498,
0.665105653912051, 0.692156862745098, 0.692423377117838, 0.686541024176662,
0.690329335617742)), .Names = c("row", "col", "pixel"), row.names = c(NA,
500L), class = "data.frame")
This might be a good use for the little used matrix indexing. Here is a simpler example:
> mat <- matrix(1:9, 3,3)
> mat
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> mat[cbind(1:2, 3:2)]
[1] 7 5
It looks like you already have the rows and columns of the pixels you want, so it would be something like :
inpixno0[cbind(rows, cols)]
Related
How to consecutively subset an array and count the number of iterations?
I need to repeatedly apply a function on the resultant arrays until all data in the array is reduced to a single set, and count the number of iterations. Data Array ar structure(c(0, 11, 17, 15, 22, 67, 73, 68, 31, 31, 28, 33, 34, 32, 11, 0, 9, 12, 21, 67, 73, 67, 35, 30, 34, 67, 60, 36, 17, 9, 0, 6, 19, 70, 74, 68, 36, 36, 36, 64, 66, 39, 15, 12, 6, 0, 13, 64, 69, 66, 34, 37, 39, 77, 65, 45, 22, 21, 19, 13, 0, 59, 60, 66, 38, 39, 39, 40, 43, 43, 67, 67, 70, 64, 59, 0, 10, 18, 77, 75, 78, 93, 93, 85, 73, 73, 74, 69, 60, 10, 0, 15, 76, 74, 80, 103, 101, 95, 68, 67, 68, 66, 66, 18, 15, 0, 59, 65, 73, 90, 87, 82, 31, 35, 36, 34, 38, 77, 76, 59, 0, 8, 19, 24, 28, 32, 31, 30, 36, 37, 39, 75, 74, 65, 8, 0, 12, 20, 22, 23, 28, 34, 36, 39, 39, 78, 80, 73, 19, 12, 0, 6, 14, 18, 33, 67, 64, 77, 40, 93, 103, 90, 24, 20, 6, 0, 2, 8, 34, 60, 66, 65, 43, 93, 101, 87, 28, 22, 14, 2, 0, 6, 32, 36, 39, 45, 43, 85, 95, 82, 32, 23, 18, 8, 6, 0), .Dim = c(14L, 14L)) From a<-colSums(ar<25) b<-which.max(a) c<-ar[ar[,b] > 25,, drop = FALSE] we get structure(c(0, 11, 17, 15, 22, 67, 73, 68, 11, 0, 9, 12, 21, 67, 73, 67, 17, 9, 0, 6, 19, 70, 74, 68, 15, 12, 6, 0, 13, 64, 69, 66, 22, 21, 19, 13, 0, 59, 60, 66, 67, 67, 70, 64, 59, 0, 10, 18, 73, 73, 74, 69, 60, 10, 0, 15, 68, 67, 68, 66, 66, 18, 15, 0, 31, 35, 36, 34, 38, 77, 76, 59, 31, 30, 36, 37, 39, 75, 74, 65, 28, 34, 36, 39, 39, 78, 80, 73, 33, 67, 64, 77, 40, 93, 103, 90, 34, 60, 66, 65, 43, 93, 101, 87, 32, 36, 39, 45, 43, 85, 95, 82), .Dim = c(8L, 14L)) then from a<-colSums(c<25) b<-which.max(a) d<-c[c[,b]>25,,drop=FALSE] we get structure(c(67, 73, 68, 67, 73, 67, 70, 74, 68, 64, 69, 66, 59, 60, 66, 0, 10, 18, 10, 0, 15, 18, 15, 0, 77, 76, 59, 75, 74, 65, 78, 80, 73, 93, 103, 90, 93, 101, 87, 85, 95, 82), .Dim = c(3L, 14L)) applying once more a<-colSums(d<25) b<-which.max(a) e<-d[d[,b]>25,,drop=FALSE] results in a array with no values structure(numeric(0), .Dim = c(0L, 14L)) Then, the operation can be performed a number of times, and I need to know how many times; in this case it was 3 times. Also, I need to reduce the number of the lines of the code probably with a loop function, as the action is repetitive.
You can use recursion as shown below: fun<- function(x, i=1){ a<-which.max(colSums(x<25)) b<-x[x[,a] > 25,, drop = FALSE] if(length(b)) Recall(b, i+1) else i } fun(ar) [1] 3
Specifying bins in histogram on the x- axis using ggplot2 in r
In am plotting histogram in r an I want to specify my bins but I am getting some else. This is my lenght data pss <- structure(list(LengthSize = c(48, 39, 94, 30, 81, 49, 44, 85, 44, 55, 45, 47, 44, 43, 42, 44, 76, 42, 65, 43, 43, 90, 105, 32, 31, 43, 36, 65, 21, 15, 113, 113, 44, 46, 94, 90, 95, 37, 25, 72, 49, 46, 48, 49, 49, 44, 50, 48, 37, 37, 55, 60, 65, 30, 22, 26, 43, 43, 43, 43, 18, 67, 110, 64, 28, 29, 38, 37, 38, 37, 38, 70, 58, 65, 55, 60, 40, 22, 68, 88, 88, 32, 44, 86, 37, 38, 67, 52, 48, 123, 50, 114, 37, 38, 39, 41, 60, 55, 50, 99, 57, 44, 45, 45, 51, 44, 45, 37, 39, 43, 43, 50, 51, 34, 42, 44, 46, 67, 67, 56, 56, 57, 56, 47, 65, 66, 43, 41, 69, 45, 114, 60, 55, 37, 88, 85, 39, 39, 46, 50, 60, 44, 77, 61, 68, 46, 114, 51, 105, 48, 95, 32, 40, 28, 42, 47, 46, 48, 50, 96, 45, 47, 118, 55, 60, 34, 118, 39, 52, 119, 40, 55, 60, 55, 59, 102, 73, 42, 78, 56, 74, 102, 88, 38, 36, 33, 34, 41, 120, 50, 46, 79, 98, 65, 40, 45, 42, 50, 61, 44)), row.names = c(NA, 200L), class = "data.frame") This is my code in ggplot2. pss %>% ggplot(data = pss,breaks = 25,xlim = c(0,528,11), mapping = aes(x = LengthSize )) + geom_histogram(binwidth = 10, col = "black", fill = "grey")
You're in the right track, just modify your script as follows. You can experiment with binwidth and bins. You can easily modify or remove the title, xlab and ylab if you don't want them. library(ggplot2) ggplot(pss, mapping = aes(LengthSize)) + geom_histogram(binwidth = 3, bins=50, col = "black", fill = "grey") + ggtitle("My plot title") + xlab("My X axis label - length size") + ylab("Y axis label - Fequency count") + theme(plot.title = element_text(hjust = 0.5)) On the basis of the sample data above, the script produces the following histogram.
Determining the FWHM from a distribution in R
I have a dataset: x = c(30, 23, 22, 31, 18, 16, 19, 16, 15, 23, 21, 17, 17, 27, 24, 22, 27, 32, 23, 21, 14, 19, 22, 23, 22, 19, 13, 22, 33, 25, 24, 26, 24, 24, 13, 21, 23, 23, 31, 23, 25, 20, 24, 23, 24, 16, 19, 33, 36, 29, 29, 26, 22, 23, 23, 23, 20, 27, 32, 30, 34, 37, 28, 24, 29, 32, 35, 32, 25, 28, 27, 32, 33, 25, 25, 30, 25, 25, 26, 36, 30, 37, 27, 25, 26, 34, 38, 29, 30, 32, 31, 33, 33, 31, 37, 36, 42, 32, 37, 34, 37, 38, 31, 25, 27, 27, 32, 33, 39, 38, 36, 32, 35, 33, 36, 40, 39, 32, 32, 34, 35, 38, 43, 43, 39, 30, 33, 39, 39, 46, 51, 40, 30, 37, 48, 52, 47, 58, 41, 47, 51, 55, 56, 46, 34, 47, 45, 48, 52, 66, 56, 49, 66, 71, 63, 47, 41, 50, 56, 58, 56, 56, 61, 49, 57, 58, 52, 65, 70, 75, 59, 55, 44, 48, 49, 49, 55, 61, 58, 56, 56, 62, 64, 58, 59, 64, 64, 61, 56, 62, 64, 78, 86, 80, 75, 65, 75, 66, 72, 85, 65, 70, 63, 58, 73, 83, 89, 78, 75, 79, 84, 92, 90, 80, 76, 82, 83, 82, 82, 93, 93, 79, 92, 97, 84, 85, 81, 90, 99, 106, 100, 96, 97, 101, 128, 120, 121, 109, 125, 118, 121, 118, 123, 108, 115, 123, 123, 117, 113, 124, 128, 142, 134, 120, 116, 112, 129, 149, 150, 134, 134, 133, 139, 165, 167, 173, 158, 170, 188, 186, 191, 172, 163, 176, 182, 188, 205, 203, 219, 211, 229, 242, 245, 273, 270, 285, 292, 329, 355, 358, 362, 403, 429, 480, 516, 512, 525, 544, 575, 595, 668, 622, 612, 627, 649, 620, 576, 608, 576, 545, 471, 435, 422, 416, 405, 372, 338, 299, 285, 279, 274, 251, 241, 213, 201, 197, 203, 197, 196, 189, 184, 166, 165, 161, 167, 160, 157, 139, 131, 141, 152, 143, 144, 140, 136, 148, 123, 114, 113, 109, 122, 134, 120, 103, 117, 134, 117, 106, 114, 112, 104, 86, 94, 103, 108, 98, 109, 98, 100, 108, 114, 92, 78, 83, 111, 98, 78, 80, 80, 68, 62, 76, 74, 89, 78, 85, 86, 86, 76, 71, 72, 72, 64, 76, 77, 77, 89, 76, 65, 61, 66, 68, 72, 75, 72, 67, 67, 69, 75, 65, 63, 75, 68, 65, 59, 68, 61, 60, 63, 63, 58, 63, 59, 49, 68, 55, 60, 67, 65, 69, 68, 53, 59, 64, 45, 43, 42, 48, 46, 50, 52, 41, 38, 44, 38, 51, 50, 51, 41, 40, 41, 41, 34, 41, 32, 35, 39, 52, 46, 38, 37, 39, 36, 36, 34, 41, 40, 38, 38, 47, 45, 46, 36, 40, 34, 32, 39, 41, 47, 38, 38, 33, 44, 37, 38, 30, 34, 30, 40, 43, 41, 31, 27, 39, 34, 31, 29, 29, 25, 38, 38, 33, 42, 45, 46, 42, 37, 40, 35, 50, 34, 29, 25, 30, 36, 35, 36, 35, 24, 22, 29, 29, 32, 32, 25, 32, 30, 28, 23, 28, 34, 31, 28, 30, 27, 27, 20, 25, 32, 32, 41, 28, 19, 22, 23, 20, 25, 31, 27, 24, 26, 21, 20, 25, 33, 31, 44, 31, 31, 22, 29, 29, 32, 20, 24, 26, 27, 28, 24, 16, 19, 24, 23, 28, 27, 22, 24, 18, 19, 19, 21, 26, 26, 25, 28, 28, 32, 32, 26, 23, 31, 27, 20, 18, 29, 25, 15, 23, 28, 29) I know the data follows the following distribution, a quadratic background with a cauchy peak: a + bx + cx^2 + dx^3 + ex^4 + fx^5 + gx^6 + (A/pi)(gamma/((x-pos)^2 + gamma^2)) I am trying to determine the optimum parameters to fit this data in order to find the 'gamma' parameter. This will tell me the FWHM of the distribution. I only know the 'pos' parameter. This is the location of the peak. Here is a plot of this distribution: I have achieved this in python using the lmfit package but have not found a way to do this in R yet. I believe it requires the fitdistrplus package but I have had no luck. Any ideas?
Even though the data sets are a little different, you should be able to use the same recipe as found at R Find Full width at half maximum for a gausian density distribution -- to get FWHM you need a density curve which is the part that's missing: > d <- density(na.omit(x),n=1e4) > xmax <- d$x[d$y==max(d$y)] > x1 <- d$x[d$x < xmax][which.min(abs(d$y[d$x < xmax]-max(d$y)/2))] > x2 <- d$x[d$x > xmax][which.min(abs(d$y[d$x > xmax]-max(d$y)/2))] > points(c(x1, x2), c(d$y[d$x==x1], d$y[d$x==x2]), col="red") > (FWHM <- x2-x1) [1] 43.37897
Add vertical lines to time-series plot
I have the code below which plots two time-series. I'd like to add a vertical line every say 10 units on the x-axis to basically divide the plot up into like 5 squares. Any tips are very much appreciated. Code: ## Plot Forecast & Actual ts.plot(ts(CompareDf$stuff1),ts(CompareDf$stuff2),col=1:2,xlab="Hour",ylab="Minu tes",main='testVar') legend("topleft", legend = c("Actual","Forecast"), col = 1:2, lty = 1) Data: dput(CompareDf) structure(list(stuff1 = c(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), stuff2 = c(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)), .Names = c("stuff1", "stuff2"), row.names = c(NA, -50L), class = "data.frame")
After plotting timeseries data, use abline to draw vertical lines. abline(v = seq(10, 50, 10))
Discretize the columns first - Apriori in R
I extracted this data from a file: forests<-read.table("~/Desktop/f.txt", header = FALSE, sep = " ", fill = TRUE) library(arules) //after installing the package I get an error when I type forests<- apriori(forests, parameter = list(support=0.3)) Error in asMethod(object) : column(s) 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 not logical or a factor. Discretize the columns first. I tried discritize(forests). It still doesn't work. itemFrequencyPlot(forests) also gives an error saying unable to find inherited method. Have I imported the data incorrectly?