polygon shading the inverse region in R- how to fix - r

I am plotting a truncated density in R. Here are the coordinates of my polygon from dput output:
dat <- structure(list(pro.d.x = c(0.587084148727984, 0.589041095890411,
0.590998043052838, 0.592954990215264, 0.594911937377691, 0.596868884540117,
0.598825831702544, 0.600782778864971, 0.602739726027397, 0.604696673189824,
0.60665362035225, 0.608610567514677, 0.610567514677104, 0.61252446183953,
0.614481409001957, 0.616438356164384, 0.61839530332681, 0.620352250489237,
0.622309197651663, 0.62426614481409, 0.626223091976517, 0.628180039138943,
0.63013698630137, 0.632093933463796, 0.634050880626223, 0.63600782778865,
0.637964774951076, 0.639921722113503, 0.64187866927593, 0.643835616438356,
0.645792563600783, 0.647749510763209, 0.649706457925636, 0.651663405088063,
0.653620352250489, 0.655577299412916, 0.657534246575342, 0.659491193737769,
0.661448140900196, 0.663405088062622, 0.665362035225049, 0.667318982387475,
0.669275929549902, 0.671232876712329, 0.673189823874755, 0.675146771037182,
0.677103718199609, 0.679060665362035, 0.681017612524462, 0.682974559686888,
0.684931506849315, 0.686888454011742, 0.688845401174168, 0.690802348336595,
0.692759295499021, 0.694716242661448, 0.696673189823875, 0.698630136986301,
0.700587084148728, 0.702544031311155, 0.704500978473581, 0.706457925636008,
0.708414872798434, 0.710371819960861, 0.712328767123288, 0.714285714285714,
0.716242661448141, 0.718199608610568, 0.720156555772994, 0.722113502935421,
0.724070450097847, 0.726027397260274, 0.727984344422701, 0.729941291585127,
0.731898238747554, 0.73385518590998, 0.735812133072407, 0.737769080234834,
0.73972602739726, 0.741682974559687, 0.743639921722113, 0.74559686888454,
0.747553816046967, 0.749510763209393, 0.75146771037182, 0.753424657534246,
0.755381604696673, 0.7573385518591, 0.759295499021526, 0.761252446183953,
0.76320939334638, 0.765166340508806, 0.767123287671233, 0.769080234833659,
0.771037181996086, 0.772994129158513, 0.774951076320939, 0.776908023483366,
0.778864970645793, 0.780821917808219, 0.782778864970646, 0.784735812133072,
0.786692759295499, 0.788649706457926, 0.790606653620352, 0.792563600782779,
0.794520547945205, 0.796477495107632, 0.798434442270059, 0.800391389432485,
0.802348336594912, 0.804305283757339, 0.806262230919765, 0.808219178082192,
0.810176125244618, 0.812133072407045, 0.814090019569472, 0.816046966731898,
0.818003913894325, 0.819960861056751, 0.821917808219178, 0.823874755381605,
0.825831702544031, 0.827788649706458, 0.829745596868884, 0.831702544031311,
0.833659491193738, 0.835616438356164, 0.837573385518591, 0.839530332681018,
0.841487279843444, 0.843444227005871, 0.845401174168297, 0.847358121330724,
0.849315068493151, 0.851272015655577, 0.853228962818004, 0.85518590998043,
0.857142857142857, 0.859099804305284, 0.86105675146771, 0.863013698630137,
0.864970645792564, 0.86692759295499, 0.868884540117417, 0.870841487279843,
0.87279843444227, 0.874755381604697, 0.876712328767123, 0.87866927592955,
0.880626223091976, 0.882583170254403, 0.88454011741683, 0.886497064579256,
0.888454011741683, 0.89041095890411, 0.892367906066536, 0.894324853228963,
0.896281800391389, 0.898238747553816, 0.900195694716243, 0.902152641878669,
0.904109589041096, 0.906066536203523, 0.908023483365949, 0.909980430528376,
0.911937377690802, 0.913894324853229, 0.915851272015656, 0.917808219178082,
0.919765166340509, 0.921722113502935, 0.923679060665362, 0.925636007827789,
0.927592954990215, 0.929549902152642, 0.931506849315068, 0.933463796477495,
0.935420743639922, 0.937377690802348, 0.939334637964775, 0.941291585127201,
0.943248532289628, 0.945205479452055, 0.947162426614481, 0.949119373776908,
0.951076320939335, 0.953033268101761, 0.954990215264188, 0.956947162426614,
0.958904109589041, 0.960861056751468, 0.962818003913894, 0.964774951076321,
0.966731898238748, 0.968688845401174, 0.970645792563601, 0.972602739726027,
0.974559686888454, 0.976516634050881, 0.978473581213307, 0.980430528375734,
0.98238747553816, 0.984344422700587, 0.986301369863014, 0.98825831702544,
0.990215264187867, 0.992172211350294, 0.99412915851272, 0.996086105675147,
0.998043052837573, 1), pro.d.y = c(0, 0.0124763366981593, 0.0141124551851867,
0.0159018210787856, 0.0178518210828018, 0.0199740509194942, 0.0223445527492011,
0.0249064839074225, 0.0276662964727761, 0.0306302708705622, 0.0338545211312297,
0.0373385397435565, 0.0410496060511471, 0.0449918664776745, 0.0491758970805405,
0.0536901895276933, 0.0584490455650341, 0.0634544237153568, 0.0687081431462095,
0.0742700062159316, 0.0801277089300282, 0.086240025634052, 0.0926086975186578,
0.0992440060892446, 0.106239285811027, 0.113504059066459, 0.121044019265177,
0.128865573418761, 0.137048548852888, 0.145593863797181, 0.154467617291928,
0.163685889084493, 0.173280379056521, 0.183418988934254, 0.193997374886688,
0.205045023363949, 0.21659280832808, 0.228818951343029, 0.241754902638203,
0.255354359835895, 0.269660574110284, 0.284751550765693, 0.300974352004478,
0.318105788417593, 0.336193761251992, 0.355285850148719, 0.37570768608243,
0.397453045785548, 0.420391899838677, 0.444560692193431, 0.470052830600742,
0.497323432291453, 0.525934510701712, 0.55589495930279, 0.587210052668763,
0.620209479614381, 0.654737312164808, 0.690555064040503, 0.727627117977952,
0.765968447827405, 0.805823333992903, 0.846691934407899, 0.888496561425915,
0.931155864506105, 0.974725912749141, 1.01894272802093, 1.06358946338081,
1.10855652184076, 1.15373258418124, 1.19891666804584, 1.24392280480721,
1.28863577510934, 1.33294134574882, 1.37653543493332, 1.41927889838227,
1.46115790850083, 1.50208011643625, 1.54188812802516, 1.58010796114519,
1.61704327373793, 1.65264078703095, 1.68685158559086, 1.71924489000963,
1.74994440160701, 1.77914340182238, 1.80683605037902, 1.8329284799749,
1.85702633860664, 1.87966618832539, 1.90087561596266, 1.92068458372179,
1.93878680429694, 1.95543121172768, 1.97084637077113, 1.98507694736024,
1.99809848497021, 2.00970880002476, 2.0203323177052, 2.03001268341046,
2.03879277291169, 2.04650577512196, 2.05333770822687, 2.05943314402644,
2.06482425018454, 2.06949798196726, 2.07333994584229, 2.07659348278548,
2.07927970501823, 2.08141886898654, 2.08289568306932, 2.0838167309169,
2.08426362597097, 2.08425136563913, 2.08376347737203, 2.08272129413889,
2.0812848925494, 2.07947064189011, 2.07729538738197, 2.07469292214384,
2.07175241370009, 2.06853824861803, 2.06507440050244, 2.06137194834198,
2.05743920123393, 2.05338026815633, 2.04922923154637, 2.04502128439529,
2.04081183457356, 2.03667849489226, 2.03266717082546, 2.02882485726764,
2.02522571223853, 2.02203770460098, 2.0192476674025, 2.01691496522578,
2.01510017656093, 2.01407190394343, 2.01385052206395, 2.01443658686903,
2.01590202326501, 2.01841515958639, 2.02236038715918, 2.02751514911719,
2.03395909497369, 2.0417722975773, 2.0515066820042, 2.06304583606758,
2.07630508949246, 2.09136419448661, 2.1084876952701, 2.12825404999518,
2.15014999861247, 2.17424485778335, 2.2006062832039, 2.23002312279098,
2.26215697251894, 2.2968041549068, 2.33400970407032, 2.37407124084828,
2.41760250489697, 2.4638260220103, 2.51275499122616, 2.56439904161634,
2.61956960340193, 2.67771334572469, 2.73852719993077, 2.80198177705806,
2.86829607508334, 2.93788571360972, 3.00990094479677, 3.08426858458271,
3.16091081580917, 3.24034038243829, 3.3219275671303, 3.40533998955121,
3.49044957580254, 3.57725694079797, 3.66572547223431, 3.75521994794363,
3.84555419923005, 3.93653584878367, 4.02795152621416, 4.11931406838431,
4.21034136592417, 4.30077594829744, 4.39020281325105, 4.47783699125228,
4.56363022834507, 4.64726414556742, 4.72841524545307, 4.80564182077226,
4.87897706777627, 4.94833350100604, 5.01335537401227, 5.07307996209023,
5.1259118810479, 5.17287726780172, 5.21364011252759, 5.24787000019841,
5.27287709283815, 5.28975954430921, 5.29886896386876, 5.29997375028775,
5.29185426024869, 5.27278869421654, 5.24503234244234, 5.20852067701082,
5.16320857873499, 5.10629616085988, 5.04001017913819, 4.96520581342729,
4.88204743797809)), .Names = c("pro.d.x", "pro.d.y"), row.names = 301:512, class = "data.frame")
I generally shade a polygon region fairly easy using plot() and polygon() like this:
plot(dat,xlim = c(0,1), ylim = c(0,10), bty = 'n', xlab = NA, ylab = NA, main = NA, yaxs='i', xaxs = 'i', las = 1, cex = 0)
polygon(dat, col = 'green')
However this plot looks like this:
Its shading some type of inverse of what I would like, and adding this very straight line. Any idea how I could shade all regions below the line?

The concept of polygon() is to connect all the points in order and at last it will link the last point to the first one. (That is why the straight line is created) To deal with it, you need to make an additional vertex c(1, 0) as a relay between the first and last points.
polygon(rbind(dat, c(1, 0)), col = 'green')

Related

factoextra variable plotting and labeling subset

I've run a PCA using prcomp in R and I am trying to produce a variable plot that has 1) a subset of the variables (arrows) in a different color (black) than the rest of the variables, 2) sort those variables prior to plotting so the black arrows aren't covered up by any of the other arrows, and 3) label the black arrows with their TUXXXX number.
Here is a truncated version of my data:
structure(list(sdev = c(21.7106794138444, 15.6885074594869, 11.9124316528111,
10.155277241318, 9.31528828036412, 7.56876266263865, 7.19938201515987,
5.81620977435434, 5.00785424840699, 4.57228787327195, 4.51525494575488,
3.20601607034873, 2.91477640215067, 2.48737967730048, 2.13230488376163,
1.74923754200417, 1.32745772948038, 1.27373216417502, 0.924437474777366,
0.749074623004602, 2.499709597053e-15), rotation = structure(c(-0.0710441966458092,
0.091894514828866, -0.0892433537986534, -0.269473709517009, -0.270455466278075,
0.217492458575054, 0.104541973199297, 0.198858094257877, 0.0222680112919805,
-0.220704163347643, -0.0144913885562279, 0.191255085890651, -0.0639203495167002,
0.156262929972648, 0.184067836594737, -0.221797618857792, 0.152932751853774,
0.218733634550932, 0.213989867048259, -0.219209176045661, 0.162232843055052,
-0.182649403408074, 0.0732706002149817, -0.0636694683826245,
0.197950156891487, -0.201332798849071, -0.0908133550739104, 0.204561640782066,
0.144543461720936, 0.128722984809536, 0.152014925749702, -0.219186163224382,
0.137427869376807, -0.0385728465064452, -0.210836096156869, 0.295086522896984,
0.0791956927556792, 0.154250304318938, 0.212537035577304, 0.165554637810987,
0.0559168852882808, 0.0389301896007549, 8.50109782205011e-06,
0.0386719091643949, -0.223181316780788, 0.0201295766763838, -0.0993945550528442,
-0.0797657364873552, 0.290392094157839, -0.210104310439075, 0.190507620860241,
0.288216604566037, -0.128091623622577, 0.0523741236254804, 0.165286635170839,
0.26567738477018, 0.067304658063138, -0.185395359345644, -0.0824392053628523,
0.0232331903282599, 0.206712718290849, -0.0599454190622519, 0.0694655529225049,
0.27934713843206, 0.120105146108152, 0.276987279291797, 0.137068573563571,
0.290227550805898, 0.0299855300238849, -0.048045392789825, 0.217209812823655,
-0.245207498414819, -0.0969050397538762, 0.0152958298727508,
0.0611874790392221, 0.244015616675892, -0.341439467152496, -0.0574074669754257,
-0.187473105527714, 0.0859725296785313, -0.230526618631008, 0.164238788534203,
-0.422116092865793, -0.0847447867791298, 0.0984005915083912,
0.0400031956936472, -0.0768787993932683, -0.127305543959199,
0.174417860300221, 0.00642366483942508, -0.0644270895895563,
-0.0445557418009, -0.206781215165792, -0.398796252833277, 0.198729650467593,
0.0351312161529938, -0.149474677035792, -0.0671760700655275,
-0.0776573448858521, -0.0276268199179851, -0.0622850132226139,
-0.0133848915206553, -0.0412751078511757, -0.196695235499305,
0.035533391699955, -0.0471461480863407, -0.471887698745297, 0.385807939659813,
-0.0521508210675749, -0.106054753553622, -0.0520098951237357,
-0.328301752147492, -0.0885218905714762, -0.392528395483506,
-0.0535243088811493, -0.0012232141956296, -0.143968515821013,
0.110020791115645, 0.105789408005642, -0.0173550741395983, -0.0754711706175302,
0.105789800532602, 0.035696880367874, -0.0235700328573188, -0.086664772131814,
0.140659513401983, -0.147738109860937, 0.158253547879328, 0.096508294839766,
-0.0405645836586841, -0.0308947063294566, 0.196563611460491,
-0.163272613036823, 0.102159062108236, -0.242801481572191, 0.100060947435785,
-0.122408620775568, 0.0879253395566385, 0.0964448760240853, -0.10574290240921,
0.473324985332234, 0.237791221156891, 0.121312056042028, -0.0156952213549262,
-0.0509545579371996, -0.188663582956394, -0.0296470996543493,
0.259217775431442, -0.151966417414955, -0.0246227447960926, 0.412107323913548,
0.135772975480753, -0.0902168853917747, -0.0478519874649824,
-0.0293395316078502, 0.036873372126565, -0.0600803315985544,
0.0951569802806819, -0.174539345831377, -0.0567399129758035,
-0.0522218071944251, -0.0208963041931566, 0.15376867866167, -0.0906491458699936,
-0.13257887196968, -0.0137277293203767, -0.329880055323168, -0.227898895128165,
-0.0402178711138279, -0.191724054954671, -0.0457418642155951,
-0.113122585201102, -0.0523451728177034, -0.0619052030873901,
-0.232037793152732, -0.286608825250971, -0.337575786471338, -0.106930203013502,
-0.0488020861881439, -0.0342694484533206, -0.316010730818033,
-0.138362452107825, 0.000204654351180751, 0.34062110315846, -0.0174472565351551,
0.321216705099307, 0.109402271713093, -0.195953316697484, 0.106147314965255,
0.0965509971711528, -0.0619150877056984, 0.0823900429544073,
0.107331603831664, -0.196505739977505, -0.106531330626324, 0.083316396319378,
-0.0150328553267955, -0.131557865292901, -0.197676924192316,
-0.302438247351333, 0.0850937235677142, -0.0588088540505814,
-0.0137660099545295, 0.0981614485249126, 0.064387690302654, 0.0870877447014138,
0.115369776587117, 0.0743160202050587, -0.302341999379974, 0.0990657432090211,
-0.109416851119016, 0.0566207941620112, -0.563376445425201, 0.022987878311214,
0.00914995477596945, -0.0698458973541682, -0.339551294424273,
0.328653723240687, -0.311621856766325, 0.0451475830297207, 0.0486778657760599,
-0.203890915518391, -0.00733314039012801, 0.0302380986964549,
-0.187740946530342, 0.107505967223115, 0.0112058761308914, 0.0124189091072754,
-0.0946013978939246, -0.0108516955206006, -0.0224544300789198,
-0.200447923165361, 0.00227210575436315, -0.0230219082173417,
-0.115276809069961, 0.113599893064251, 0.0547021334782484, 0.0750295713780988,
0.0318176533771422, 0.270756307602172, 0.0158888930662066, -0.0321100878211108,
0.0137635276916354, 0.285629078757821, -0.0707869220022327, -0.196194173921016,
0.0691894805409008, 0.196425060869134, 0.0609662656649608, 0.0112726648905893,
0.0763548314916228, -0.0203154117969338, -0.154183879477363,
-0.263435512251247, 0.0904584427059006, 0.443081105454767, 0.145760707944433,
-0.124076693641366, -0.00926033658765893, 0.322008337751576,
0.038976848601001, -0.00243771729397693, 0.0682300992336678,
0.101208051579165, -0.0044801862648156, -0.0288479608546419,
0.338293188340527, -0.148520145256712, -0.114001067101895, 0.0429068793268258,
-0.0808048267081677, 0.0147906601414549, 0.00024012812721683,
-0.0245592922986743, 0.26189600932665, 0.0089069926197661, 0.102640316156789,
0.0293398861736338, 0.461748240785653, 0.0268162505216955, -0.0262617915066989,
0.128477574136056, 0.304269602566599, -0.00833894425455883, 0.0183612622798514,
-0.0694598653855358, 0.143473468921974, 0.494741840190451, 0.11710470731812,
0.0938975501248067, -0.111819356177393, 0.074487393087207, -0.236744656433776,
-0.00888263838265717, -0.0411308668141714, -0.0205247425377322,
0.00682064532990936, -0.0657589971316789, -0.0854778766938379,
-0.0446898232128541, 0.0595158665026168, -0.164975323219787,
-0.21297106776461, -0.24328292167101, -0.0421999896796148, -0.0454152302659223,
0.536798625888851, -0.107184764029374, -0.134710620292912, -0.0486878301757869,
0.0112145455041351, -0.0888773206829193, -0.0231311387150055,
0.160224502964451, 0.116642580196556, 0.132842779999407, 0.0815222556677455,
0.0846715273158058, -0.168358316697627, -0.141086214309714, -0.0812000194350307,
0.108662664398591, -0.0391921007274459, -0.231315089883372, 0.25650378734853,
-0.0459946623780035, -0.366253502200094, 0.0485143397750607,
0.0417032700002287, -0.133038776710167, -0.230298287475923, 0.0369759420468897,
-0.00906710613565646, 0.0342935061381997, -0.273232725280753,
0.0602621389601039, 0.206808600842623, -0.32050527301245, 0.0622454407253566,
-0.0606525149750042, -0.0660628546307464, -0.038482743495315,
-0.198081930190034, -0.0732284456644516, 0.405606839140943, 0.0459681574877996,
-0.0809991829793473, -0.0121246261443008, -0.326485629157016,
0.0750251561011149, -0.0511696428776832, -0.0776271119484428,
0.0114894367280979, 0.014021492503364, 0.118849898460583, -0.0350044617639092,
0.0269961041673681, 0.239744113644073, -0.233879279575839, -0.115473220682409,
-0.139126309932733, 0.11002945246376, -0.0830577400425309, -0.0642289884705754,
-0.0817307298240943, 0.0291046627888711, -0.0466920006759583,
0.256672775663942, -0.0137954035992047, 0.17957481447807, -0.0303657675236596,
0.518263963411537, 0.344005787563606, -0.0655688570898937, -0.0265294401946153,
-0.0263107898868621, 0.289506920843548, 0.118475732210224, 0.053567958881615,
0.146877133183997, -0.0549414997788912, 0.143851895777107, 0.0428308701652828,
-0.348959761946987, -0.189096210288467, -0.00385035890156567,
0.150120708161381, 0.0607203081654835, 0.000166036942981724,
-0.0397011322296128, 0.0239760961564877, -0.00913095901818713,
-0.136862964262375, 0.102111106897839, 0.0687169707815706, 0.239013538410316,
-0.39892902681439, -0.197510750116376, 0.0499804963638917, 0.128640553281137,
0.110040614593457, -0.0490679379082568, -0.234025706976098, 0.157764576030734,
0.0571664700394519, -0.074307378291659, -0.231764546118876, 0.565447067892318,
-0.21480675567977, 0.0854469138131175, -0.0976097155414531, 0.160801495571792,
-0.0624041394929365, -0.0127140410117323, 0.196633195933416,
0.0340211274197625, 0.0195595932420065, 0.0964345475133984, -0.174817763577626,
0.118153342593374, 0.225094093283825, 0.0276798513722757, -0.39244132040106,
0.0738620568768489, 0.027031867335908, 0.0584738274201628, 0.00728266929277696,
-0.200928813049723, 0.100479151028423, 0.217133686900663, 0.152350478604359,
0.0798552081200005, 0.00566909653422267, 0.15071802028838, -0.0828967915619975,
0.0689991484295119, 0.118221935665031, 0.418906970577945, 0.161468821548893,
-0.0443917405359232, 0.127346300562372, 0.0854487046042784, 0.0086243432217815,
0.0186528057922736, 0.143224492276055, -0.113522100459258, 0.365748845830972,
-0.353026210924788, 0.0928434654809545, -0.217256921150109, 0.0926292235900054,
-0.0360081503134767, -0.0761247497474667, 0.0137710983238269,
0.135604749840715, -0.366785665744512, 0.174928851514231, 0.0364877873595186,
0.0808239023932249, 0.057715463934493, 0.103601691734105, -0.109666857605533,
0.38393023627746, -0.117248332196572, 0.134270206368149, 0.0580313640704774,
0.0649484539445183, 0.215353645432973, 0.0977108936702795, 0.16612143102929,
0.152770485581267, -0.0516344648811396, -0.0620940862015108,
0.114473317921704, -0.0383202829927206, 0.0820220075422196, 0.0556449561108403,
0.0053103511581772, 0.141569483443786, 0.264761186392854, 0.231379833347928,
-0.186458908266326, -0.280612868599116, -0.0134977078164976,
-0.190682413873154, 0.103622962625572, 0.048589128670959, 0.189228510250879,
-0.267141675131746, 0.24968486643315, 0.000515248155113446, 0.0518714761513495,
-0.0123619986636973, -0.0400774164248301, 0.14696585867848, 0.00893605318819822,
-0.115310054522583, 0.0730556770081751, -0.0415182843892857,
-0.100197819082248, 0.366565649542296, -0.280217314004845, 0.0742548639941826,
-0.0598573375691124, 0.0773367591528449, 0.00440008401952238,
-0.133823453930746, 0.0129306082883093, 0.249237874757721, 0.178566150191566,
0.0236542331383419, -0.121983928027758, -0.407639692892733, 0.105625218150781,
0.0606170741355181, -0.205658012407281, 0.198411895098939, 0.314706002474467,
-0.0699572649574542, -0.256054196724344, -0.0597932960838045,
0.12552805883941, -0.0738282459049305, -0.270044343022259, -0.241030766471284,
0.101198016298546, 0.207020228203116, -0.105134856921501, -0.0677078010226698,
0.0597983546937891, -0.181549492585234, -0.0707807644693492,
-0.242992185702685, -0.164512180143508, 0.0345701932987703, -0.0663154906257285,
0.302765631613708, 0.118224308922645, 0.0673423264385151, 0.417740310978627,
0.111554431260226, 0.16022442648127, -0.25635936207313, -0.35265959952747,
0.0429263261414316, 0.140480243375922, -0.130382613873709, -0.0446328161712632,
0.120679204177165, 0.100894280068327, 0.102396715497024, 0.292226072514004,
0.0170232777619504, -0.0854499461046589, 0.150649869168952, 0.054457847943396,
-0.0755734422376126, -0.0485946974863497, -0.214957242429833,
-0.19078567042026, -0.265677083691519, 0.11169177584997, -0.0546945656068388,
-0.133918556644411, -0.0815433043760734, 0.0419081230722499,
-0.119806690064691, -0.129001645490559, 0.164330559962565, -0.494999436348284,
-0.0104711534954494, -0.151933262861035, -0.0616048245349729,
0.0510091356535412, -0.15286661063535, 0.286795952380893, -0.00227878893487643,
-0.121875652782209, -0.0463279998907995, -0.08440989195395, 0.0161463685432679,
0.283239464786081, -0.0997334879966379, -0.107077349937415, 0.0797083319878824,
-0.023817698507559, 0.0308411589066249, -0.33639040110823, -0.22283646210506,
-0.0472382156514798, -0.0383954704026199, -0.279873414947505,
-0.110928067466998, 0.0224738031700519, -0.240448693328791, -0.0234165327843327,
-0.164995870598194, 0.0704836439566211, -0.0341351365684308,
0.253109207656731, 0.0986224720533927, 0.0788051554263656, 0.0348921204456385,
0.179396594950779, 0.183681460022634, 0.165162541829685, -0.131056895026594,
-0.043658366747374, -0.164919051972395, 0.000438005379084683,
-0.203731301273092, 0.156795003278361, 0.147134057978626, 0.0674666995224469,
0.0387098969653998, 0.0780676163151537, -0.020902824954741, 0.0863172379869736,
0.0254543096586073, 0.221894385139484, 0.605679699321179, 0.00510726652451246,
0.0122597366937914, -0.177909018628657, 0.112879199323714, 0.00722763837901889,
-0.226515396833189, -0.179516219763039, 0.0427914553668244, -0.301499857490821,
0.07077374887633, 0.0684478824696676, 0.109600278015023, -0.246938356324243,
0.0361050298642269, -0.273671368850468, 0.0361740234532937, -0.0379154357295922,
-0.140858482198813, -0.210557939874765, -0.183530140411959, -0.170246755949232,
-0.153695213839514, 0.197257212733899, 0.176328880404244, 0.267335680405474,
0.0108203611164604, 0.420718967278865, 0.0610513794750232, -0.0695142190235678,
-0.0211301631659223, -0.109598478540236, 0.0168455051311258,
0.0297211637285332, 0.0641419705460361, 0.196768178957862, -0.101223881022882,
0.156790420489079, -0.248396712859248, -0.0324362090799154, 0.116497846273851,
0.206631759837322, -0.26949738606709, 0.0523825016986687, 0.273991705979531,
-0.211717836239047, 0.343025575540513, 0.123225187210276, -0.130175293604759,
0.383814266477628, -0.0425846714760806, -0.120958839664608, -0.222129739206683,
0.0129931836329733, 0.144776049791581, -0.162517014653887, -0.0859042045925978,
-0.181054333724447, 0.0216585016380889, -0.0923250223535876,
-0.103640362754872, -0.223956862384563, 0.213416939275391, -0.21381164272001,
-0.235056303853187, -0.0630316125230271, -0.105100763021652,
-0.0360147146171049, -0.0709796908578211, 0.113552355258653,
-0.00724023466942227, -0.0325253318186826, -0.0460572605581843,
0.362655177721721, -0.102542875980008, -0.0801797191999012, 0.107198034030814,
0.183373945987406, 0.0309647928318137, -0.236970374446373, 0.0520664942182686,
-0.0402703424381589, 0.0504291632173252, -0.158130484290709,
-0.112283134517499, -0.300637215703608, -0.231320162708774, 0.0785169224262154,
0.23958352054759, 0.426485005426312, -0.0815406319434192, 0.00340039424643938,
-0.0375428689912434, 0.33351700972973, -0.0800149829632403, 0.140498383489292,
0.167368656035586, -0.0844117015281891, 0.0785085162751926, 0.0489486062002369,
-0.0561511863757523, 0.234387154088994, 0.0481142285230063, 0.0705808780719704,
0.221875388986379, -0.148031374880166, -0.0894717302805895, 0.231743942045981,
-0.210616362796393, -0.0812013243962482, -0.185771800435443,
-0.0438135376399639, 0.218374086144347, 0.121623152294345, -0.170058260806661
), .Dim = c(35L, 21L), .Dimnames = list(c("TU35976", "TU38792",
"TU35975", "TU4897", "TU9262", "TU18312", "TU38793", "TU30299",
"TU38794", "TU16422", "TU32068", "TU12311", "TU18325", "TU22117",
"TU20746", "TU19660", "TU41790", "TU43092", "TU18309", "TU19659",
"TU16277", "TU34798", "TU10953", "TU18317", "TU18307", "TU9186",
"TU17864", "TU15658", "TU14030", "TU15669", "TU9890", "TU33617",
"TU16279", "TU27042", "TU17167"), c("PC1", "PC2", "PC3", "PC4",
"PC5", "PC6", "PC7", "PC8", "PC9", "PC10", "PC11", "PC12", "PC13",
"PC14", "PC15", "PC16", "PC17", "PC18", "PC19", "PC20", "PC21"
))), center = c(TU35976 = 6.61238491831215, TU38792 = 10.2487864648085,
TU35975 = 7.41959195648095, TU4897 = 10.9311685931669, TU9262 = 11.0918083964267,
TU18312 = 11.7076238975148, TU38793 = 9.74398564779285, TU30299 = 12.892315214803,
TU38794 = 9.54139631961704, TU16422 = 12.3329627148834, TU32068 = 7.97610519995008,
TU12311 = 10.3617106799956, TU18325 = 7.59060411317826, TU22117 = 9.03799676912002,
TU20746 = 9.178102720165, TU19660 = 9.87502757868133, TU41790 = 8.9943466756264,
TU43092 = 10.9585816938684, TU18309 = 10.6298994150062, TU19659 = 10.3934726050624,
TU16277 = 8.32556725898017, TU34798 = 7.02103110645297, TU10953 = 7.01860382462013,
TU18317 = 7.36244270525821, TU18307 = 11.3778296252987, TU9186 = 10.7024172065364,
TU17864 = 8.2903853475227, TU15658 = 6.58819311715545, TU14030 = 8.134641008701,
TU15669 = 7.01449935590416, TU9890 = 8.13988320730633, TU33617 = 10.6809972208427,
TU16279 = 7.9614057227348, TU27042 = 7.34221912393268, TU17167 = 11.2592403834747
), scale = FALSE, x = structure(c(24.4897868475554, 12.4416010164283,
12.3200413661576, 6.91612686789472, 30.5655787046573, 33.5373707608136,
6.7521417841798, 31.2938791704631, 29.0635289584208, -23.3354523484558,
-15.6229701200559, 6.43010789907339, -24.9147092475567, -31.0094950116289,
-28.4137928961069, -21.5926413362042, -3.97585196983663, -20.8563124842282,
-0.593920033332157, -3.64572299218967, -19.8492949360489, -9.96693484414473,
-23.5449684594658, -18.996265716943, -23.7811581634436, 3.93738430048,
9.37353195322868, -19.423648038774, 7.87596964384287, -3.13661328005585,
-7.09016527838172, 3.71259770410569, 23.247076153704, -2.03249135782906,
-3.64325919770972, -6.94450353973124, 7.55306940052889, 31.346240781817,
-3.48931847017886, 29.4056029647248, 12.4861765573918, -6.8883231131661,
-6.8786118382072, 15.8325477899497, 15.3870141654559, -11.3237572934353,
-4.49560209730399, 1.115062768913, -20.2141393532659, 9.76193925462921,
-1.15731242202584, 6.22677163564855, 14.431909276018, -13.4812455995592,
-9.58096578254001, -10.0824168036361, -10.1417065317313, 6.11246229818054,
-4.84525793168699, 14.9352609583166, 16.1272133946181, -17.0836840355263,
9.35451814718846, -6.63382933222961, 5.79244304277846, 7.73028939318989,
-6.00113726185851, -8.57575275731579, -6.70414747438031, 11.0021295692077,
7.62205208005063, -6.03281844054887, -1.59444275830952, -7.70227835025931,
-6.4184560830441, -12.3696585650364, 1.42826497938592, 8.28315195406988,
-13.8464231033084, -6.95076374785849, 5.03179685038719, 16.6482451825695,
25.4557137741208, -6.16437895161065, -0.188023057317339, 3.51919999146276,
-3.06021456766217, 12.7300628490286, 0.340348842202129, -4.18342952961951,
9.91999265542052, -6.2673275997563, -6.19445525060979, -4.89432563084305,
13.3155996092707, -17.5672684031691, -11.0570104414685, -10.1512940397755,
-2.83422832939901, 11.2933772476208, 18.438427176494, -10.6101681685668,
2.08048057352476, 5.50745324586983, -0.13719717270706, -8.9884667602456,
-2.05928444313446, -0.305773393272364, 4.86554463593651, 3.76285186780856,
6.3828926870771, -9.3655246100571, 7.43651870615979, -1.67082019758757,
10.9895599029683, -13.9919213196391, -7.47010213808747, -5.18316808251628,
3.09935494483666, 0.0636557688816208, -1.54858827353552, 5.76445544260896,
-7.0244817763404, -6.91485718432494, 6.12343812013661, 16.0347161023267,
-6.04452306419056, 0.503323158087318, 1.11905480618314, -3.32310396567328,
3.1274151532053, -1.0139189902977, -2.77210893619864, 5.73300675328722,
-2.20016077060467, 6.88733384115661, 9.29615931028018, 7.89283806484175,
-0.0231784873612931, -13.6472361241707, 8.57047305874539, 9.82836851225084,
-12.0042402273719, -9.50514572716062, -7.91330903314343, 8.52172081325052,
-3.03276814511544, -8.49580406093405, -2.33842120098311, -0.32804627167184,
-3.21259723306479, 11.1229701308803, 0.598883041283415, 5.30460923308376,
1.00174438392002, -1.97289932239362, -10.2991918599681, -3.87835358264123,
-2.25659286308468, -0.166946108131337, -4.78949081440092, 3.19183354598423,
4.8383258847991, 0.890821633270318, 14.2375666218091, -6.1498396420083,
0.00274092818159516, 2.69868755607018, -6.72994548395586, -2.87563409646143,
6.39162216555694, -1.86430696261611, 1.49349337044142, -0.729377873822674,
3.91976580406387, -4.55169784656593, 4.01470477896383, 9.45815735561592,
3.87143750044459, 2.8485319829077, -3.54554995799273, 2.1592779575771,
-10.2265407210419, -2.57734842434015, 3.85728531842232, 3.91861430861892,
-6.08938026261039, 2.90775931244037, -5.65086822564584, -7.43442226552497,
-0.28834246695089, 4.46777893840091, 1.61867182502613, -1.24379496084697,
3.09330484415506, 4.6129428255061, 4.21327794340158, -2.73480960596815,
-1.34501116390485, -4.95417459779772, -1.50853746838879, 4.27315750243976,
5.82184171959861, -3.417633941994, 9.09574374372561, -2.51371486672449,
-5.3839624715871, 4.02549123440248, -4.25205519103745, -6.14575157593085,
3.39653326861517, 2.02195972923006, 0.737129651195039, -3.82060608464456,
-5.8916049884138, -1.48103866101421, -4.15462594283813, 2.85762680978641,
8.01848063520863, -0.33318325779775, -4.11692676832453, -3.14609004733945,
-3.87716607273847, 2.15863837910027, 5.89633795035078, 6.78408908767204,
5.31335299395838, 2.26271596099398, -6.04004269690954, 0.66943773718038,
-7.25501768327073, -1.85352333808763, 0.881321761644734, -3.37300816578494,
1.72336449307423, -1.12738959732561, -1.00315691989581, 1.22856047955961,
-5.49078567616185, 7.36566150881169, 1.5098504796481, -4.40047569826546,
1.27392003732061, -1.32431052177641, -3.49056795319117, -1.81317399652518,
4.21470358856118, -3.35477920804357, 0.76847614798403, 4.53289945581295,
1.08860795433414, 2.64380516830634, -3.5282442958868, 0.44695028657078,
6.40236529107031, 1.40990912221294, -0.646731159533322, -0.886819655598922,
-0.75355529529178, -3.22681748481868, 1.14443040171423, -1.01721629940821,
-1.91406441412825, 2.79795257160194, 1.42125707801834, -3.78513731974819,
5.22015934186783, -3.11038146921718, 4.2502371748613, -2.23573426796635,
1.2890326250481, -3.2044887828359, -0.0731034485321765, 1.00689042463065,
3.10770789167095, 0.653717111815948, 2.0046773828616, -5.05502285926648,
0.670269628945531, -0.0735066911646989, 0.567378074513561, -4.12216401435608,
-1.34361612453716, -2.31031886740573, 4.81653041064168, 0.0225272846463933,
-2.29390683891735, -3.34883149458521, 1.62567523985947, 1.46413728406672,
2.19861127383831, -2.2813563892377, 1.48246659900362, 1.20813467297598,
4.19016124903691, -3.8965814643401, 3.56184752348759, -1.3878373295987,
1.21458977444305, -0.654562661488364, 0.95632241225039, -1.55232118983605,
-2.44238882925152, 1.13264815277383, -1.78862294430386, 0.991256365490394,
-3.88951966260596, 0.202107196565979, 0.551090451321218, 2.66348016156913,
-0.854309938495967, -0.232716071672556, 0.851674118269924, -0.985653498045505,
1.36933618443016, -0.586856938122795, -4.1119301713715, 1.0178280457984,
0.338189042904915, -3.94726389810293, 1.81377558126379, 1.58372328030388,
1.72308989346244, 2.19193280022478, -1.61256462530986, 1.96795000825428,
-0.618527965915178, 0.0756192601567935, -0.659358025397941, 0.0429550715360451,
-0.381324238450748, -0.652905276182954, 0.170764900323459, -0.641706794178386,
-0.0221222785810836, 2.30873232738461, 1.27649223277723, 0.0239704300112933,
1.61258498499148, -2.05589657580369, 0.0265571467877399, 1.25029287962319,
-0.571431828720408, -1.73619374916576, -0.0382088144200337, -0.321239936961026,
-0.611330806517872, -2.02090803294801, 3.14396363436869, -1.10262030332252,
-1.04168768271449, 0.37429480503427, 0.104514606536275, -0.478767952336214,
-0.192546893385304, 2.1627661920923, 0.195395664072845, -0.782469508379251,
2.08546244595102, -0.0517755897340584, -2.39907743930418, -0.211807017194547,
1.71381905614487, 0.83072521628824, -1.60469551897193, 0.245800797687136,
-1.46819802906202, 0.849407708022543, 0.917523253251796, -1.60319562494994,
1.89391093257302, 0.378604048008997, 0.14292959825461, -0.126413703865446,
-1.54645185235878, -0.749700454486876, -0.0658077625468692, 1.55140944467166,
0.302322289091245, 0.458890126236888, -0.788003883776506, -1.8522098865949,
-0.303600234099745, -0.869244048674518, 2.09285131170145, 1.13594169034256,
0.00257581110260907, 1.28821287823238, -0.603719944771219, -0.0929817189825319,
0.646083878133434, -0.997976432253114, -0.0887921104756663, -0.252335674196718,
0.885171983511843, -0.455664633944459, -0.080944547822266, -1.11153038212543,
0.684953529364659, 0.00107091275654292, -0.0523472212020604,
-0.722665903230519, 0.112695429436832, -0.431332703576418, 2.28632079906497,
0.389976510624337, -0.672770348738164, -0.710872748519771, 1.11097033239239,
-0.117525841496901, -0.38659967304807, -0.311519238694312, -0.58769206070627,
0.880912821150173, -0.223180949008615, -0.0563287542495416, 0.677577799103385,
0.174786309548343, -0.486054451272286, -0.875421020334039, -1.99840144432528e-15,
-2.19269047363468e-15, -2.80331313717852e-15, 3.39658856596259e-15,
4.44089209850063e-16, 4.44089209850063e-16, -2.44249065417534e-15,
1.11022302462516e-15, -6.66133814775094e-16, -4.44089209850063e-15,
-3.5527136788005e-15, -2.33146835171283e-15, -6.43929354282591e-15,
2.44249065417534e-15, -1.4432899320127e-15, -2.19269047363468e-15,
1.0547118733939e-15, -1.11022302462516e-15, -9.99200722162641e-16,
1.33226762955019e-15, -1.55431223447522e-15), .Dim = c(21L, 21L
), .Dimnames = list(c("EDA_01", "EDA_02", "EDA_03", "EDA_04",
"EDA_05", "EDA_06", "EDA_07", "EDA_08", "EDA_09", "NDA_01", "NDA_02",
"NDA_03", "NDA_04", "NDA_05", "NDA_06", "NDA_08", "NDA_09", "NDA_10",
"NDA_11", "NDA_12", "NDA_13"), c("PC1", "PC2", "PC3", "PC4",
"PC5", "PC6", "PC7", "PC8", "PC9", "PC10", "PC11", "PC12", "PC13",
"PC14", "PC15", "PC16", "PC17", "PC18", "PC19", "PC20", "PC21"
)))), class = "prcomp")
Here is the minimal code to reproduce this problem:
library(factoextra)
library(tidyverse)
# create a named factor for coding the coloration of the variables in the plot
markers <- facto_summarize(pca,
element = "var",
result = "contrib",
axes = c(1, 2)) %>%
mutate(candidate = ifelse(name == "TU35976" | name == "TU18317" | name == "TU12311" | name == "TU3565" | name == "TU9890" | name == "TU18316", "1", "0")) %>%
select(name, candidate)
markers_vec <- as.factor(markers$candidate)
names(markers_vec) <- markers$name
(var_cluster_PC12 <- fviz_pca_var(pca,
col.var = markers_vec,
axes = c(1,2),
select.var = list(contrib = 200),
palette = c(
"grey90",
"black"),
label = "none",
) +
theme_bw()
)
Which produces this plot:
This image doesn't quite do it justice, so here is a plot of the full data set that shows how bad the overlap is:
Probably the simplest method, given your particular colour scheme, is to make the arrows all black but use the alpha channel to make the gray ones gray. This means the black arrows will still be completely black even if other arrows are drawn over the top:
fviz_pca_var(pca,
axes = c(1,2),
select.var = list(contrib = 200),
alpha.var = ifelse(markers_vec == 0, 0.2, 1),
label = "none") +
theme_bw()

Scatterplot/volcano plot as an output from t.test

It is my data:
> dput(data)
structure(list(foldchange = c(-0.17853057272962, 3.60013440830337,
0.648944710423407, 1.38528656859267, 2.38882890772698, 1.91371568283765,
1.77591931363495, -1.51447851175922, 3.1416903855924, 1.51711016957237,
3.14707703341916, -1.44751697381751, 1.23658565660726, -0.512829478520189,
1.68928069854351, 2.07214007434345, 1.24799276690488, 6.25149659558487,
6.35918877435554, 5.86088034655694, 6.38890659730165, 5.05510489389194,
4.62060389613534, 3.75508710774868, 4.18575763169519, 5.31627264153051,
5.87091236649665, 6.71464565321037, 5.24000610137973, 4.25821377851955,
7.32277714374523, 3.1963295806222, 7.26249808789293, 4.44427454088613,
6.21495395454133, 8.74469985969472, 7.49982946564144, 4.45020943795387,
5.66199031471621, 5.29959827685333, 8.65819317196484, 5.86664903755707,
4.5740575604176, 8.24504501687473, 5.7916074097308, 4.18199181353134,
6.73956641707995, 4.60357435173805, 5.9205153184753, 3.65014593638562,
3.25607795403669, 5.56919529940933, 5.76811109641351, 6.10600807588152,
5.69234974521511, 5.5102283323841, 4.71232921328194, 6.55727667796477,
6.19995053763513, 4.64209842048131, 2.29238227264409, 6.79465189260383,
7.51968952300944, 7.81695579226993, 6.29926703626301, 4.64687557749141,
-2.44220257171186, 5.33199370895397, 5.18820654974805, 5.03498241997507,
6.29395095024283, 6.27602377186869, 6.78363927671209, 2.93759015053983,
6.65061604346668, 5.671080311536, 8.45199131823131, 5.79230415012306,
6.3270025568739, 7.0934690916107, 3.53800869528685, 3.08683779646569,
6.82111375813946, 3.02729078403818, 5.36024214796805, 4.04778690916444,
5.74765756930797, 4.10788604670319, 6.39978058654016, 5.7746717387066,
2.9247167920294, 7.54315906042106, 4.2742172444481, 6.61121261965006,
3.77012175922873, 4.94407566887151, 7.93185716981795, 7.05304621480995,
5.59261760605766, 5.42381827536197, 4.22645498896606, 5.15806113482742,
4.15403623593809, 6.40153592433128, 7.38902001442131, 2.72654942454391,
3.28741231093207, 3.79334363176751, 5.86527050546341, 5.10320299162235,
3.99883612485974, 6.45475273104195, 4.85567883821983, 6.55055641729645,
5.03746875764267, 7.27660375171087, 3.30817125205364, 5.23766518187252,
7.6588755830143, 3.53552741086444, 5.66455197986778, 8.40623211540503,
3.93151438658523, 4.26875667827774, 4.38704995079332, 6.75232207417316,
3.76563594385214, 6.08008097541859, 2.40905905886796, 5.50981339395085,
5.78780269825563, 0.2322416329745, 5.69410860233132, 4.94656296117567,
4.20594226169741, 4.50293112094816, 6.07430576125864, 3.67684848946483,
5.825851099141, 5.22439201628482, 7.72829018644622, 5.24910611944979,
4.01783420322782, 6.3888069709767, 5.26066649741256, 4.81678726754752,
6.5683773907454, 4.86957242886115, 6.76705114368644, 4.45769029291236,
7.77607596853254, 6.85213457577069, 1.40150885676552, 5.43409652313493,
3.21738153172066, 6.23015085020594, 5.50091556711613, 3.99948543388746,
5.85816098688073, 4.33775608630599, 5.91715214825299, 5.45674826103132,
7.66790792082782, 6.63325838131012, 3.89631178894691, 2.38526575667126,
1.58661549426288, 4.76626341270591, 6.73426272316295, 5.54006035262931,
4.07996836453406, 7.12087390022358, 6.96007461543701, 5.68202490906633,
6.58504044389069, 5.41036820315057, 6.61076809589319, 1.23772469006557,
4.1661166499875, 2.94059625298825, 8.38336956160413, 4.84906289871508,
4.93787691221829, 6.82379835301371, 5.82520798412864, 4.87582657907206,
5.36621724700676, 8.91922991774938, 3.49025109999629, 5.1232073414505,
4.27193651596412, 5.07417945071012, 3.61930149745523, 2.7469092502892,
6.67162003616042, 4.86698118654996, 7.53876919017093, 4.58878989189686,
5.78956520376246, 3.98567045767003, 8.14934433289609, 6.88879716040936,
7.00251456012974, 5.05095662412332, 6.39777439550296, 7.96268799093557,
4.82826575143863, 8.31032763539508, 4.74493707321909, 6.8929416113222,
6.84202549278968, 8.20121430968127, 2.91031632522241, 4.86190488550545,
5.5516465446887, 7.74996457744065, 2.25505738807845, 5.71069872298306,
1.97493599527532, 5.60445326341706, 6.39297603198736, 7.16298115056911,
4.52688105225386, 6.46061751569601, 4.78104064111529, 2.84526825975018,
6.5537923066898, 6.98258253798747, 5.0967396817644, 6.64593966293456,
4.8990397150507, 4.59878411928317, 5.55158425631398, 2.1065660739172,
2.40396884881286, 6.45421536580342, 5.98567305090568, 6.48593538806214,
7.41313242816247, 5.99024340460149, 5.63812101302136, -1.43793573368627
), all_pvalue = c(0.818887590433193, 0.00892139546812015, 0.434133425685163,
0.0536266013313456, 0.0450933986128537, 0.0367856407800243, 0.0941222253709068,
0.213526299326008, 0.00855327289085924, 0.0449444491492238, 0.00465098209958804,
0.0369667514121697, 0.0910501610463896, 0.378892060498093, 0.0595757077704777,
0.031626850730261, 0.0878934608628569, 0.0124722939899662, 0.0249040599008334,
0.0150448394759397, 0.0104024068916351, 0.0340577599419123, 0.0244947271472485,
0.122485247246688, 0.0211309039009709, 0.0201043058824927, 0.0152779076456381,
0.00938733157248341, 0.013807428170544, 0.01948348499862, 0.0119978631408916,
0.0997968486684091, 0.00504808432168479, 0.041226720485986, 0.0127407583225205,
0.00709338243276709, 0.00383605674079435, 0.0399854589187244,
0.0163222001450531, 0.00909800553027099, 4.81144191594885e-06,
0.0105231068132293, 0.0377428014886314, 0.00709059633291303,
0.00743882656849872, 0.0696633906261403, 0.00568508439144595,
0.0142206230933159, 0.0183625193075117, 0.0818974933908099, 0.0609423408539195,
0.00581572852382799, 0.00603085345605447, 0.00684099077236254,
0.0194814381299995, 0.0325641567545152, 0.0404062983698557, 0.00626431765905907,
0.00287654691487974, 0.0183318967557602, 0.202860908663261, 0.0033395497287839,
0.00804091896430431, 0.00524635934550195, 0.0100089274728679,
0.00470611875383887, 0.361169323059008, 3.90129727113067e-06,
0.0205225005371219, 0.0120500045076898, 0.00732055098038156,
0.0229916087025324, 0.00544774481324614, 0.108252753362848, 0.00911923198666818,
0.0122812312739145, 0.00343585528287351, 2.06257208918569e-05,
0.000904993210532763, 0.0015065294739414, 0.102118204143709,
0.118350948568527, 0.0136202759386966, 0.15251012082679, 0.0428316882385798,
0.0752744217284719, 0.00632986043900174, 0.0269567937932686,
0.00707537967267082, 0.0149356605279715, 0.163005190656644, 0.00929612911973378,
0.0196453775259569, 0.0133262667903121, 0.114115405959882, 0.0189109801950218,
0.00590387539250432, 0.00802692325541374, 0.0184346327727756,
0.00104714399950925, 0.0554774130259537, 0.0084910975380844,
0.0349856904843115, 0.0124547169142572, 8.60050865459788e-07,
0.1188515828269, 0.133720711339729, 0.0641704698591151, 0.0075124796175742,
3.94432669779951e-07, 0.0740343932996142, 0.00160775849728933,
6.61326355967731e-06, 0.0106610228625055, 0.00539717052083514,
0.00492255859958016, 0.122531121480312, 0.0159768620962635, 0.00365780586610517,
0.0135086464724098, 0.0256265865459836, 0.00391215568816396,
0.0271580638871089, 0.0512876942387616, 0.0135566028247977, 0.0169110062500104,
0.0848247460082605, 0.0158705161056627, 0.176345767878009, 0.0124719098589431,
0.0152388258990332, 0.866066700538701, 0.042979313425954, 0.0160125031962862,
0.0441592105265668, 0.0124108545467876, 1.80875437447348e-06,
0.0704671677844812, 0.0111351361909711, 1.62883074487165e-07,
0.000694779973318456, 0.0120079549431507, 0.0196183531679123,
0.0100186493225724, 0.0173294242221405, 0.0573614373022037, 1.30612522568528e-07,
0.023898721968545, 0.00665918028588502, 0.06907634259105, 0.00518730999717143,
0.0109717740506543, 0.394662670743417, 0.0165847750353483, 0.0593829446004973,
0.000113216713641592, 0.0222583368635018, 0.0694462386106761,
0.00437955933335859, 0.0631677226779205, 0.00649674049335009,
1.10699021652115e-07, 0.00543997929535101, 0.00292890932795308,
0.021574456097881, 0.17997075681454, 0.279770535484078, 0.050945514039484,
0.0102923410906512, 0.0103389721465925, 0.112227938286441, 0.00398090342551613,
0.0428676019413789, 0.00337313863923396, 0.0092822848687081,
0.00778850900332348, 0.00146877357609246, 0.448950342618974,
0.0801294647165026, 0.0797698230881222, 3.72112644308374e-05,
0.00728653989704988, 0.0320006421510141, 0.000420503611946363,
0.0207086037412542, 0.0448889114898146, 6.94555482668648e-07,
0.000135994029220134, 0.0982385638169219, 0.0130399195487442,
0.0297056523919866, 0.0208455457844783, 0.104595177359326, 0.100684824982166,
0.00110472016462074, 0.0664329210478157, 0.0140428240948167,
0.0468861767036331, 0.0102813794498838, 0.0693803856754811, 0.00566014993761021,
0.00275831473628789, 0.00234550137829788, 0.0116252548991317,
0.0143153463606759, 0.00995352784254985, 0.00238257560505637,
0.00182950280683248, 0.013195116994233, 0.000640921917352429,
0.000171523469251389, 0.0087256530793244, 0.0989386901919075,
0.0321103798387662, 0.0222773975090858, 0.007943310795799, 0.171053950985746,
0.012874269835152, 0.38817395138115, 0.00787508757030877, 0.0114487159712535,
0.0187831808209386, 0.0452479566115196, 0.00640720682677851,
0.0315482155790946, 0.125132833439637, 0.0115284490664364, 0.00538397509568388,
0.00517772137814985, 0.00176762574966497, 0.0273122011845722,
0.0191341545126795, 0.0235413416908084, 0.270326642321866, 0.179926862630332,
0.008734949388329, 0.0138918131322944, 0.00507817315892406, 0.000173786133243839,
0.00225184544628237, 0.0181059516295825, 0.541544494598043),
probename = c("Mark_1", "Mark_2", "Mark_3", "Mark_4", "Mark_5",
"Mark_6", "Mark_7", "Mark_8", "Mark_9", "Mark_10", "Mark_11",
"Mark_12", "Mark_13", "Mark_14", "Mark_15", "Mark_16", "Mark_17",
"Mark_18", "Mark_19", "Mark_20", "Mark_21", "Mark_22", "Mark_23",
"Mark_24", "Mark_25", "Mark_26", "Mark_27", "Mark_28", "Mark_29",
"Mark_30", "Mark_31", "Mark_32", "Mark_33", "Mark_34", "Mark_35",
"Mark_36", "Mark_37", "Mark_38", "Mark_39", "Mark_40", "Mark_41",
"Mark_42", "Mark_43", "Mark_44", "Mark_45", "Mark_46", "Mark_47",
"Mark_48", "Mark_49", "Mark_50", "Mark_51", "Mark_52", "Mark_53",
"Mark_54", "Mark_55", "Mark_56", "Mark_57", "Mark_58", "Mark_59",
"Mark_60", "Mark_61", "Mark_62", "Mark_63", "Mark_64", "Mark_65",
"Mark_66", "Mark_67", "Mark_68", "Mark_69", "Mark_70", "Mark_71",
"Mark_72", "Mark_73", "Mark_74", "Mark_75", "Mark_76", "Mark_77",
"Mark_78", "Mark_79", "Mark_80", "Mark_81", "Mark_82", "Mark_83",
"Mark_84", "Mark_85", "Mark_86", "Mark_87", "Mark_88", "Mark_89",
"Mark_90", "Mark_91", "Mark_92", "Mark_93", "Mark_94", "Mark_95",
"Mark_96", "Mark_97", "Mark_98", "Mark_99", "Mark_100", "Mark_101",
"Mark_102", "Mark_103", "Mark_104", "Mark_105", "Mark_106",
"Mark_107", "Mark_108", "Mark_109", "Mark_110", "Mark_111",
"Mark_112", "Mark_113", "Mark_114", "Mark_115", "Mark_116",
"Mark_117", "Mark_118", "Mark_119", "Mark_120", "Mark_121",
"Mark_122", "Mark_123", "Mark_124", "Mark_125", "Mark_126",
"Mark_127", "Mark_128", "Mark_129", "Mark_130", "Mark_131",
"Mark_132", "Mark_133", "Mark_134", "Mark_135", "Mark_136",
"Mark_137", "Mark_138", "Mark_139", "Mark_140", "Mark_141",
"Mark_142", "Mark_143", "Mark_144", "Mark_145", "Mark_146",
"Mark_147", "Mark_148", "Mark_149", "Mark_150", "Mark_151",
"Mark_152", "Mark_153", "Mark_154", "Mark_155", "Mark_156",
"Mark_157", "Mark_158", "Mark_159", "Mark_160", "Mark_161",
"Mark_162", "Mark_163", "Mark_164", "Mark_165", "Mark_166",
"Mark_167", "Mark_168", "Mark_169", "Mark_170", "Mark_171",
"Mark_172", "Mark_173", "Mark_174", "Mark_175", "Mark_176",
"Mark_177", "Mark_178", "Mark_179", "Mark_180", "Mark_181",
"Mark_182", "Mark_183", "Mark_184", "Mark_185", "Mark_186",
"Mark_187", "Mark_188", "Mark_189", "Mark_190", "Mark_191",
"Mark_192", "Mark_193", "Mark_194", "Mark_195", "Mark_196",
"Mark_197", "Mark_198", "Mark_199", "Mark_200", "Mark_201",
"Mark_202", "Mark_203", "Mark_204", "Mark_205", "Mark_206",
"Mark_207", "Mark_208", "Mark_209", "Mark_210", "Mark_211",
"Mark_212", "Mark_213", "Mark_214", "Mark_215", "Mark_216",
"Mark_217", "Mark_218", "Mark_219", "Mark_220", "Mark_221",
"Mark_222", "Mark_223", "Mark_224", "Mark_225", "Mark_226",
"Mark_227", "Mark_228", "Mark_229", "Mark_230", "Mark_231",
"Mark_232", "Mark_233", "Mark_234", "Mark_235", "Mark_236",
"Mark_237", "Mark_238", "Mark_239", "Mark_240", "Mark_241",
"Mark_242")), row.names = c("Mark_1", "Mark_2", "Mark_3",
"Mark_4", "Mark_5", "Mark_6", "Mark_7", "Mark_8", "Mark_9", "Mark_10",
"Mark_11", "Mark_12", "Mark_13", "Mark_14", "Mark_15", "Mark_16",
"Mark_17", "Mark_18", "Mark_19", "Mark_20", "Mark_21", "Mark_22",
"Mark_23", "Mark_24", "Mark_25", "Mark_26", "Mark_27", "Mark_28",
"Mark_29", "Mark_30", "Mark_31", "Mark_32", "Mark_33", "Mark_34",
"Mark_35", "Mark_36", "Mark_37", "Mark_38", "Mark_39", "Mark_40",
"Mark_41", "Mark_42", "Mark_43", "Mark_44", "Mark_45", "Mark_46",
"Mark_47", "Mark_48", "Mark_49", "Mark_50", "Mark_51", "Mark_52",
"Mark_53", "Mark_54", "Mark_55", "Mark_56", "Mark_57", "Mark_58",
"Mark_59", "Mark_60", "Mark_61", "Mark_62", "Mark_63", "Mark_64",
"Mark_65", "Mark_66", "Mark_67", "Mark_68", "Mark_69", "Mark_70",
"Mark_71", "Mark_72", "Mark_73", "Mark_74", "Mark_75", "Mark_76",
"Mark_77", "Mark_78", "Mark_79", "Mark_80", "Mark_81", "Mark_82",
"Mark_83", "Mark_84", "Mark_85", "Mark_86", "Mark_87", "Mark_88",
"Mark_89", "Mark_90", "Mark_91", "Mark_92", "Mark_93", "Mark_94",
"Mark_95", "Mark_96", "Mark_97", "Mark_98", "Mark_99", "Mark_100",
"Mark_101", "Mark_102", "Mark_103", "Mark_104", "Mark_105", "Mark_106",
"Mark_107", "Mark_108", "Mark_109", "Mark_110", "Mark_111", "Mark_112",
"Mark_113", "Mark_114", "Mark_115", "Mark_116", "Mark_117", "Mark_118",
"Mark_119", "Mark_120", "Mark_121", "Mark_122", "Mark_123", "Mark_124",
"Mark_125", "Mark_126", "Mark_127", "Mark_128", "Mark_129", "Mark_130",
"Mark_131", "Mark_132", "Mark_133", "Mark_134", "Mark_135", "Mark_136",
"Mark_137", "Mark_138", "Mark_139", "Mark_140", "Mark_141", "Mark_142",
"Mark_143", "Mark_144", "Mark_145", "Mark_146", "Mark_147", "Mark_148",
"Mark_149", "Mark_150", "Mark_151", "Mark_152", "Mark_153", "Mark_154",
"Mark_155", "Mark_156", "Mark_157", "Mark_158", "Mark_159", "Mark_160",
"Mark_161", "Mark_162", "Mark_163", "Mark_164", "Mark_165", "Mark_166",
"Mark_167", "Mark_168", "Mark_169", "Mark_170", "Mark_171", "Mark_172",
"Mark_173", "Mark_174", "Mark_175", "Mark_176", "Mark_177", "Mark_178",
"Mark_179", "Mark_180", "Mark_181", "Mark_182", "Mark_183", "Mark_184",
"Mark_185", "Mark_186", "Mark_187", "Mark_188", "Mark_189", "Mark_190",
"Mark_191", "Mark_192", "Mark_193", "Mark_194", "Mark_195", "Mark_196",
"Mark_197", "Mark_198", "Mark_199", "Mark_200", "Mark_201", "Mark_202",
"Mark_203", "Mark_204", "Mark_205", "Mark_206", "Mark_207", "Mark_208",
"Mark_209", "Mark_210", "Mark_211", "Mark_212", "Mark_213", "Mark_214",
"Mark_215", "Mark_216", "Mark_217", "Mark_218", "Mark_219", "Mark_220",
"Mark_221", "Mark_222", "Mark_223", "Mark_224", "Mark_225", "Mark_226",
"Mark_227", "Mark_228", "Mark_229", "Mark_230", "Mark_231", "Mark_232",
"Mark_233", "Mark_234", "Mark_235", "Mark_236", "Mark_237", "Mark_238",
"Mark_239", "Mark_240", "Mark_241", "Mark_242"), class = "data.frame")
I would like to create a nice graph (publication wise) to represent a data stored in this data frame. In general I would like to create a scatterplot or volcano plot with colors/shapes indicating what is important in my data.
I would like to achieve something like that:
Or:
As a filter cutoff we can start with: foldchange > 4 & all_pvalue < 0.05. I would like to also have a possibility to highlight (different color/shape and with a label) only couple of rows. Lets say as a starting point I would like to highlight Mark_23 and Mark_65.
Is it doable in R ? I have already tried something with volcano plot:
volcano = ggplot(data = data, aes(x = foldchange, y = -1*log10(all_pvalue)))
volcano + geom_point()
Can someone help me with going further ?
Here is an attempt to show what's possible
data$zones := interaction(abs(data$foldchange)>4,data$all_pvalue<0.05)
library(ggplot2)
library(ggrepel)
ggplot(data = data, aes(x = foldchange, y = -1*log10(all_pvalue),color = zones))+
geom_point()+
theme_light()+
geom_label_repel(aes(label=ifelse(probename %in% c("Mark_23","Mark_65"),as.character(probename),"")),
box.padding = 0.35,
point.padding = 0.5,
segment.color = 'grey50',show.legend = FALSE) +
geom_hline(yintercept = -1*log10(0.05),linetype = "dashed")+
geom_vline(xintercept = 4,linetype = "dashed")+
scale_color_manual(labels = c("bad", "also bad","good","not sure"), values = c("gray50","green","blue", "red"))+
labs(x = "fold change",
y = expression(log[10](p)),
color = "meaning")
there are many things here. geom_label_repel from library(ggrepel) allow to link your few points you want to show with a label. The easiest way of having the different colours is to create a variable that says in what zone you are. That is what I did with the zones variable, that you use in colour to have a changing color. You can manualy change it with the scale_color_manual function.
In the labs function I used the expression function that allows you to do superscripts. You can of course change the theme including, margins, axis, text size etc.
You can for example add theme(legend.position="top") to your plot to have the legend on top.

Shade Area using Polygon in R

This is my data:
data=structure(c(-0.0471957808923093, -0.0467988635384236, -0.0463342530558707,
-0.0457926330882236, -0.0451638382532273, -0.0444368569264807,
-0.0435998514313549, -0.0426401983500639, -0.0415445516030975,
-0.0402989307802484, -0.0388888369350307, -0.0372993976665075,
-0.0355155428026053, -0.0335222113649458, -0.0313045897396848,
-0.028848380109615, -0.026140097233332, -0.023167390606974, -0.0199193879382963,
-0.0163870547327459, -0.0125635636730607, -0.00844466640840535,
-0.00402905939999997, 0.000681265356828354, 0.00568069455370951,
0.0109597029612447, 0.01650460835736, 0.0222973821907433, 0.0283155256820092,
0.0345320202245192, 0.0409153597579977, 0.0474296712607441, 0.0540349276635842,
0.0606872553679083, 0.0673393362019039, 0.0739409011365642, 0.0804393104801724,
0.0867802126590783, 0.0929082711616744, 0.0987679468617816, 0.104304320836165,
0.109463941032544, 0.114195674804447, 0.11845154847061, 0.122187554726899,
0.125364408967776, 0.127948236372159, 0.12991117296429, 0.131231865742443,
0.131895859325063, 0.131895859325063, 0.131231865742443, 0.12991117296429,
0.127948236372159, 0.125364408967776, 0.122187554726899, 0.11845154847061,
0.114195674804447, 0.109463941032544, 0.104304320836165, 0.0987679468617816,
0.0929082711616744, 0.0867802126590783, 0.0804393104801724, 0.0739409011365642,
0.0673393362019039, 0.0606872553679083, 0.0540349276635841, 0.0474296712607441,
0.0409153597579976, 0.0345320202245192, 0.0283155256820092, 0.0222973821907433,
0.01650460835736, 0.0109597029612447, 0.00568069455370951, 0.000681265356828344,
-0.00402905939999997, -0.00844466640840537, -0.0125635636730607,
-0.0163870547327459, -0.0199193879382963, -0.023167390606974,
-0.026140097233332, -0.028848380109615, -0.0313045897396848,
-0.0335222113649458, -0.0355155428026053, -0.0372993976665075,
-0.0388888369350306, -0.0402989307802484, -0.0415445516030976,
-0.0426401983500639, -0.0435998514313549, -0.0444368569264807,
-0.0451638382532273, -0.0457926330882236, -0.0463342530558707,
-0.0467988635384236, -0.0471957808923093, -169.075778315231,
-165.419424999945, -161.763071684659, -158.106718369373, -154.450365054087,
-150.794011738801, -147.137658423515, -143.481305108229, -139.824951792942,
-136.168598477656, -132.51224516237, -128.855891847084, -125.199538531798,
-121.543185216512, -117.886831901226, -114.23047858594, -110.574125270654,
-106.917771955368, -103.261418640081, -99.6050653247953, -95.9487120095092,
-92.2923586942231, -88.636005378937, -84.9796520636509, -81.3232987483648,
-77.6669454330787, -74.0105921177926, -70.3542388025065, -66.6978854872204,
-63.0415321719343, -59.3851788566482, -55.7288255413621, -52.072472226076,
-48.4161189107899, -44.7597655955038, -41.1034122802177, -37.4470589649316,
-33.7907056496455, -30.1343523343594, -26.4779990190733, -22.8216457037872,
-19.1652923885011, -15.508939073215, -11.8525857579289, -8.19623244264278,
-4.53987912735666, -0.88352581207058, 2.77282750321554, 6.42918081850162,
10.0855341337877, 13.7418874490738, 17.3982407643599, 21.054594079646,
24.7109473949321, 28.3673007102183, 32.0236540255043, 35.6800073407905,
39.3363606560765, 42.9927139713627, 46.6490672866487, 50.3054206019349,
53.9617739172209, 57.6181272325071, 61.2744805477931, 64.9308338630793,
68.5871871783654, 72.2435404936515, 75.8998938089376, 79.5562471242237,
83.2126004395098, 86.8689537547959, 90.525307070082, 94.1816603853681,
97.8380137006542, 101.49436701594, 105.150720331226, 108.807073646513,
112.463426961799, 116.119780277085, 119.776133592371, 123.432486907657,
127.088840222943, 130.745193538229, 134.401546853515, 138.057900168801,
141.714253484087, 145.370606799374, 149.02696011466, 152.683313429946,
156.339666745232, 159.996020060518, 163.652373375804, 167.30872669109,
170.965080006376, 174.621433321662, 178.277786636948, 181.934139952235,
185.590493267521, 189.246846582807, 192.903199898093), .Dim = c(100L,
2L), .Dimnames = list(NULL, c("Y", "X")))
With this data I can plot this graph below:
plot(data[,1],data[,2],type="l")
abline(h=quantile(data[,2],0.75))
abline(h=quantile(data[,2],0.25))
Using polygon function how can I shade the area (with different colors) between the distribution tails and 0.75/0.25 quantiles lines?
An answer:
Let dat be your data frame,
oo <- dat[24:76, ];
oo[1, ] <- c(dat[1,1], dat[25,2]);
oo[, nrow(oo)] <- c(dat[100,1], dat[75,2]);
plot(dat, type = "l"); polygon(oo, col = "gray")

Plotting Curves from Data Frame Columns

i am facing a problem in plot ols estimations in a scatterplot:
I have this data frame: With 9 columns and 99 rows:
structure(list(Y = c(-0.145442175, 0.291096141, 0.489923112,
-2.038363166, 1.180430664, 0.188114666, 0.850922634, 1.172142766,
-3.980837975, 0.285762444, 2.497040646, 0.658010994, -0.925171981,
0.37076995, -1.108211119, -0.409242669, -1.234583525, -0.385841816,
0.016744771, -0.584406288, 1.17224811, -0.746804388, -0.625028046,
0.257871468, -2.735845346, 2.619304857, -0.406825232, 0.323665151,
2.218951363, -0.821029648, -0.872854889, -2.663306158, -0.121976044,
0.881566376, -1.972706678, -3.855576256, 2.927421113, 1.314753531,
0.234296206, 0.828464757, -0.909318569, 0.616134903, -0.567630403,
0.624571064, -0.414112923, 0.642200314, -0.309421266, 0.195312598,
-0.519988256, 0, 0.081070175, 0.032446432, -0.534025032, -0.426783307,
-0.38495511, -0.207900219, -1.953789746, -0.616924355, -0.783222881,
-1.935420969, 0.638445535, 1.080925923, -1.598076681, 0.25063631,
-0.697183766, 0.188971653, -0.415267389, -4.154506044, 1.163226552,
0.036569698, -0.547147074, 1.11937374, 0.383311682, -0.875037781,
-0.372684863, 0.306816004, -1.250561544, -1.042237738, -1.757788446,
0.021079982, 1.844023775, 1.674645753, -0.428546132, -0.527705597,
0.542202572, -0.621479123, -0.050415867, -0.122332943, 0.468553764,
0.216998274, 3.088480781, 0.434099931, 2.114916704, -2.407018936,
-0.127060127, 0.546756422, 0.263207486, 0.63453915, 0.76832746
), X = c(0.009476137, -0.0236354, 0.0094081, 0.11715252, 0.032324021,
0.0461193, 0.050794971, 0.032372819, 0.202121874, 0.390821859,
-0.124492596, -0.127305193, -0.22233597, -0.081113713, 0.09952616,
0.22494711, 0.226621495, 0.411607624, 0.089200478, -0.013454832,
-0.013547165, -0.232366214, 0.03140992, -0.026798837, -0.084556341,
-0.091993172, -0.303730207, -0.236679148, -0.284235285, -0.355253166,
-0.179645537, -0.01381843, -0.022950244, -0.050065976, -0.032018504,
-0.087168055, -0.081865767, -0.253991077, -0.242882759, -0.150225053,
-0.16596575, -0.156887247, -0.071795146, -0.100408802, -0.067307731,
0.024006869, -0.019250912, -0.02399429, 0.038421097, 0.062320065,
0.07187025, 0.024019462, 0.038421097, 0.033539309, 0.014351457,
-0.009575137, 0.014343968, 0.028561284, 0.0404213, 0.026065697,
-0.004700435, -0.072739794, -0.042217496, -0.05889531, -0.130522139,
-0.136291869, -0.120099035, -0.091418565, -0.122040844, -0.124609029,
-0.096255449, -0.190338762, -0.11611752, -0.055598423, -0.065293448,
-0.038746326, -0.029090518, -0.067627348, -0.082097445, -0.215845836,
-0.389993696, -0.264371785, -0.126530291, -0.111840985, -0.094952196,
-0.136700196, -0.190968195, -0.156564122, -0.181077278, -0.15381292,
-0.122020692, -0.107867301, -0.068642333, -0.034348677, -0.073289926,
-0.063314884, -0.092537576, -0.165375956, -0.15042398), Null = c(-0.036795117836493,
0.0120555676565338, -0.0366906491623935, -0.22323992930528, -0.0728300398338213,
-0.0955073599141197, -0.103350601084975, -0.0729090354522075,
-0.400153521158964, -0.887015257107641, 0.1362666683468, 0.13919994231771,
0.221388292373518, 0.087380368104602, -0.189831042487278, -0.452154909992189,
-0.456044210600938, -0.948567833126862, -0.170785020294756, -0.00253939338337472,
-0.00240533038312774, 0.228145471304061, -0.0713518661553421,
0.0165138860659871, 0.0915102566139487, 0.100284493544177, 0.265652059802101,
0.230938443729295, 0.257246215885006, 0.281209408151878, 0.188533028671265,
-0.00201164134414489, 0.0110851592192505, 0.0481858583559124,
0.0237904823161768, 0.094614581053392, 0.0882862377341187, 0.241468070168396,
0.234837060900023, 0.162029971029324, 0.176601607696189, 0.168307425791361,
0.0759851164110966, 0.109970788582389, 0.0703849242291975, -0.059492586621119,
0.00581616568295407, 0.0125631925046972, -0.0827672867080164,
-0.123023227393077, -0.139691063870559, -0.0595125909296922,
-0.0827672867080164, -0.074799966578053, -0.044324863847201,
-0.00820062690976645, -0.0443132308515717, -0.0667648997869916,
-0.0860567642206439, -0.0627706942069095, -0.0153914247452083,
0.0771546773236518, 0.0377224646820258, 0.0596889425617937, 0.1425196179012,
0.148379247725525, 0.13162698340227, 0.0996137276510431, 0.133686233062275,
0.136388667637584, 0.105222539655097, 0.197385328960716, 0.127361748973716,
0.0554268640818151, 0.0678473149754353, 0.0330232883757411, 0.0197208677278167,
0.0707862239701058, 0.0885648870712001, 0.216820906265572, 0.286245951224793,
0.247258814186372, 0.138394666330137, 0.122716205945161, 0.103719679674083,
0.148789344619283, 0.197893429730301, 0.168006688568371, 0.189742414352596,
0.165430712615822, 0.133664933948451, 0.11833998959919, 0.0720581343490991,
0.0270069004188009, 0.077834296346802, 0.0653403280475977, 0.100918894574441,
0.176071877748707, 0.162219750035618), OLS_1 = c(-2.97674658085357,
-2.95792547866683, -2.97674412477729, -2.7937460366665, -2.96913739819288,
-2.95639989365184, -2.95069150171007, -2.96910314906723, -2.3856485268894,
-0.647452287114872, -2.68293610049662, -2.670570393744, -2.10297963546522,
-2.84137496711892, -2.84927190111917, -2.23638642750757, -2.22477621905134,
-0.385841816000001, -2.87715002139054, -2.96747293407547, -2.96740133507642,
-2.02609643038743, -2.9697648045679, -2.95427875550959, -2.8310157181346,
-2.80733412921436, -1.38551048535346, -1.99204069101103, -1.57679230211392,
-0.821029648, -2.39395151432173, -2.96718943992586, -2.95867282134313,
-2.9175506236826, -2.94755679517459, -2.82290206987746, -2.83914454134393,
-1.84931168689084, -1.94200482386918, -2.56030139156351, -2.4747687889082,
-2.52507434784403, -2.86749990988846, -2.77838660436577, -2.87908253396987,
-2.97385415360498, -2.96244666805069, -2.95752797222193, -2.96426392038595,
-2.93361303993881, -2.91621877029975, -2.97384869333029, -2.96426392038595,
-2.96826157356433, -2.97653443074828, -2.97023260580068, -2.97653534550966,
-2.9715473503959, -2.96240424133875, -2.97289412424858, -2.9730125951007,
-2.86497897723402, -2.93188917574701, -2.89904800305061, -2.6561144854951,
-2.62935195635151, -2.70174255054932, -2.80922741244202, -2.69350740105694,
-2.68242924921473, -2.79295820376613, -2.32657978700299, -2.718248099245,
-2.90625073580661, -2.88407071600265, -2.93759776247538, -2.95143559806685,
-2.87827902655775, -2.83845377816351, -2.15100018436527, -0.392139380784325,
-1.7590965971582, -2.67400272569948, -2.73540774982849, -2.79741598960129,
-2.62741730304073, -2.322499279269, -2.52681590220219, -2.38514457172383,
-2.541507865502, -2.6935934995898, -2.75082409521646, -2.87570553083222,
-2.94427256930162, -2.86349763526591, -2.88884317216564, -2.80553055841713,
-2.47811758528604, -2.55927025907886), OLS_2 = c(-2.83865555876367,
-2.82203271957637, -2.83865550287755, -2.66277932892391, -2.83073328950317,
-2.8182826854432, -2.81275284604234, -2.83069942358793, -2.27571536741022,
-0.632851535784811, -2.56646067709365, -2.55491098827374, -2.02364579120999,
-2.71420058960775, -2.71564453925406, -2.13442002502496, -2.12343285482248,
-0.385841816, -2.74223576659719, -2.83068449367348, -2.83062014186059,
-1.95158880862936, -2.83135434505306, -2.81870405841395, -2.70456098525177,
-2.68251016192609, -1.35080974869909, -1.91966655284606, -1.53026524143009,
-0.821029648, -2.29619548286091, -2.83042962848176, -2.82271365766308,
-2.78489427206998, -2.81254809712918, -2.69700817487578, -2.71212546804251,
-1.78585373408616, -1.87276085874404, -2.45184700668681, -2.37183555552258,
-2.41889982491589, -2.73848954857785, -2.65553364194069, -2.74924637290594,
-2.8354502300085, -2.82614423798244, -2.82167034953476, -2.82594242161564,
-2.7962902949221, -2.77959589724382, -2.83544467118397, -2.82594242161564,
-2.82986834510621, -2.83829410413293, -2.83315419155684, -2.83829521382395,
-2.83312719078141, -2.82412509152621, -2.83447802392599, -2.83561001727694,
-2.73614728712302, -2.79813447119318, -2.76776591170989, -2.54140667394362,
-2.5163996858597, -2.58402223424852, -2.68427373122372, -2.57633280462435,
-2.56598731123967, -2.66911582708562, -2.23311605677819, -2.59943103595799,
-2.7744383205277, -2.75387620457868, -2.80339428073398, -2.81610308322424,
-2.74850042856033, -2.71148276169435, -2.06864445166113, -0.418358709691658,
-1.7012556906544, -2.558117011201, -2.61544592452239, -2.67326984561107,
-2.5145916492569, -2.22929491666958, -2.42052887445801, -2.28795076147412,
-2.43427089501948, -2.57641320261571, -2.62982944259216, -2.74611100908034,
-2.80953310903525, -2.73477077084888, -2.75830410348864, -2.68083005992821,
-2.37496906485549, -2.4508827380889), OLS_3 = c(-2.58083646581942,
-2.5683178338716, -2.58084089114316, -2.41826149362172, -2.57232965672457,
-2.56041470241702, -2.55521822468909, -2.57229650627193, -2.0704676472292,
-0.605591599496051, -2.34899840070827, -2.33897223601076, -1.87552769159633,
-2.47676312148376, -2.46615920192222, -1.94404642215785, -1.9342224786085,
-0.385841816000001, -2.49034777076914, -2.57529735049815, -2.57524652934739,
-1.81248137667339, -2.57293885513887, -2.56558300171966, -2.46846711008925,
-2.44946096338359, -1.28602268062379, -1.78454238349805, -1.4433981562183,
-0.821029648, -2.11368273887782, -2.57509593622485, -2.56887479307252,
-2.53722183306237, -2.56048377359198, -2.46196139684977, -2.47497795642607,
-1.66737628649693, -1.7434807939705, -2.24936019247138, -2.17965685727221,
-2.22066956504207, -2.49762425675709, -2.42616435450559, -2.50683929408026,
-2.57704694280319, -2.57166448720316, -2.56802106429762, -2.56769302344379,
-2.53990559282486, -2.52451787208599, -2.57704119998386, -2.56769302344379,
-2.57148502596854, -2.58019625622877, -2.57722566059429, -2.58019772985789,
-2.57469359055957, -2.56595475982599, -2.57605200249485, -2.57907626550515,
-2.49561557851369, -2.54841138215235, -2.52265924802504, -2.32724456926626,
-2.30551521644622, -2.36423571438323, -2.45098235381054, -2.35756515622,
-2.3485875529132, -2.43789928063234, -2.05861713726078, -2.37759686441414,
-2.52834152993493, -2.51080007744427, -2.55283331443161, -2.56343418632904,
-2.50620082129485, -2.47442497328161, -1.91488441727801, -0.467310795744689,
-1.59326539683083, -2.34175573481226, -2.39147445613669, -2.44148615865099,
-2.30394357612981, -2.05528024243402, -2.22208856552246, -2.10648769733616,
-2.23405702128991, -2.35763491117015, -2.40392966200837, -2.50415507637054,
-2.55797145858227, -2.49443477420494, -2.51458468009137, -2.44801138045477,
-2.18238842077399, -2.24852076027753), OLS_4 = c(-2.4289478285331,
-2.41681903415288, -2.42895104301202, -2.27867081965274, -2.4213161496905,
-2.41038194422522, -2.40559515788832, -2.42128586809391, -1.95522949388955,
-0.590647453749078, -2.21077815389366, -2.20138321248198, -1.76758669368012,
-2.33060054299992, -2.32313500877883, -1.83755181381677, -1.82840597739465,
-0.385841816, -2.34557046847711, -2.42346978407977, -2.42342111188123,
-1.70861264386732, -2.42187239429871, -2.41422413566286, -2.32281181955877,
-2.30497392699143, -1.21632553238408, -1.68248005204524, -1.36346128591018,
-0.781669317752002, -1.99042352676657, -2.42327691796255, -2.41734804581689,
-2.38744248609079, -2.40939495374384, -2.31670510436427, -2.32892438647688,
-1.57289978140148, -1.64407512538075, -2.11744278294415, -2.05217911016675,
-2.09057710272701, -2.35019495754122, -2.28311871426765, -2.35885543710246,
-2.42560672084754, -2.42000135641999, -2.4165372393818, -2.41707097497419,
-2.39145946177805, -2.3772271125231, -2.42560153071694, -2.41707097497419,
-2.420544236609, -2.42841594588832, -2.4253216199613, -2.42841722040367,
-2.42347169955882, -2.41547562547196, -2.42470587973943, -2.4271143253132,
-2.34830761179908, -2.39799094116799, -2.37373288731684, -2.19039487337143,
-2.17003793409615, -2.22505776553193, -2.30640152341961, -2.21880622042115,
-2.21039315621698, -2.294126428977, -1.93888869626962, -2.23758086989921,
-2.37908034073483, -2.36257901260101, -2.4021644964107, -2.41218787827608,
-2.35825527028976, -2.3284051877118, -1.80440438182757, -0.451087514089169,
-1.50359480720157, -2.20399138892979, -2.25058992243427, -2.29749148286179,
-2.16856567860513, -1.93576601076367, -2.09190575790345, -1.98368936201681,
-2.10311255036163, -2.21887159162467, -2.26226743315126, -2.35633238493592,
-2.407018936, -2.34719820268328, -2.36613768370737, -2.30361375259329,
-2.05473632620086, -2.11665669129059), OLS_5 = c(-2.2911912568638,
-2.28123967681215, -2.29119683586224, -2.14805590207021, -2.28325670505768,
-2.27261386268403, -2.268006850245, -2.28322682471889, -1.84560662105751,
-0.576090713535621, -2.0945064647732, -2.0859234999636, -1.68828464788266,
-2.20368547406672, -2.18986194988925, -1.73587625378362, -1.72735189094969,
-0.385841816, -2.21101101562234, -2.28700200417098, -2.28696051646281,
-1.63411554699496, -2.28380625780731, -2.27896044246845, -2.19661244829441,
-2.18039652225164, -1.18146437845759, -1.6101070827248, -1.31682391434599,
-0.781364138557704, -1.89278224977018, -2.28683751979873, -2.28170279433502,
-2.25507742887343, -2.27469315563211, -2.19106352335337, -2.20216376634672,
-1.50940418054145, -1.57481865165838, -2.00915316980509, -1.94938461398854,
-1.98455653642811, -2.22145418758665, -2.1605019074557, -2.22929361026136,
-2.28754415075922, -2.28401553991566, -2.28099274980288, -2.2790962708342,
-2.25448944582185, -2.24095826297856, -2.28753886744317, -2.2790962708342,
-2.28249611688223, -2.29051254450738, -2.28856644852124, -2.29051401027405,
-2.28539476406181, -2.27754391464367, -2.28663199585719, -2.29003809100396,
-2.21974449731936, -2.26453458741267, -2.24273365564754, -2.0758820670505,
-2.05727208640147, -2.1075473897372, -2.18169509790509, -2.10183883759691,
-2.0941547862828, -2.17052540371127, -1.84552009498619, -2.11897925887711,
-2.2475539961602, -2.23266092019845, -2.26826235385205, -2.27716458812284,
-2.22875067319638, -2.20169236013157, -1.72209385129724, -0.476893951190187,
-1.44569200778405, -2.08830648613957, -2.13084935049209, -2.17358829667077,
-2.05592583172644, -1.84265559786228, -1.98577321824112, -1.88660772323127,
-1.99603456442263, -2.10189853669134, -2.14149931364702, -2.22701080101746,
-2.27258448425562, -2.21873931960315, -2.23587705524471, -2.17915915787995,
-1.95172754860073, -2.00843362344438), OLS_6 = c(-2.14615029819501,
-2.1274826763545, -2.14613692884822, -2.038363166, -2.14482079785526,
-2.13839956793073, -2.1352633011825, -2.14480554064275, -1.77137087834078,
-0.604458131512312, -1.92044345866761, -1.91142894340333, -1.5035051350835,
-2.03720410348948, -2.07364942604987, -1.67230210256299, -1.66457879312031,
-0.427523081653794, -2.09111249534671, -2.1358169999572, -2.13575175544593,
-1.44873737433719, -2.14509683128765, -2.12442374236989, -2.02946586195686,
-2.01185030632841, -0.994510606111227, -1.42450007218492, -1.12983335353955,
-0.596198212559954, -1.7115906309286, -2.13555900800151, -2.12811588444992,
-2.09509015766854, -2.11889016916752, -2.02341958358771, -2.03553614239934,
-1.32305159796573, -1.38891263096519, -1.83141440901763, -1.76969899713653,
-1.80596583024281, -2.05682837956465, -1.99043348930533, -2.06558998487816,
-2.14664801486533, -2.13135448546891, -2.1271468279034, -2.14250449627423,
-2.12545741758249, -2.11509475538252, -2.1466464083569, -2.14250449627423,
-2.14442504670383, -2.14684049810003, -2.13838680613343, -2.1468398441846,
-2.14583791450693, -2.14156460788614, -2.14633891192499, -2.14114136796206,
-2.05492693125432, -2.10632999229998, -2.08080895764288, -1.90090288083161,
-1.88144765246016, -1.93417313784094, -2.01325520240903, -1.9281579797124,
-1.92007377040746, -2.00120005643771, -1.66327168223962, -1.94624457366434,
-2.08634430885867, -2.06937728900294, -2.11086434271986, -2.12206882778948,
-2.06498074945642, -2.03501978622706, -1.5377512452434, -0.29431817292714,
-1.25902518147068, -1.91393009686737, -1.95881793980313, -2.00449939786682,
-1.88004274517372, -1.66034827254381, -1.80722288608151, -1.70526848086161,
-1.81783189921089, -1.92822084254891, -1.97013652098612, -2.0630309651189,
-2.1162243283256, -2.0538104595074, -2.07300962091288, -2.0105124912345,
-1.7721107506457, -1.83066883021211)), .Names = c("Y", "X", "Null",
"OLS_1", "OLS_2", "OLS_3", "OLS_4", "OLS_5", "OLS_6"), row.names = c(NA,
99L), class = "data.frame")
My scatter plot will consist of the first column (Y) and the second column (X).
The third column i will not use.
From the fourth column are the curves that are fitted values of OLS regressions.
How do I include them using the plot function?
i am doing this, but its not working
plot(data[,2],data[,1])
for(i in 4:9){
lines(data[,i])
}
What am i doing wrong?
Basically you want
data <- data[order(data$X), ] ## reordering so that `X` is increasing
plot(data$X, data$Y)
for (i in 4:9) {
lines(data$X, data[,i], col = i) ## remember to set `x-coordinates`
}
legend("topright", legend = names(data)[4:9], col = 4:9, lty = 1) ## add legend

Identify significant change on a slope

I am trying to find a point where a big change happens on a slope.
Tried to use ecp::e.divisive() function with different settings and couldn't make it identify the change the way I need.
Below code and plot should illustrate more. Grey lines are cutoffs coming from ecp package, and I am trying to it to plot blue line (which is at the moment plotted manually).
Please advise if there are better packages for this task.
library("ecp")
#get cutoff points
ecpOutput <- e.divisive(x, k=1)
ecpOutput$estimates
#plot
plot(x, bty = "n")
abline(h = 0, lty = "dashed", col = "grey")
#add ecp estimates
abline(v = ecpOutput$estimates, col = "grey", lty = 2)
#ideal line, doesn't have to be exact, anything around this line is OK
abline(v = 384, col = "blue", lty = 2)
Data: x is a sorted 1 column matrix (required structure by ecp).
x <- structure(c(-27.0409169663486, -26.129156710088, -24.7600585044449,
-24.3953228174759, -24.1577613311647, -23.5016667274094, -20.6947912955816,
-20.5608424516568, -20.2274448352933, -20.1776769950718, -20.0154244013066,
-19.9185729879804, -18.6495729388285, -18.5966280348446, -18.4877201244377,
-17.8697488852, -17.6964369055135, -17.4580050047899, -17.4503603253745,
-17.2138236467553, -17.1978137674216, -17.0883917900212, -17.0780609255085,
-16.5936774343097, -16.5302509987677, -16.5047064149008, -16.3610339293733,
-16.3179953827084, -15.8098489669017, -15.8017464582135, -15.7931978631991,
-15.7048014947664, -15.6438615495371, -15.5724025176269, -15.4769417976187,
-15.4566078151486, -15.3683766952941, -15.3598144860889, -15.2872530572521,
-15.2615773975834, -15.1553721281872, -15.1253479156971, -14.8574803029005,
-14.7886609612358, -14.7052679457918, -14.6700476095839, -14.4967591359077,
-14.3902100635321, -14.1344579537429, -14.1122694462137, -13.6977777268339,
-13.5002575880219, -13.3931536856711, -13.2720061859572, -13.1630633420915,
-13.120694599871, -13.0989354290794, -13.0305773443162, -13.0247860189105,
-12.9679198987695, -12.9339256928714, -12.7503935672128, -12.6086380226913,
-12.5204925937268, -12.5018027956008, -12.3362734240611, -12.2633206830352,
-12.2389866512024, -12.2261511983906, -12.2082248950284, -12.1988063383678,
-12.1625438078306, -12.105137364671, -12.0283612849999, -12.0207914207455,
-11.9569828730108, -11.9377135887775, -11.7974478043662, -11.7856404961775,
-11.7070359492143, -11.685818522575, -11.6150116648869, -11.5889351441476,
-11.5696939812167, -11.5218497980405, -11.4278460823336, -11.2616735602107,
-11.1371735315344, -11.1101486927449, -11.0545987008828, -11.0497610649959,
-10.9777305856557, -10.9538432991084, -10.8709063558819, -10.8321978567433,
-10.7154060103612, -10.6904665022414, -10.6728147078525, -10.6319097418566,
-10.6084139374132, -10.5435156393802, -10.4969417190754, -10.4910751521816,
-10.4076974735856, -10.1987743033507, -10.1084368444001, -10.067653486032,
-10.0184841375099, -10.0184804878766, -9.98796628047806, -9.92940612537163,
-9.79791464687017, -9.78385633218692, -9.74746828052048, -9.5340969261009,
-9.3872416240278, -9.34975526969295, -9.34350605265574, -9.33678121532548,
-9.28246068708019, -9.26231844050325, -9.24219935644902, -9.22973616812829,
-9.1945301757694, -9.18742543173638, -9.09818179983656, -8.84383892771356,
-8.71390231428188, -8.63727799501085, -8.62365985718404, -8.57368937545283,
-8.56896270190976, -8.51750651338242, -8.36994967607861, -8.34940777555791,
-8.24579609778514, -8.16499004491889, -8.13648089733029, -8.12443902872708,
-8.06884804631702, -8.01978044346555, -8.00857010892087, -7.98752833340474,
-7.98494612290397, -7.92263788171607, -7.92262794402356, -7.91254741704133,
-7.8612668717642, -7.85956255484269, -7.80614005087113, -7.78172738274975,
-7.76590807725416, -7.73362312192246, -7.73353827316045, -7.73074597208869,
-7.66932615651785, -7.63405189653259, -7.52008106079428, -7.4794467369495,
-7.37006671254996, -7.33891732414829, -7.28638734342099, -7.18383241390338,
-7.16967207372779, -7.12414638131948, -7.11845633549842, -7.10069950413643,
-7.08453272839936, -7.06225675561229, -7.01372949292662, -6.97700431366834,
-6.96221498775977, -6.95474531960413, -6.92853733102012, -6.87948984857266,
-6.81875907966932, -6.80049432153244, -6.74491178906506, -6.73722594655536,
-6.65241543109368, -6.64432866218849, -6.57471615362753, -6.538942870458,
-6.48556582657451, -6.44344567639388, -6.44142334658235, -6.40718664187937,
-6.37631310536975, -6.35766817597632, -6.33004629281548, -6.32243131413274,
-6.31465060236999, -6.30957529705468, -6.20042487075657, -6.19852760812695,
-6.19516991943952, -6.19169299941109, -6.14915041772936, -6.1341182105985,
-6.06886854629022, -6.03316938455024, -5.92620275477516, -5.915861751696,
-5.8993712054553, -5.8562854376353, -5.81203754357936, -5.81031989834777,
-5.79005016179402, -5.70715392727825, -5.64380201251138, -5.61822898444235,
-5.61198428778589, -5.55181215637224, -5.5414283688058, -5.51873245882438,
-5.51677200116567, -5.51551938308407, -5.50995967349956, -5.47456882908066,
-5.46182989367447, -5.34425037377986, -5.32571672656563, -5.24036884690065,
-5.2280272754873, -5.13269699736861, -5.13115055948407, -5.09522822858288,
-5.06292167413738, -5.05058297801937, -5.00080776299476, -4.99907315892117,
-4.96652519349395, -4.94319578011658, -4.93410325981468, -4.88778233710964,
-4.86817149761772, -4.86068237590916, -4.86018697766406, -4.79260202993216,
-4.78704006512903, -4.7697286436275, -4.74319640834353, -4.66791717712299,
-4.63451063153309, -4.55913300698176, -4.54773156325591, -4.5029917001588,
-4.50232432311584, -4.48448651461267, -4.48195952916376, -4.45942294767531,
-4.43684117621408, -4.39862573806896, -4.39218230499561, -4.32218627745706,
-4.29986813130089, -4.25622220689577, -4.24164586680005, -4.1312719357839,
-4.12513288512985, -4.07946940297387, -4.07261512152973, -4.01165511465604,
-3.95381138375493, -3.95044337911054, -3.92340302872436, -3.91833355363622,
-3.87865234740553, -3.84667242505411, -3.78315737709957, -3.76505984684618,
-3.76018864171568, -3.71684064078827, -3.70873189126336, -3.69375597807388,
-3.69325959119626, -3.6505984611436, -3.62183744181462, -3.58883235361055,
-3.52391205093927, -3.51617596119693, -3.51388800083065, -3.33813297717772,
-3.26837985172665, -3.25102638292739, -3.24658212799339, -3.22891542458543,
-3.17892565704276, -3.1283260157199, -3.04352634415322, -3.02299584335501,
-3.01373331291241, -2.99552385674378, -2.86372198270148, -2.83811475137176,
-2.77463274263238, -2.69783066358124, -2.69434624147968, -2.65941589188957,
-2.6221506974122, -2.57311913722599, -2.56672727866588, -2.56667581533302,
-2.55574357522681, -2.50395283367281, -2.49786830801457, -2.46604172470746,
-2.42877871397163, -2.4017493435186, -2.369794488014, -2.30769529142191,
-2.28289171086166, -2.24736457553032, -2.22224835097905, -2.21993628534018,
-2.21938775070459, -2.21330765240871, -2.1970203297687, -2.18405609540363,
-2.1707971627096, -2.16975015841871, -2.11020976431672, -1.75392661111203,
-1.73701872449349, -1.7094653013083, -1.68198644925752, -1.67745819232374,
-1.65515644561234, -1.57194744043552, -1.57116047214388, -1.51000042083035,
-1.50463279855355, -1.46346551065156, -1.44224216231154, -1.37311564470336,
-1.32779620730326, -1.32592791352896, -1.31204307149482, -1.27756472934721,
-1.23603794634302, -1.1702376900797, -1.16378397319292, -1.12499060545068,
-1.1088125112075, -0.939262072448041, -0.862163030101959, -0.858354175324213,
-0.654474645495292, -0.628257109630334, -0.606705081751156, -0.550295255225927,
-0.497332185765383, -0.482929007849059, -0.440957523688129, -0.360777543733516,
-0.305426972877309, -0.199639436822714, -0.108018704644053, 0.0346309488381769,
0.034746934017077, 0.0493559949320308, 0.192914632754347, 0.218667918037225,
0.245737147003821, 0.247527541956785, 0.333222558419853, 0.557256844844099,
0.669063496448389, 0.720955292473698, 0.767660102407662, 0.878291398784322,
0.946178076488395, 0.946467111772845, 1.11121506783806, 1.22835778244999,
1.24961769756564, 1.29790330264223, 1.30574614563616, 1.30823862002444,
1.35802473804966, 1.48779920673317, 1.6470804669896, 1.75657725379367,
1.91434060426954, 3.12793082392592, 4.05818631097523, 5.87762107232264,
7.87360999893622, 54.7844606150857), .Dim = c(389L, 1L))
Try:
e.divisive(diff(x), k = 1, min.size = 2)

Resources