I have the following PCA plot with 7 variables (see data and code below), where I want the variable names to be put in subscript.
In ggbiplot() however, the variable names are automatically taken from the column names of the matrix that was used to generate the PCA, and (as far as I know) no options are available to add these manually.
Therefore I tried to create subscripts in my actual column names by using
c(expression("j"["d"]),expression("k"["1"]), etc.)
which only results in the variable names turning into "j"["d"],"k"["1"] on the PCA plot.
Is there a workaround for this problem? e.g. making the second character smaller or something alike?
The plot was generated using the data:
Param.clean <- structure(c(0.314689287410068, 0.279479887056815, 0.448790689537864,
0.25336455455925, 0.289008161177184, 0.314501392595033, 0.291144087910652,
0.30630205659933, 0.283940162753961, 0.293902791758693, 0.384490609026053,
0.287376099118374, 0.308181312257512, 0.295516976083076, 0.299962079750977,
0.377418190053724, 0.577708482228635, 0.548861542714413, 0.445100820783724,
0.454234613160057, 0.509303280474031, 0.485557486397235, 0.512794671011103,
0.438809386918853, 0.584488774396788, 0.485077847448695, 0.54242611837146,
0.50295109738886, 0.462140343335828, 0.482811895512131, 0.505123975906175,
0.454883826310242, 0.270198900375524, 0.375171915727647, 0.415330703464691,
0.335520417496773, 0.337301727971001, 0.414448624199441, 0.413407199816623,
0.480251511263297, 0.381597639419929, 0.299997369191106, 0.716363262901133,
0.334190348030789, 0.339749957548872, 0.615860520668703, 0.399995858493781,
0.290916481482953, 0.271565709782391, 0.434971269297489, 0.426102875513371,
0.245620918873499, 0.350757895503111, 0.314711831388176, 0.233155192989713,
0.248289747469244, 0.260316266382216, 0.435133707574921, 0.270357707406285,
0.460714026041302, 0.300202262584418, 0.283894965208827, 0.286731230742906,
0.476076636930455, 0.496419713185873, 0.449716335449948, 0.414301742272173,
0.517741851053675, 0.452096411019313, 0.503619428840069, 0.46590684730812,
0.426164828533173, 0.526710272381684, 0.60162289738927, 0.501571299694807,
0.5204289094248, 0.501944294148778, 0.420989057029306, 0.486676941655541,
0.581232141136961, 0.388451077003248, 0.348575471498664, 0.541637601056563,
0.280529225927791, 0.474527715587717, 0.427368925204716, 0.247233045036043,
0.371205006512827, 0.350378420436755, 0.334934610173675, 0.672485054825788,
0.387370130421541, 0.442394016641109, 0.410245000087745, 0.554591956294395,
0.292541225836523, 4.53967830833669, 4.07671677879989, 6.31220323958745,
5.53951495559886, 6.14831664270411, 2.49250044245273, 3.56566691364472,
0.905669305473566, 5.65883946946512, 2.64297457644716, 3.05508165226008,
3.18054619676744, 6.40823390030613, 3.37302493648604, 3.66350222987433,
2.54057804516827, 8.20203100920965, 2.71440979468947, 2.91087136765321,
1.99383332931126, 2.54861955298111, 2.76731871856997, 2.8168199171933,
1.87471640761942, 2.36744814319536, 2.70068269921467, 3.73984078429639,
3.14727020682767, 2.44860090082511, 2.96811391366646, 3.15924824448302,
2.75038693333045, 0.571991651163747, 0.0795687369691844, 0.199285715352744,
0.278876264579594, 0.0878147967159748, 0.776516450569034, 0.109050695318729,
0.214585168287158, 0.0733157177455723, 4.99666969059035, 0.681611322797835,
0.511083496330927, 0.103351746220142, 2.09765716294448, 0.163525143420944,
1.15420421690991, 3.94337840843946, 3.16239102048179, 5.81127347030366,
5.75235136287908, 2.48392526193832, 2.46317023644224, 1.81044878205284,
4.65439113788307, 5.15721979085356, 2.87866174476221, 2.5224589696154,
5.64499958222732, 1.54218871717652, 2.67105548409745, 3.03890538634732,
4.12773523628712, 2.30713835917413, 1.77077499361088, 2.28220948716626,
2.27822186658159, 2.22687301691622, 2.78089357145751, 2.10296940756962,
1.62778628757223, 2.64038743038351, 2.90401477599517, 2.85171007970348,
2.15843657962978, 2.22618574043736, 2.01146884588525, 7.50444248911614,
2.78893498703837, 0.369707935024053, 0.640407053288072, 1.16690336726606,
1.19069673353806, 0.369720328599215, 0.222492094617337, 1.97003725056226,
0.0771822298566498, 0.0491951033473015, 1.58942595124245, 0.56345314020291,
3.61783248605207, 0.470429616980255, 4.25140980218227, 0.416233133369436,
0.838344213552773, 2.73752116598189, 3.02740207407624, 1.11222450388595,
2.30047973571345, 2.45276295114309, 2.64581680834914, 2.78265510452911,
3.27471463242546, 3.1145193031989, 2.56884276599934, 3.15895244479179,
3.14939826028422, 2.39397626441593, 3.8150685878781, 2.75754480964194,
2.56850058445707, 1.10134650183221, 1.90155507440989, 1.71837465837598,
2.29802527697757, 1.24960316416497, 1.95164571283385, 1.49695883272216,
2.51760663697496, 1.04954799870029, 1.59203378250822, 1.33138975808397,
1.30761714885011, 1.6880701482296, 1.33289409801364, 1.18721167324111,
2.10289117880166, 4.97532397741452, 4.08132408546905, 4.83296488539005,
5.65673472359776, 1.52374835970501, 4.90433675702661, 6.09970323707287,
4.9611738567551, 4.98475494328886, 1.58062443907062, 6.51368894614279,
5.82539446347704, 4.37618843046948, 6.41044923532754, 1.36502551613376,
2.11988553637639, 2.87185357650742, 1.99067048101375, 2.6186517810449,
1.56325603065391, 3.18718514405191, 2.67383103277534, 3.39795261013011,
3.97267814834292, 3.7929962793986, 2.75519806658849, 2.67046403462688,
1.61068025619412, 3.21088001991933, 2.72053461754695, 2.89008031133562,
2.44437138317153, 1.87622146913782, 2.30537068654473, 1.77740855213876,
1.46343538770452, 2.23820108221844, 1.83238286990672, 1.70535395620391,
3.20611382601783, 1.42552105011418, 1.35411406867206, 1.67505901074037,
1.27963638935859, 1.50879823369905, 3.20626547553887, 1.34457757696509,
1.4350421866402, 3.01776180580879, 4.42168406192213, 2.68915122840554,
5.10021819965914, 5.7995775481686, 3.69961343705654, 5.6582765411896,
5.42690238449723, 4.12574668414891, 2.88072699628149, 7.37651169135546,
0.877918794285506, 3.48383912583813, 7.16048748052369, 1.10681456684445,
2.0056554214408, 0.843951161950827, 0.892569730058312, 1.82299897540361,
1.4852886760508, 1.34192016645273, 1.12837524153292, 0.788718643598258,
2.59369957595567, 1.03540072683245, 0.767321502789855, 1.23622662722568,
0.784033099189401, 0.784212271682918, 1.25203002375116, 0.76296656858176,
0.985467154532671, -0.220396793447435, 0.106205735355616, 0.124459093059103,
-0.00892141033460625, -0.10377890299509, 0.0926174888437004,
-0.0549866305664182, 0.278899986296892, 0.0474233353460836, -0.109990653581917,
0.0474225463395319, 0.0338427349925041, 0.0972763542085886, 0.114185092970729,
-0.00886052381247282, -0.0589170539751649, -2.3298938000525,
-1.84932016064972, -3.90579685030132, -6.07330848347147, 1.8304963277777,
-2.75582764982432, -4.77531149517745, -3.8228796642224, 1.27799427995086,
-5.45959737020979, -2.00253919902196, -2.22745593055834, -2.6610222319141,
-3.21496389806271, -6.01237997648368, -3.13309627585113, 0.733061715583007,
1.04009207207213, 1.51182846035312, 1.92732659168541, 1.29081471823156,
0.809032674878836, 0.954354595015446, 1.5407008677721, 0.961584224718311,
0.949504360179106, 1.17135864682496, 0.758904917165637, 1.27744460012764,
1.13802453503013, 1.12143621314317, 0.923086409457028, -0.207958693792422,
-0.112425229889651, -0.114883695351581, -0.0418341891790419,
0.00953924376517534, 0.0761805102229118, 0.06189932115376, -0.279747098684311,
-0.139795616269112, -0.0957655208185315, -0.211875131353736,
0.15934879556795, -0.307479116310674, -0.171517195557555, -0.147559982724488,
-0.151702696457505, -3.2736393770054, -3.77255320500831, -2.50166923739016,
-3.38403152301908, -3.54271008856346, -5.98730765748769, -3.46145537216216,
4.08535061942786, -5.5706409165586, -5.12871207815409, -2.00980313849697,
-1.74987012389551, -4.99012113984178, -5.52220372986048, -4.09768380224705,
-2.65192667357127, 0.368457175791264, 0.276677635120849, 0.660357913002372,
0.536901503801346, 0.122066596522927, 0.248441367586455, 0.326834246516228,
0.414462564513087, 0.374505277723074, 0.157977417111397, 0.147561579942703,
0.327407247386873, 0.481645831460754, 0.471423282288015, 0.402634991332889,
0.600018523255984, -0.754529740661383, -0.333761049988369, -0.479209786502022,
-0.338527356651923, -1.15156446321805, -0.187009451910853, -1.59113974179576,
-0.26058504227052, -0.629410240799189, -1.30694395600756, -0.916838761041562,
-1.07011808082461, -1.22627768199891, -0.492068426683545, -1.54347744450718,
-0.104520670138299, 1.40759190544486, 7.8479420303603, 2.75170721710225,
3.93423731307934, 9.85715725720922, 5.37083122879267, 6.15668724537641,
4.96914687916388, 6.46718368138125, 1.0871206430619, 9.84838488511741,
9.75057146977633, 1.26769720241924, 9.92010527290404, 9.98543933499604,
9.69677802640945, 0.169932479038835, 0.409494925844172, 0.409328970126808,
0.740250064991415, 0.193447070196271, 0.51206505260865, 0.250175041146576,
0.391306541860104, 0.295244073495269, 0.243191891349852, 0.293793072924018,
0.408511371351779, 0.281213674316803, 0.680068365474543, 0.41473192628473,
0.371123384684324, -0.753847653398911, -0.768953422084451, -1.10839967615902,
-0.771105247549713, -0.155546843451758, -0.282887876386441, -0.444554830901325,
-0.305730849504471, -0.499526713974774, -0.561585665126642, -0.731248073279858,
-0.888600847683847, -0.92906893696636, -0.32135354558875, -0.635939710773528,
-0.817948141487935, 1.6278464008073, 3.22130429296692, 3.22974945083757,
7.91888088977089, 9.00321571025501, 2.45285976212472, 6.25077115575721,
5.03100211266428, 2.74867731204381, 1.44042827261984, 5.0080717401579,
9.99481210485101, 1.47605382240812, 5.14414160776883, 7.90287723037352,
9.74082155153155, 6.49911883796255, 4.32981192308168, 8.23767292803774,
4.77091148030013, 9.32961360147844, 2.72199803938468, 3.16884732668598,
0.829324850315849, 5.05701699654261, 2.34405943658203, 3.72586945071816,
2.25735736116767, 6.09207524452358, 4.18884709620227, 3.34469041476647,
2.58207156453282, 0.749518116253117, 10.288818734698, 4.99554592402031,
12.6404861267656, 7.78458069656044, 5.22634824855874, 19.4536860734845,
18.1442198018854, 2.79630955830216, 9.58980514369905, 7.07662330872069,
2.31912315823138, 4.37504726642122, 10.6357821771254, 4.86855507971098,
15.2487045279394, 2.38136078696698, 2.2241358395045, 2.6578728514413,
2.00018415475885, 14.3105512278154, 2.79016453381628, 2.82581362407655,
3.00503022968769, 0.867751725018024, 19.840897029452, 2.76507987820854,
2.18780075665563, 2.29634886607528, 5.24806297849864, 3.00219499040395,
3.75070014856756, 4.70993515464167, 2.90138749877612, 7.88213442545384,
6.81599063475927, 2.61536476109177, 2.09377944457034, 1.96296699593464,
5.57947221212089, 5.20963124415527, 3.33875400945544, 2.79699660371989,
4.58355573223283, 1.3866287143901, 2.73741390556097, 3.83854200132191,
3.64578404258937, 8.6152262147516, 7.4471927308167, 10.7907397029921,
3.60064423537503, 2.21575071569532, 13.2584175148358, 6.69871457573026,
7.57425001679609, 10.597095658568, 6.29717063158751, 19.982005469501,
4.82507357889165, 12.0198037318264, 12.1128156222403, 0.971864601969719,
7.90290065680941, 2.69096855397026, 3.41408452807615, 3.34672867252181,
3.73752327635884, 2.59765107041846, 3.69969989638776, 6.49256678763777,
1.34200508563469, 2.58853476804991, 6.08448572133978, 2.86518771201372,
8.80958803463727, 3.21931545517097, 13.9356314451744, 3.31248219124973,
2.6213849833856, 6.81703055885931, 4.57180783512692, 0.800920105539262,
1.6915382258594, 1.97274463716894, 8.35091653894633, 4.07009290065616,
4.70143776244173, 3.73712390195578, 3.32298003757993, 2.70389301739633,
9.40504621323198, 5.22653986488779, 6.91535883117467, 4.01246708252778,
6.83368936007222, 5.52582112283756, 9.52527065730343, 5.19140504300594,
8.41044230709473, 6.70183763448149, 6.82530618272722, 9.10367484707385,
14.269079283004, 4.7895190725103, 6.59831102186193, 5.62791180796921,
6.17603897117078, 7.14836313854903, 5.96534776215752, 6.33691875052949,
4.8933652261893, 13.8731955783442, 17.7908931604276, 13.79737106661,
12.8477370319888, 1.87629146464169, 19.782149810344, 19.4288346171379,
18.9387338103106, 18.204667886657, 1.15545298295716, 19.7008843562255,
17.4563148757443, 14.7906976851945, 17.3621598140026, 1.16834398824722,
1.00026320200414, 15.0063681416214, 3.00477131232619, 5.1401701791212,
0.385864693671465, 4.36085817695906, 8.55562331421922, 5.70195167902857,
5.37442221703629, 4.73609448876232, 3.56490389804045, 5.62534291626265,
1.87125703754524, 4.91213823029151, 5.71994946518292, 5.81859005757918,
4.93084813430905, 8.53895935813586, 8.24653955462078, 5.77503573757907,
8.75251863257339, 6.42709499839693, 6.27165456948181, 6.44690599292517,
16.0425839032357, 5.03840921912342, 5.87823822697004, 4.41305622781316,
5.94650622780124, 5.56597112355133, 1.1121899643292, 5.33032094594091,
5.42635044082999, 6.6131685036545, 11.4281271025538, 6.12669843180726,
14.5658381014441, 18.4343010187149, 10.0486588804051, 19.8108604625488,
19.3252983829007, 9.68507033698261, 7.62796785042932, 14.9589012558262,
0.670023332349956, 11.1040308237076, 17.0357564882065, 1.06506975100686,
0.740390195945899), .Dim = c(96L, 7L), .Dimnames = list(NULL,
c("jd", "k1", "k2", "b1", "b2", "p1", "p2")))
and code:
library(ggbiplot)
Param.pca <- prcomp(Param.clean,scale=TRUE)
groups <- factor(c(rep("Cercis L",16),rep("Cornus L",16),rep("Ginkgo L",16),rep("Cercis R",16),rep("Cornus R",16),rep("Ginkgo R",16)))
g <- ggbiplot(Param.pca, choices=c(1,2), obs.scale = 1, var.scale = 1, ellipse.prob=0.95,
groups = groups, ellipse = TRUE, alpha=0, varname.size=6,labels.size = 10)
g <- g + geom_point(aes(shape=groups,col=groups))
g <- g + theme(legend.direction = 'vertical',
legend.position = 'right',
axis.text=element_text(size=14),axis.title=element_text(size=14),
legend.text=element_text(size=14),legend.title=element_text(size=14))
plot(g)
Following up on #RichardTelford's comment, you can get the code for ggbiplot by typing ggbiplot in your console. Then copy and paste this into a script file and assign it to a new name, say, my_ggbiplot. Then change the last if statement in the function from this:
if (var.axes) {
g <- g + geom_text(data = df.v,
aes(label = varname, x = xvar, y = yvar,
angle = angle, hjust = hjust),
color = "darkred", size = varname.size)
}
to this:
if (var.axes) {
df.v$varname = paste0(substr(df.v$varname,1,1),"[",substr(df.v$varname,2,2),"]")
g <- g + geom_text(data = df.v,
aes(label = varname, x = xvar, y = yvar,
angle = angle, hjust = hjust),
color = "darkred", size = varname.size, parse=TRUE)
}
Note the addition of the line that creates the new text labels and then the addition of parse=TRUE to geom_text. This hard-codes the labels to work specifically for your case, but you can make the function more general if you're going to do this a lot.
To run the function (once you've loaded it into your environment):
my_ggbiplot(Param.pca, choices=c(1,2), obs.scale = 1, var.scale = 1, ellipse.prob=0.95,
groups = groups, ellipse = TRUE, alpha=0, varname.size=6, labels.size = 10) +
geom_point(aes(shape=groups, col=groups)) +
theme(axis.text=element_text(size=14),
axis.title=element_text(size=14),
legend.text=element_text(size=14),
legend.title=element_text(size=14))
And here's the result:
Related
I have been trying to plot a surface from the following data
z = structure(c(-0.901395069908367, -0.889270928722939, -0.860498522734513,
-0.81162341264254, -0.741289594106116, -0.648766092305491, -0.534349208041337,
-0.399845199903741, -0.249855361975959, -0.0897251475874947,
0.0746259084372811, 0.236078784565598, 0.387628389740501, 0.523650864552803,
0.640660077165393, 0.737009776188609, 0.812016566072894, 0.866304532376613,
0.901568276352029, 0.920360368688146, -0.882227601197732, -0.878930792308559,
-0.858884362955903, -0.817800704213168, -0.753368006803807, -0.66449012298986,
-0.551487546899363, -0.416392170535135, -0.264538800185667, -0.101517270999555,
0.0666888146596484, 0.232341014061553, 0.38749240207806, 0.524415630048401,
0.640084827807316, 0.733314764232348, 0.80301493330195, 0.850313451774813,
0.877753646407397, 0.88963725156383, -0.847475017108294, -0.85307262716591,
-0.842574356483547, -0.810407792264141, -0.752945080664338, -0.668633289794334,
-0.558328099823412, -0.425304557346782, -0.275029532996457, -0.112783840666026,
0.0558934516670588, 0.222087091307297, 0.377068406557726, 0.512939126260687,
0.625804332859799, 0.714297176038507, 0.777443068815926, 0.816496676689634,
0.836080905877643, 0.842475340232409, -0.794484724176902, -0.808695744871725,
-0.807980823999131, -0.784767233676891, -0.734736669998055, -0.656413316398881,
-0.550933437104475, -0.423358808055153, -0.278506129655091, -0.120866143889105,
0.0433499698178821, 0.204910622000686, 0.354654201404564, 0.486317568693109,
0.594088245807692, 0.675931270610041, 0.731821498045365, 0.763893435823378,
0.777321922428609, 0.779323256471572, -0.721035151310193, -0.742489349579308,
-0.750777748074823, -0.736960272021206, -0.694879238996425, -0.623996082218869,
-0.526887497647841, -0.408661964047638, -0.273737770263875, -0.125356088457535,
0.0291260358276813, 0.180223786309321, 0.319893992902204, 0.442102408314768,
0.541837117518898, 0.616080337260617, 0.665334074499435, 0.692652951176559,
0.702073015925212, 0.70014294591476, -0.62634454997212, -0.653025685850572,
-0.669123471922786, -0.664287895904395, -0.631116905593864, -0.570128054300068,
-0.484723579158402, -0.37958495150019, -0.258935393270118, -0.125654469777847,
0.0133852524560905, 0.148202883542656, 0.272475225263341, 0.380626909415146,
0.468703561895249, 0.534264335754137, 0.577639625419541, 0.60157076785374,
0.608220344177334, 0.604222909445142, -0.512344852448439, -0.541555029880959,
-0.562961881990283, -0.566220190988011, -0.543544051940367, -0.494559171738039,
-0.423645361722741, -0.335975893586483, -0.235168989845246, -0.122814581317589,
-0.00544137604122541, 0.107867126384677, 0.212427329329769, 0.302909552255676,
0.376324025384858, 0.431708923178868, 0.469101279162711, 0.489651660797114,
0.495110354415309, 0.491340850885662, -0.381309022143908, -0.410783959771577,
-0.435389666816656, -0.445770602448373, -0.433004934811996, -0.397491381573017,
-0.344416516747127, -0.278325021678467, -0.202169912833898, -0.117875088678067,
-0.0294056925490386, 0.0579458089153829, 0.140043646562862, 0.211102719345402,
0.267777922857105, 0.311419904628903, 0.341855012664907, 0.358730431743406,
0.364518297473459, 0.363240891735784, -0.234818927315893, -0.262530061791553,
-0.288066101196323, -0.304121353637761, -0.30170450668554, -0.28177357685378,
-0.249245809281562, -0.208160977212965, -0.161032375996862, -0.109589438945933,
-0.0551612509212895, 0.00133984767391211, 0.057278847069763,
0.106580792282658, 0.145980466712106, 0.176865463055578, 0.199541843346534,
0.213637631708287, 0.221141803400151, 0.223933303241254, -0.0765199640532891,
-0.0999824673920636, -0.124480103191673, -0.144232572992753,
-0.152909373229339, -0.150600362070916, -0.140686596578978, -0.127276509402488,
-0.112080967355925, -0.0959971815561244, -0.0777115240226103,
-0.0558079539376473, -0.0309319906934945, -0.00646144860933681,
0.0146751773765434, 0.0330917549345564, 0.0489244989926914, 0.0618218644227028,
0.0717616083498969, 0.0786560810115295, 0.0876811192087983, 0.0697400208872219,
0.0487788538006448, 0.0269055967832573, 0.00740020063383676,
-0.00851809193381578, -0.0224246952709679, -0.0379057923626619,
-0.0560184192271167, -0.0760996990081513, -0.0948316128077681,
-0.108936070754438, -0.11749086793581, -0.120509248006123, -0.118650306371636,
-0.112254286791432, -0.102307567397665, -0.0903088095336494,
-0.0780987508225131, -0.0677553904853005, 0.252646739761749,
0.240806809421569, 0.223572332072489, 0.200384337470906, 0.171532673986367,
0.138038979275939, 0.099610125026476, 0.0551872652032152, 0.00478723497468162,
-0.0502739399996399, -0.105903142355679, -0.155416635318454,
-0.196689276852176, -0.227281308354055, -0.245510439464005, -0.251680533953819,
-0.248151683021562, -0.237873594096041, -0.224643816034859, -0.211430976424376,
0.412083487867202, 0.405482299038271, 0.391485749933225, 0.366992663926528,
0.329942922079822, 0.280376438559889, 0.21975434771463, 0.148349710354354,
0.067171462923436, -0.0202860188429894, -0.109178441152915, -0.191951597181515,
-0.263855321315342, -0.320191772332899, -0.358801475967917, -0.379017864632865,
-0.383976898947948, -0.377538947209839, -0.364498733398927, -0.348859987545735,
0.56036438679084, 0.557157741301204, 0.545344926607978, 0.518682844541529,
0.473913612380679, 0.411336768404074, 0.332508721380021, 0.237443242494199,
0.128967481760446, 0.0132250816174241, -0.104269156052445, -0.215831154932383,
-0.314796148353222, -0.395698871967524, -0.454216961405671, -0.48939478748932,
-0.504182544361458, -0.503121698419469, -0.491894461703185, -0.47520035398754,
0.69476361802097, 0.692646546646937, 0.680749564713899, 0.651398622302503,
0.600078777493038, 0.527047463117019, 0.433013134286506, 0.318393751312497,
0.187931883938919, 0.0485415189557076, -0.0924602858559757, -0.227761779764008,
-0.349631680299174, -0.451607698807979, -0.528541780407822, -0.578524956928284,
-0.603804265252205, -0.609120896979876, -0.601289608746566, -0.585540179764667,
0.813266228275571, 0.810506038003487, 0.796844139738609, 0.764453028221258,
0.707450116347561, 0.62571592477967, 0.519614498955702, 0.390246591949968,
0.243341967239212, 0.0861522807453793, -0.0736279085948786, -0.227866185933834,
-0.367494474867194, -0.485506871937427, -0.577239558663347, -0.640408877746243,
-0.676403050530757, -0.690576743314995, -0.688678577814345, -0.676429718923359,
0.914072702896753, 0.908848990145119, 0.891584857432632, 0.855267032404637,
0.793376249877853, 0.705115303908622, 0.590546028589521, 0.451414245822445,
0.293839303219628, 0.124680267405642, -0.0479108928191147, -0.215106602279399,
-0.366655256382102, -0.495161152151185, -0.598221357198254, -0.673169863459292,
-0.720490363187427, -0.745181116693124, -0.752125846282775, -0.746149917016764,
0.996287679954195, 0.986818541859255, 0.964596295451714, 0.923525516175463,
0.857144605481058, 0.763534587185077, 0.643488846938705, 0.499393204416234,
0.337016085032734, 0.161976880625728, -0.016856861210691, -0.190116150981591,
-0.348063434276771, -0.484140957679691, -0.596125932517742, -0.681344256408742,
-0.740038823605145, -0.775737430336284, -0.792663253588154, -0.794732374768692,
1.06061439454562, 1.04544076891118, 1.01703653222416, 0.970302993290611,
0.899469571463457, 0.801462174884477, 0.678313451930742, 0.533231256117207,
0.370392935930347, 0.195249209837787, 0.0162592847293909, -0.157296899896799,
-0.317350547718038, -0.458168879616197, -0.576604274565736, -0.670309502260843,
-0.739228543711965, -0.785405276981519, -0.812214818331992, -0.822803313137382,
1.10885869520928, 1.08711267560539, 1.05160959155953, 0.998207666512568,
0.922707795799498, 0.822302049371678, 0.698635387488528, 0.554846624007452,
0.394359048691908, 0.223064832052992, 0.0483074128559527, -0.121990772157994,
-0.280872368359659, -0.423065532821516, -0.545072934983457, -0.644763290060129,
-0.721744372269504, -0.777280364350403, -0.813503852239487, -0.832785269227836
), .Dim = c(20L, 20L))
and I have been using the plotly library, for example, with
library(plotly)
fig <- plot_ly(z = ~z, type = "surface", x = seq(0, 1, len = 20), y = seq(0, 1, len = 20))
fig
My problem is that the graph looks strange in that the x-axis and y-axis are much too small. The problem is alleviated if instead I use
fig <- plot_ly(z = ~z, type = "surface")
fig
but then the axis ticks are not correct since the data were collected in the [0,1]-cube. Is it possible to resize the first plot so that it looks more like the second one but with the correct axis ticks? Thank you.
Based on this link,
library(plotly)
fig <- plot_ly(z = ~z, type = "surface", x = seq(0, 1, len = 20), y = seq(0, 1, len = 20)) %>%
layout(scene = list(aspectratio = list(x=1, y=1, z=1)))
fig
I need a graph including three lines and one of them has different y-axes.
I wonder why the blue line is so short here? This is my code:
library(plotrix)
a <- c(41.995 ,41.749, 41.484, 41.200 ,40.898, 40.587, 40.274 ,39.968, 39.672, 39.388, 39.108 ,38.822,
38.521 ,38.202, 37.866, 37.528, 37.205, 36.909 ,36.643 ,36.401, 36.167 ,35.918, 35.631 ,35.289,
34.886 ,34.418, 33.890, 33.320, 32.726, 32.121, 31.517 ,30.924, 30.348, 29.792, 29.258, 28.749,
28.262, 27.789 ,27.324, 26.861, 26.401, 25.944 ,25.489, 25.034 ,24.571 ,24.087 ,23.564 ,22.996,
22.390 ,21.755 ,21.114 ,20.495 ,19.923 ,19.416 ,18.984 ,18.625 ,18.332 ,18.083)
b<-c(1960:2017)
c<-c(22.184, 21.652, 21.126, 20.608, 20.096, 19.591 ,19.093, 18.603, 18.122, 17.650, 17.187 ,16.730,
16.280, 15.838, 15.408 ,14.994, 14.601, 14.233 ,13.892, 13.577, 13.290, 13.027 ,12.786 ,12.558,
12.337, 12.117, 11.888 ,11.649, 11.398, 11.135 ,10.864, 10.588, 10.316 ,10.055, 9.808, 9.579,
9.370 ,9.179 , 9.003 ,8.841, 8.692 , 8.555 ,8.427 , 8.306 , 8.189 , 8.074 ,7.958 , 7.839,
7.720 ,7.602 , 7.492 ,7.393 , 7.312 , 7.251 ,7.212 ,7.194 ,7.195 , 7.210)
d<-c(5.906, 5.902, 5.894, 5.880 ,5.859, 5.830 ,5.794, 5.751 ,5.702, 5.648 ,5.587, 5.519 ,5.444, 5.363,
5.278 ,5.192, 5.108, 5.029 ,4.956, 4.889 ,4.827 ,4.766 ,4.703, 4.636 ,4.563 ,4.484, 4.399 ,4.311,
4.222 ,4.132, 4.045, 3.959 ,3.877 ,3.799 ,3.723 ,3.651 ,3.582 ,3.514 ,3.446, 3.379 ,3.311 ,3.244,
3.176 ,3.109, 3.041, 2.972, 2.899 ,2.823, 2.743 ,2.661, 2.581 ,2.506, 2.439 ,2.381, 2.333 ,2.295,
2.266 ,2.243 )
df1<- data.frame(Year=b,CBR=a,CDR=c,TFR=d)
twoord.plot(lx=df1$Year,ly=df1$CBR,rx=df1$Year,ry=df1$TFR,type = "l",xaxs='i',xticklab =c(1960:2019,by=3),xtickpos =c(1960:2019,by=3),xlab = "Year", ylab = "Number",main = 'R')
grid()
lines(b,c ,type = "l", col = "blue")
Hi welcome to Stackoverflow, in the future please include the library call which has any none base R functions that you use.
The below gives you what you want
library(plotrix)
ypos <- seq(from = 0, to = 50, by = 5)
twoord.plot(lx=df1$Year,ly=df1$CBR,rx=df1$Year,ry=df1$TFR,type = "l",xaxs='i'
,xticklab =c(1960:2019,by=3),xtickpos =c(1960:2019,by=3),xlab = "Year",
ylab = "Number",main = 'R', lytickpos = ypos, lylim = c(0,50))
grid()
lines(x= df1$Year, df1$CDR, type = "l", col = "blue")
The blue line is small because some of its values are off the chart. This is because when the limits are picked automatically, CDR is excluded so adding it later with lines() plots only the values above 18, which is only prior to 1970 for CDR.
I've passed the lylim as 0 to 50 and I've also added the lytickpos as going from 0 to 50 in increments of 5.
Although this answers your question, this graph makes no sense to me.
I am plotting my PCA data (= pca.df) with ggplot (t9) and try to label my data points. With over 500 data points right next to each other, the labeling gets messy if I label every single one of them, so I want to label just every 10th or 15th data point.
I tried using gghighlight, but my basic knowledge of ifelse statements leads to no solution for me.
My code for (t9) is printed down below. The result should be a Score/Score-Plot with the values of PC1 and PC2 on the axes and every, lets say 5th element being labelled according to rownumber.
> dput(head(df))
structure(c(0.720861821444252, 0.741748310125995, 0.708402634437037,
0.709801133484984, 0.715066449559326, 0.736082130721757, 0.653704393279032,
0.698276222706427, 0.72211406630421, 0.715258352183308, 0.687541455566144,
0.70691880577236, 0.648677410108962, 0.652162185222687, 0.673335514807695,
0.686310528373139, 0.672288782506351, 0.685387769196099, 0.606520717118424,
0.591747992573417, 0.637540141146717, 0.643172972625407, 0.631438730296424,
0.684507675451748, 0.555771668718988, 0.593784695895615, 0.587382247577035,
0.576747109369824, 0.609475457932671, 0.59866246596185, 0.519063669593429,
0.530651696453032, 0.532787309576192, 0.541282412745153, 0.548262870872043,
0.538801663579734, 0.4839370855738, 0.483476075398052, 0.495569187972609,
0.508351252780073, 0.516728822566894, 0.536204665645588, 0.444866548123319,
0.451359569708855, 0.488498854129039, 0.458566775758124, 0.466211854024398,
0.491560347455407, 0.391256576032243, 0.407839446833575, 0.415186340652814,
0.424286965099726, 0.411873304932725, 0.429761415133547, 0.339836624254536,
0.368786621339532, 0.355816757014086, 0.378789270638685, 0.39335185857301,
0.391171949534279, 0.301518457021116, 0.323263419961053, 0.319326889442142,
0.329577970161398, 0.332317131236052, 0.328401547614829, 0.278194980292123,
0.271241021424707, 0.278009324614118, 0.289181065286873, 0.282708689390152,
0.287325696956387, 0.217765754577593, 0.229671138051344, 0.214270216785571,
0.248548146541018, 0.242536426938002, 0.247615674841088, 0.161232560624038,
0.167964791643414, 0.184493790352952, 0.169637631152996, 0.207171184044563,
0.18671607328531, 0.136854807157759, 0.110836224164859, 0.141700425964129,
0.170466088007464, 0.147693931044626, 0.161727182052285, 0.0820803384886784,
0.094874042231684, 0.0838404020068718, 0.106453346170999, 0.0893222925431942,
0.105425228691039, 0.0320549073043817, 0.031817899518242, 0.0492796841876071,
0.0534417407043242, 0.0492509775017762, 0.0576741314907473, -0.0003305571982993,
0.00517263223466776, 0.00898939160288738, 0.0137528774437919,
0.0152605422021288, 0.0215229583963186, -0.0532648337441121,
-0.0283369016701845, -0.0236614679612245, -0.0250738126949397,
-0.0339585310595162, -0.00502894085262935, -0.0837238471177474,
-0.0406195676958014, -0.0552223579150677, -0.0469508535844212,
-0.0427121136759291, -0.0552519953457953, -0.099633841516817,
-0.0838034423810929, -0.0891078733208937, -0.0847178896069714,
-0.0416689904316896, -0.0845884534908026, -0.112879391070739,
-0.116357070717273, -0.105954173536172, -0.0809224012272034,
-0.0803030066250538, -0.0804524938179006, -0.141076501626193,
-0.141494024691483, -0.133849678959928, -0.118385990843758, -0.113423371384572,
-0.123928162937715, -0.145978888454582, -0.145163933507897, -0.155263241214673,
-0.139704305017438, -0.119946496925743, -0.143872145220887, -0.157460920557041,
-0.172462482989155, -0.161340062935618, -0.160945553437212, -0.142106857091405,
-0.146243108149656), .Dim = c(6L, 25L), .Dimnames = list(NULL,
c("1360", "1361", "1362", "1363", "1364", "1365", "1366",
"1367", "1368", "1369", "1370", "1371", "1372", "1373", "1374",
"1375", "1376", "1377", "1378", "1379", "1380", "1381", "1382",
"1383", "1384")))
pca.df = prcomp(na.omit(df), center = FALSE)
t9 = ggplot(get_pca_var(pca.df)$coord, aes(x = get_pca_var(pca.df)$coord[,1], y = get_pca_var(pca.df)$coord[,2])) +
xlab("PC1") + ylab("PC2") +
geom_point() +
geom_text(aes(label=seq(1:nrow(get_pca_var(pca.df)$coord)), hjust=0, vjust=5))
t9
This gives the following plot, but with every element labelled:
I want some code part like this as a result, but with the ifelse or for statement (every nth row/column, for example 5th element in my "try" below) to highlight the labels:
b = as.numeric(rownames(as.data.frame(iris.pca$x)))
ggplot(iris.pca) +
geom_line(aes(x, y, colour = colour)) +
gghighlight
""(for (b in PC1) {
if (x%%5 == 0) {
print(label = as.numeric(rowname))
} else (no label)
}""
Thanks for the help!
I would have added few lines before. It is similar to your idea.
Every_Nth_label <- 5
Index <- 1
Label_full <- c(1:length(get_pca_var(pca.183s)$coord[,1]))
for(I in Label_full){
if (Index %% Every_Nth_label != 0){
Label_full[Index] <- ""
}
Index <- Index + 1
}
t9 = ggplot(get_pca_var(pca.183s)$coord,
aes(
x = get_pca_var(pca.183s)$coord[,1],
y = get_pca_var(pca.183s)$coord[,2])
) +
xlab("PC1") +
ylab("PC2") +
geom_point() +
geom_text(aes(label=Label_full, hjust=0, vjust=5))
Please let us know if this is what you wanted.
I am having an issue when trying to fit my data.
This is the data:
x = c(1, 1.071519305, 1.148153621, 1.230268771, 1.318256739, 1.412537545, 1.513561248, 1.621810097, 1.737800829, 1.862087137, 1.995262315, 2.13796209, 2.290867653, 2.454708916, 2.630267992, 2.818382931, 3.01995172, 3.235936569, 3.467368505, 3.715352291, 3.981071706, 4.265795188, 4.570881896, 4.897788194, 5.248074602, 5.623413252, 6.025595861, 6.45654229, 6.918309709, 7.413102413, 7.943282347, 8.511380382, 9.120108394, 9.77237221, 10.47128548, 11.22018454, 12.02264435, 12.88249552, 13.80384265, 14.79108388, 15.84893192, 16.98243652, 18.19700859, 19.498446, 20.89296131, 22.38721139, 23.98832919, 25.70395783, 27.54228703, 29.51209227, 31.6227766, 33.88441561, 36.30780548, 38.9045145, 41.68693835, 44.66835922, 47.86300923, 51.2861384, 54.95408739, 58.88436554, 63.09573445, 67.60829754, 72.44359601, 77.62471166, 83.17637711, 89.12509381, 95.4992586, 102.3292992, 109.6478196, 117.4897555, 125.8925412, 134.8962883, 144.5439771, 154.8816619, 165.9586907, 177.827941, 190.5460718, 204.1737945, 218.7761624, 234.4228815, 251.1886432, 269.1534804, 288.4031503, 309.0295433, 331.1311215, 354.8133892, 380.1893963, 407.3802778, 436.5158322, 467.7351413, 501.1872336, 537.0317964, 575.4399373, 616.5950019, 660.693448, 707.9457844, 758.577575, 812.8305162, 870.96359, 933.2543008, 1000)
y = c(0, 39.42531967, 81.67031097, 126.9366341, 179.8504534, 237.9146471, 300.9332733, 373.9994125, 452.2911911, 544.5717812, 644.4305916,757.5670443, 880.1954813, 1015.045167, 1160.563695, 1316.477197, 1483.424418, 1668.380672, 1869.099593, 2083.298305, 2308.72922, 2552.533248, 2806.782363, 3074.749213, 3354.913032, 3653.567198, 3961.982443, 4285.416754, 4625.505185, 4974.839962, 5329.418374, 5696.722268, 6069.748689, 6447.903256, 6826.334958, 7218.057591, 7607.64304, 8005.992733, 8403.318251, 8798.355661, 9201.456877, 9613.1821, 10022.47749, 10430.83497, 10841.5067, 11256.68048, 11675.94707, 12085.72448, 12500.17168, 12905.54582, 13311.92593, 13707.0245, 14089.76524, 14459.48122, 14813.21421, 15145.30591, 15459.10593, 15752.7922, 16023.09928, 16269.888, 16493.69043, 16693.68774, 16869.79643, 17021.69506, 17154.34004, 17264.76423, 17355.82129, 17427.48725, 17486.7706, 17530.49824, 17563.61638, 17588.39795, 17605.32753, 17617.36935, 17624.3971, 17629.48694, 17632.2512, 17633.91595, 17634.67971, 17635.11862, 17635.35591, 17635.4941, 17635.61014, 17635.64404, 17635.66099, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794, 17635.67794)
EDIT
Number of iterations increased untill 1000.
This is the code I used with the equation that has physical meaning when interpreting the results of the coefficients:
# tanh function definition:
ftanh = function(x, x0, a, b, k) {
(1/2) * a * (1 + (tanh(k*(x-x0)))) + b
}
# Fitting code using nonlinear least square:
nlc <- nls.control(maxiter = 1000)
fitmodel <- nls(y ~ ftanh(x, x0, a, b, k), control=nlc, start=list(x0 = 7, a = -29500, b = 17500, k = -0.032))
# Plotting fitted cumulative function
options(scipen = 10)
plot(x, predict(fitmodel), type="l", log = "x", col="blue", xlab = "x", ylab = "y")
points(x, y, col = "red")
legend("topleft", inset = .05, legend = c("exp","fit"),
lty = c(NA,1), col = c("red", "blue"), pch = c(1,NA), lwd = 1, bty = "n")
summary(fitmodel)
This is the best fit I got using Excel just by guessing the initial values (orange line):
I am assuming that my initial values are not the best, but I am in a constant loop and it seems like there is no way out. I am pretty sure that b value is okay, and that x0 and a are around my initial values, but k is actually determining the steepness. Any idea?
The problem is that your fit function does not describe your data. In the logarithmic plot it looks OK but is is actually more like a simple tanh. As the bending differs from tanh an even better model is of type a tanh(b*(x-c)**d)**(1/d)
Not using r but simple python it looks like:
import matplotlib
matplotlib.use('Qt4Agg')
from matplotlib import pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
def func1(x, a, b, c):
return a*np.tanh(b*(x-c))
def func2(x, a, b, c,d):
return a*np.tanh(b*np.fabs((x-c))**d)**(1/d)
initialGuess1=[y[-1], 1, 0]
initialGuess2=[y[-1], 1, 0,1]
popt1,pcov1 = curve_fit(func1, x, y,initialGuess1)
popt2,pcov2 = curve_fit(func2, x, y,initialGuess2)
print popt1
print popt2
fittedData1=[func1(s, *popt1) for s in x]
fittedData2=[func2(s, *popt2) for s in x]
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(x,y)
ax.plot(x,fittedData1)
ax.plot(x,fittedData2)
ax.set_xscale('log')
plt.show()
>> [ 1.74207552e+04 3.53554258e-02 2.20477585e-01]
>> [ 1.76893061e+04 1.90416542e-01 1.19819247e+00 5.59529032e-01]
Data in blue, tanhfit in orange and modified fit function in green.
Initial guesses are straight forward. (You could play around a bit to get rid of the kink near zero)
I'm trying to draw a figure of waveforms. I want to show which part of the waveforms are statistically significant. Currently, I draw a semi-transparent polygon:
plot(wave,
type = "l",
col = "red",
lwd = linewid,
pch = 19,
lty = 1,
xlab = "Time",
xaxt = "n",
xlim = c(1,duration/2),
ylab = "Difference",
ylim = c(-1, 1))
abline(h = 0)
sig <- ifelse(pval<.05,wave,0)
polygon(c(1:length(sig)),
sig,
col = rgb(1,0,0,0.5),
border = NA)
But the polygon isn't centered on the zero line of the y-axis. I'd like to shade only the areas underneath the curve of the wave. Instead, the polygon extends above the zero-line on the y-axis. Any ideas?
wave <- c(0.0484408316308237, 0.0474054439781486, 0.0467022242629086,
0.046515614318914, 0.0466686947981267, 0.0466777796491931, 0.0460966374555009,
0.0457341620230469, 0.0455045060507858, 0.0457719372614871, 0.0461446812125276,
0.0460051963987539, 0.0456347093964464, 0.0430700479769684, 0.0435837207487517,
0.0443970279017918, 0.0457508738133201, 0.0472350978374988, 0.0482361020656729,
0.0494006907171422, 0.0504508971582255, 0.0521263688769232, 0.0532433489463588,
0.0537137380543864, 0.0540428548151276, 0.0544949143122896, 0.0544225549891838,
0.0538337952743033, 0.053135984213764, 0.0523491809303349, 0.0520472332622518,
0.0517736309847163, 0.0518684887760298, 0.0514603496453925, 0.050769752723635,
0.0504389714171051, 0.0502927164308292, 0.0504354031597342, 0.0498799936275558,
0.0490825606436222, 0.0497213009991454, 0.0501938355481634, 0.0514117871384259,
0.0519380643522052, 0.0517968505801706, 0.05123157072507, 0.0520909551474945,
0.0486858357936371, 0.0493763701994425, 0.0500160784148426, 0.0505488877007248,
0.0497678090074788, 0.0480758661250716, 0.0462675525180396, 0.0453516919016191,
0.0448339366059345, 0.0445615385738649, 0.044013178561506, 0.0439648543393159,
0.0438670362724258, 0.0440913799994017, 0.0507925460506875, 0.0509727145985309,
0.0510872776847506, 0.0508104967241469, 0.0503812271559447, 0.0503631548556902,
0.0505562349708585, 0.050869650537224, 0.050115073380279, 0.0496307336460131,
0.0486946602966385, 0.0451240814629419, 0.0439636677233932, 0.0428989167765818,
0.0420026704819646, 0.0411584695778936, 0.0403788602838661, 0.040233539087147,
0.0397175149422268, 0.0389289880494877, 0.0378327839257036, 0.0360351888196015,
0.0347926091711749, 0.0341079891575494, 0.0348740749311286, 0.0349125506875405,
0.0352951033387814, 0.0344798859212136, 0.0327899391390952, 0.0303925310234825,
0.0275215845941342, 0.0265832329092289, 0.0220646495463752, 0.0122404036320984,
0.00743877530625988, 0.00181246669131438, -0.00479231506410288,
-0.0117717813867494, -0.0192370027513411, -0.027223713620762,
-0.0348613553743107, -0.0397127268587883, -0.045622681570717,
-0.0515358709254366, -0.0568288397365667, -0.0620165857779051,
-0.0669105535816898, -0.0720264470900791, -0.0766882017731929,
-0.0804427064040755, -0.0815328596670379, -0.0826051939881404,
-0.0879974600724217, -0.0924894198404777, -0.0949544486488778,
-0.104737046247734, -0.11695750473657, -0.132892205151458, -0.15164997657278,
-0.172597865364775, -0.196113512673009, -0.216646106105455, -0.244400723622597,
-0.267988695108909, -0.292598978473393, -0.317086468049069, -0.342530108945073,
-0.368486808868852, -0.399730966642985, -0.433385374917961, -0.469543692326107,
-0.507867318915593, -0.547443797215136, -0.586749203029937, -0.625603126037644,
-0.6626968183054, -0.697811797372003, -0.730226229712439, -0.760716192518167,
-0.789754092566875, -0.819837732291987, -0.844265792897494, -0.865853848085839,
-0.88772546496204, -0.908008383337203, -0.926193346905058, -0.943720637018896,
-0.958657012974673, -0.971195039738284, -0.981680462787076, -0.989209920087862,
-0.994760927508405, -0.998179967730494, -1, -0.99985631234348,
-0.998513746223383, -0.996286337260218, -0.994167673024296, -0.992029087667234,
-0.98942063019129, -0.986657143470197, -0.982080217651251, -0.97535310006632,
-0.967706563058861, -0.959931873177486, -0.953053148939477, -0.945050149326435,
-0.936863678952672, -0.927476791110897, -0.917244708485839, -0.910092208942064,
-0.898659859433262, -0.887894272800133, -0.874966302881798, -0.860464316462603,
-0.843766510522863, -0.826854760379226, -0.809030674702751, -0.790830214526396,
-0.773448702041121, -0.757822022781962, -0.742415284193991, -0.726650963278141,
-0.708671839205669, -0.690647887135473, -0.669566331925841, -0.647484673858103,
-0.625415272964118, -0.603346317669516, -0.580945690740634, -0.559174605387308,
-0.537166524153666, -0.514979755494959, -0.492190905789554, -0.4688961948937,
-0.445648845564897, -0.421370990246327, -0.394957034231288, -0.367257387362894,
-0.339759436905685, -0.31347732732076, -0.287795514335449, -0.263477496955318,
-0.240335559473844, -0.219537822868833, -0.199356394938618, -0.182724128026289,
-0.162855943390834, -0.143113588174923, -0.122168088165277, -0.100967800471397,
-0.0777710229443332, -0.0539643915465976, -0.029677750446946,
-0.00631959566058126, 0.0169258078383277, 0.0389612599575379,
0.0609770118878408, 0.0806172669927091, 0.102073705616963, 0.122665362014863,
0.142328282171209, 0.161475578433955, 0.17913203293436, 0.199700604404855,
0.216864487908698, 0.232810273813389, 0.248031682891701, 0.262732844598723,
0.276791405782004, 0.289592381780554, 0.302904563305743, 0.315933177369042,
0.331194285957781, 0.34328787498597, 0.355317635956366, 0.37161156141851,
0.385496981280364, 0.39906005718835, 0.410609126043194, 0.424557700817611,
0.432614645845991, 0.440840405298792, 0.446859449278095, 0.451067862561763,
0.454108550491332, 0.45822766032593, 0.463669025285741, 0.468751886735504,
0.477222371161998, 0.480825169330004, 0.484778309657409, 0.490686208003411,
0.496271560877119, 0.502429910894803, 0.504627189056216, 0.509277950091572,
0.512796647131139, 0.51920303298796, 0.526246371444159, 0.530172995082546,
0.537137361380815, 0.540646539738041, 0.541350429187775, 0.538037378748107,
0.53640396369579, 0.533982169384575, 0.531715003489143, 0.537771148135836,
0.541774025400292, 0.542397661260989, 0.542734418123511, 0.541900634648552,
0.551336284191385, 0.550830246001875, 0.543935588412506, 0.54850236576883,
0.546095769955119, 0.546560717106744, 0.54862072115046, 0.549873891251691,
0.549022573851835, 0.556546139368112, 0.560110491782052, 0.563328136311772,
0.556118487214394, 0.556145005611977, 0.56177222985652, 0.56211974460993,
0.563139174079545, 0.567595800986669, 0.564911164049346, 0.55370467099592,
0.549142761248365, 0.553638360274585, 0.552297050636191, 0.551520866080127,
0.545782391084414, 0.550845817729848, 0.551708815237408, 0.552680776857495,
0.560806019030281, 0.566586972251016, 0.570035930685996, 0.57778944843747,
0.575407636591647, 0.576215607600804, 0.580856907896507, 0.58111203256702,
0.580550879830819, 0.579468018580888, 0.569996133796829, 0.571472497444831,
0.570186841693214, 0.579257568699911, 0.585984875703449, 0.592864903673866,
0.592890757109184, 0.602046235792163, 0.613343736000507, 0.61690652667541,
0.615073893877543, 0.603386282668406, 0.605140483512513, 0.602317438726602,
0.601349093084652, 0.60175903066173, 0.595748856842631, 0.592466664315233,
0.579486755875179, 0.561987007946437, 0.542492099234415, 0.526383565525105,
0.5230428319822, 0.513300797695766, 0.515049254563791, 0.518848257099875,
0.506235765674015, 0.49998294091854, 0.506344856229246, 0.50475172054043,
0.507702294279798, 0.506348179486846, 0.517341319120319, 0.523551522223028,
0.530756340907612, 0.53032745351512, 0.533111198195776, 0.526453901436172,
0.529598066926201, 0.523624800099041, 0.516232193418245, 0.517039928536979,
0.501904786914197, 0.49713387651536, 0.505916234878408, 0.502395600515955,
0.489414472392961, 0.476329169842886, 0.485088777888902, 0.48219652186471,
0.469886812116599, 0.453441250799586, 0.441168281111148, 0.437074892507931,
0.438771226156789, 0.434582625256592, 0.434393831049118, 0.455313871944686,
0.46667176154786, 0.451614470975422, 0.446531804915084, 0.448747422127997,
0.442389961671837, 0.452345098594434)
pval <- structure(c(0.0237628302370617, 0.0262165284800235, 0.0285686821840486,
0.0297087853681475, 0.0299733840361694, 0.0301202024224222, 0.0323058180308351,
0.0339316553612034, 0.0381144580106053, 0.04487408115707, 0.057804021059368,
0.085158184225468, 0.136972404452296, 0.0594954230338983, 0.059815561375559,
0.0586890211651837, 0.054984325985342, 0.0505545271596015, 0.0488952044969173,
0.0471087059136155, 0.0460746356928649, 0.0424450757403614, 0.0405444189843919,
0.0411829464282132, 0.0412927140116675, 0.0417488854396546, 0.0445117370815736,
0.0550285077721912, 0.0747425914230263, 0.111097457523042, 0.15043556390993,
0.0494268999973122, 0.0517649576685409, 0.0562362352328751, 0.0608441259158306,
0.0635243703413948, 0.0648470007376663, 0.0649911512153626, 0.0678711704637943,
0.0714853336274117, 0.0656394713851027, 0.0631287997230727, 0.0691221286169588,
0.0998026839190638, 0.149978285257545, 0.195667278177658, 0.139258740157594,
0.0641286728653938, 0.0590485245121715, 0.0549751823968685, 0.0525288256855241,
0.0566756473108664, 0.0675151254516982, 0.0797001157496837, 0.0893120076127157,
0.101040948627555, 0.113937187901608, 0.136477809610576, 0.151438694370951,
0.168116109618061, 0.167693574892715, 0.055626851340157, 0.055837522780422,
0.0561583595040639, 0.0594879473890239, 0.0632759635313892, 0.065859251440062,
0.065772893740802, 0.0639366385273011, 0.0671023312185317, 0.0694330478072044,
0.0733540770931381, 0.0927895033698356, 0.0987657684863097, 0.106752925133343,
0.113783571923594, 0.121501003290016, 0.129032949168017, 0.136116836003546,
0.151762019664018, 0.175690091657166, 0.202670406948741, 0.246692883279612,
0.29427387065597, 0.339407794720914, 0.437126245014218, 0.457917873424833,
0.481184291252777, 0.523332782522114, 0.577097846183451, 0.630184174499617,
0.685063979797142, 0.719978579352288, 0.772396520583243, 0.880445896200477,
0.930819867424807, 0.983821279666508, 0.958549578396286, 0.901224976502897,
0.843510707377769, 0.785288039341357, 0.732678879305588, 0.705605278527497,
0.668649736650829, 0.632671877729848, 0.601886683772611, 0.572995378718532,
0.547203236154384, 0.522892433560929, 0.50484004632912, 0.495167623435313,
0.501651238918971, 0.508756506196948, 0.504741256872503, 0.496754657283146,
0.483869763822611, 0.453324086883336, 0.417453905522249, 0.37213400662027,
0.324969615847784, 0.279986826269611, 0.238305111925328, 0.217603724159869,
0.18615434974012, 0.165914268934125, 0.148553539851396, 0.134089539639757,
0.120930326838334, 0.108816102552138, 0.0943727175866119, 0.0809908262000654,
0.0688676027935202, 0.057965295683139, 0.0485177042268327, 0.0406774650506777,
0.0343750114181405, 0.0296041466961569, 0.0257754426846805, 0.0228491378540575,
0.0203762486696415, 0.0182050611089517, 0.0151389244136079, 0.0143291699625514,
0.0135272132937536, 0.0129494727394939, 0.0125413188392993, 0.0120654899893143,
0.0115593848885709, 0.0110971538277043, 0.0106563593895204, 0.0102594799209655,
0.00995618264115798, 0.00976027085065125, 0.00966493285702107,
0.0096809864774456, 0.00984417807568116, 0.0101140178609648,
0.0104536689988165, 0.0107455438459773, 0.0109879390786196, 0.0112476004827354,
0.0115064972197262, 0.0118436117195952, 0.012250241785758, 0.0127098856583327,
0.0131923546938643, 0.0135900513062996, 0.0140907822475909, 0.0146146781894138,
0.0153666265314495, 0.0163587611869966, 0.0165777793501283, 0.0179231032274303,
0.0193146107648153, 0.02112595949975, 0.023429090158608, 0.0263502163261689,
0.029668879819774, 0.0333886702034785, 0.0373851358797521, 0.0414244618086564,
0.0452421641298093, 0.0491730557898139, 0.0534544517392593, 0.0586659192358243,
0.0642835353988069, 0.0717515494462237, 0.0803329713593607, 0.089702211494325,
0.100024733160442, 0.111522126685171, 0.123611882298163, 0.136659951151313,
0.150762549445491, 0.166322127637489, 0.183448713985098, 0.201980927862526,
0.223096893280781, 0.248582577870016, 0.278233697709439, 0.310833681563501,
0.345332013645289, 0.382705067848975, 0.421376239222594, 0.460853428063773,
0.498027141394812, 0.535355843788925, 0.572293181350887, 0.611900790530155,
0.652931049846446, 0.698550246370366, 0.746816303915033, 0.801970397733016,
0.860682735611148, 0.922505922723998, 0.983357734959766, 0.955187832059461,
0.89653477564825, 0.837851886741715, 0.781781567273432, 0.723265191915832,
0.667546011138303, 0.614998358930736, 0.564647511026012, 0.519566323581666,
0.470697912789023, 0.432376848315165, 0.398409375708932, 0.367655920642225,
0.338940852965934, 0.312212428665514, 0.288459290398895, 0.264878592699687,
0.242704842685072, 0.217387439877778, 0.19835325593237, 0.180556517879591,
0.158171361155571, 0.141413156894301, 0.126554452518633, 0.114979049999799,
0.102918102342695, 0.0962216185484208, 0.0901194930496858, 0.0860130895609037,
0.0832340770284259, 0.0812259095628692, 0.078291162078237, 0.0742796112910293,
0.070046963535911, 0.0638805429841068, 0.0600325581294298, 0.0556731158844295,
0.0505107871259209, 0.045748968232586, 0.0409239605265858, 0.038056048177405,
0.0346897761994006, 0.0322017147611021, 0.0290191309178551, 0.0259412764702286,
0.0239876220554575, 0.0213928561132381, 0.0196012142768874, 0.0188687591178971,
0.0191111545249072, 0.0190313087419941, 0.019003849481473, 0.0188195734297014,
0.0172119066683942, 0.015771401215516, 0.0148547142814098, 0.0142875281889536,
0.0138706460080897, 0.0117612653636045, 0.0113802773390233, 0.0119929691882122,
0.011301932221071, 0.0113391829691033, 0.0111974712246918, 0.0109290980999376,
0.0105620733291735, 0.0102904702021988, 0.0088461881724392, 0.00730041492139247,
0.00668346613062251, 0.00766574212937, 0.00783946561969262, 0.00788512630201997,
0.00782058882264157, 0.00767516163055654, 0.00747564784065695,
0.00812968647923146, 0.0091908030084859, 0.00940025876764169,
0.0084452386928757, 0.00773239171673778, 0.00774262750329184,
0.00817566848920374, 0.00775737017229728, 0.00833705507967201,
0.00848219824583305, 0.00775531985314739, 0.00731753429028076,
0.00624994399211075, 0.00566319903650268, 0.00580078551643243,
0.00589284481572378, 0.00557777343775064, 0.00561015236859766,
0.00558045776809902, 0.0062741801741615, 0.00693752198709533,
0.00680148894969647, 0.00682650280136682, 0.00648353640440989,
0.00559647299682892, 0.00560310591626879, 0.00630599774150389,
0.00625849640781645, 0.00496543301213065, 0.00527106355173805,
0.00606524474497961, 0.00782669446225163, 0.00804663337329233,
0.00680216943527376, 0.00549477843115955, 0.00521495965843119,
0.00547975304067046, 0.0058083233283578, 0.00752964959779626,
0.00928425608337011, 0.0121528464847978, 0.0151204119890877,
0.0167466130623839, 0.0184702177828752, 0.0190546966808364, 0.0194217711957074,
0.0172588666752732, 0.0220542021375166, 0.0191777261144048, 0.0207814209894351,
0.0194965472902094, 0.0209260124322778, 0.0170037074001148, 0.0125849197829535,
0.0116502405937885, 0.0120529295967995, 0.0116481500418768, 0.013878269349617,
0.0138614750865797, 0.0153190475349968, 0.0194121459536112, 0.017739167396571,
0.0204726597729821, 0.0245650013533451, 0.0208055307319631, 0.0210348908355722,
0.0278106725250406, 0.035960679421438, 0.0358929798768166, 0.0405937721886476,
0.0510142810649717, 0.0561354348677331, 0.0862664430860822, 0.101130560202344,
0.111832280018731, 0.0774469081405625, 0.0813608282057401, 0.063555234011225,
0.0517641089682014, 0.0663811539378655, 0.0602507226995198, 0.0626085278234381,
0.0676820928084705, 0.0566520960018359), .Dim = c(376L, 1L))
The devil is always in the details with polygon vertices! Using your data from above, here is a solution and some explanation.
Get the polygon vertices, almost
sigy <- ifelse(pval < 0.05, wave, 0)
sigx <- 1:length(wave)
sigxy <- data.frame(sigx, sigy)
This is pretty much what you used in your original question. It doesn't quite work because while the polygon function accepts and connects x,y pairs, it also closes the polygon. Also, your approach was drawing a lot of polygons with zero area. So, some processing of sigxy is necessary to split it into separate polygons. This could be done manually, but it's more fun and useful to automate the process. First, remove the entries where sigy is zero. Then let's replot to see where things stand:
tmp <- sigxy[sigxy$sigy !=0,]
plot(wave, # removed some unnecessary items to simplify
type = "l",
col = "red",
xlab = "Time (by index)",
ylab = "Difference",
ylim = c(-1, 1))
abline(h = 0)
lines(tmp, col = "green")
At this point, we are much closer but are still connecting the dots and not making polygons, since we don't return to the zero line when necessary. Let's create a column that will show us where the polygons should start and stop, then gather that info into a couple of vectors of indices:
tmp$diff <- c(1, diff(tmp$sigx))
st <- 1 # start indices
end <- c() # end indices
for (i in 1:nrow(tmp)) {
if (tmp$diff[i] > 1) end <- c(end, i-1)
if ((tmp$diff[i] > 1) & (i != nrow(tmp))) st <- c(st, i)
if (i == nrow(tmp)) end <- c(end, i-1)
}
Now refresh the plot and add the polygons one at a time
plot(wave, # removed some unnecessary items to simplify
type = "l",
col = "red",
xlab = "Time (by index)",
ylab = "Difference",
ylim = c(-1, 1))
abline(h = 0)
for (i in 1:length(st)) {
DF <- tmp[st[i]:end[i], 1:2] # Just the data to be plotted
# Add the points needed to drop to the zero line
DF <- rbind(c(DF$sigx[1], 0), DF, c(DF$sigx[nrow(DF)],0))
polygon(DF, col = "green")
}
The result:
Bryan Hanson's solution translated into a function:
drawarea <- function(wave, pval, color){
sigy <- ifelse(pval < 0.05, wave, 0)
sigx <- 1:length(wave)
sigxy <- data.frame(sigx, sigy)
tmp <- sigxy[sigxy$sigy !=0,]
tmp$diff <- c(1, diff(tmp$sigx))
st <- 1 # start indices
end <- c() # end indices
for (i in 1:nrow(tmp)) {
if (tmp$diff[i] > 1) end <- c(end, i-1)
if ((tmp$diff[i] > 1) & (i != nrow(tmp))) st <- c(st, i)
if (i == nrow(tmp)) end <- c(end, i-1)
}
for (i in 1:length(st)){
DF <- tmp[st[i]:end[i], 1:2] # Just the data to be plotted
# Add the points needed to drop to the zero line
DF <- rbind(c(DF$sigx[1], 0), DF, c(DF$sigx[nrow(DF)],0))
polygon(DF, col = color, border=NA)
}
}