How to read multiple line formula for systemfit in R - r

I am using systemfit to run system of equations using SUR method. I need to read long (multiple line) formula.My simple reproducible dataset can be accessed using following codes.
dat<-structure(list(Time = structure(c(9L, 7L, 15L, 1L, 17L, 13L,
11L, 3L, 23L, 21L, 19L, 5L, 10L, 8L, 16L, 2L, 18L, 14L, 12L,
4L, 24L, 22L, 20L, 6L), .Label = c("Apr-00", "Apr-01", "Aug-00",
"Aug-01", "Dec-00", "Dec-01", "Feb-00", "Feb-01", "Jan-00", "Jan-01",
"Jul-00", "Jul-01", "Jun-00", "Jun-01", "Mar-00", "Mar-01", "May-00",
"May-01", "Nov-00", "Nov-01", "Oct-00", "Oct-01", "Sep-00", "Sep-01"
), class = "factor"), ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("A", "B"), class = "factor"), y1 = c(25L,
14L, 45L, 15L, 24L, 17L, 18L, 19L, 14L, 15L, 25L, 14L, 45L, 15L,
24L, 17L, 18L, 19L, 14L, 15L, 25L, 14L, 45L, 15L), y2 = c(4L,
3L, 4L, 5L, 1L, 4L, 5L, 3L, 6L, 4L, 2L, 5L, 4L, 3L, 4L, 5L, 1L,
4L, 5L, 3L, 6L, 4L, 2L, 5L), x1 = c(3L, 4L, 1L, 8L, 6L, 7L, 9L,
7L, 3L, 1L, 2L, 5L, 6L, 3L, 4L, 1L, 8L, 6L, 7L, 9L, 7L, 3L, 1L,
2L), x2 = c(4L, 3L, 4L, 5L, 1L, 4L, 5L, 3L, 6L, 4L, 2L, 5L, 4L,
3L, 4L, 5L, 1L, 4L, 5L, 3L, 6L, 4L, 2L, 5L), x3 = c(3L, 4L, 2L,
8L, 6L, 7L, 9L, 7L, 3L, 1L, 2L, 5L, 6L, 3L, 4L, 2L, 8L, 6L, 7L,
9L, 7L, 3L, 1L, 2L), x4 = c(4L, 3L, 4L, 5L, 1L, 4L, 5L, 3L, 6L,
4L, 2L, 5L, 4L, 3L, 4L, 5L, 1L, 4L, 5L, 3L, 6L, 4L, 2L, 5L),
x5 = c(3L, 4L, 3L, 8L, 6L, 7L, 9L, 7L, 3L, 1L, 2L, 5L, 6L,
3L, 4L, 3L, 8L, 6L, 7L, 9L, 7L, 3L, 1L, 2L)), .Names = c("Time",
"ID", "y1", "y2", "x1", "x2", "x3", "x4", "x5"), class = "data.frame", row.names = c(NA,
-24L))
My example formula is like this,
model1<- y1 ~ x1 + x2 + x3
+ x4 +x5
eqSystem <- list(model1)
library(systemfit)
fit_prod_SUR <- systemfit(eqSystem, method= "SUR", data=dat)
print(fit_prod_SUR)
I have to include several very long formulas into eqSystem. But my problem is since my formula (e.g. model1) are very long it has got multiple lines. When I run the eqSystem with systemfit, it reads only the variables in the very first line of each formula. I tried with following code, but it does not work.
model1<- (get(paste("y1 ~ x1 + x2 + x3",
" + x4 + x5", sep="")))
But it does not take as a formula. Please could anyone help me to how to read all variables (in multiple lines) of formula in R.

Related

warning when running clmm model

I am trying to run a clmm to examine the effects of average_Mg, average_Mn, and average_ZN on the response variable (Spawn_ID)
I have two random effects that are added into the model "(1|Time/ID)" (time is nested into ID and each ID has a different number of time because some ID's have more observations than other IDs) (see picture)
BLA14 has time 1-8 while BLA2 has time 1-11
here is my data:
data1 <- structure(list(Spawn_ID = structure(c(1L, 1L, 1L, 2L, 2L, 3L,
3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L,
2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 3L, 3L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 2L, 3L, 3L,
1L, 1L, 1L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L,
1L, 1L, 2L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L), .Label = c("1",
"2", "3"), class = c("ordered", "factor")), average_Mg = c(0.0841567034686979,
0.00262726492114602, 0.353259164624795, -0.0364882169624394,
0.355390763410209, -0.151476304441742, -0.0936982567875358, 0.162382425722223,
-1.42681140542971, -1.07244649331608, -0.781118702245109, -1.21952099438996,
-1.38524008095868, -0.898983285649544, -1.01088138645042, -1.56153946055641,
-1.21949786511762, -0.887755361220044, -1.28496187594515, -1.82368718501736,
-1.44293956646485, -1.55766693069354, -1.58965315500885, -1.44731801677922,
-1.90014469879122, -1.39433781118039, -1.3954911611769, -0.866262522744268,
-0.893780676281797, -1.13725619637354, -1.50364564206296, -0.845596282408769,
-0.911535321390588, -1.41202503488084, -1.16477711028459, -0.928588557438047,
-1.0717825099406, -1.27071094552779, -0.102981887484371, -0.419552015986426,
0.207549199784127, -0.26688619247063, -0.0285388879140084, -0.104483586190019,
-0.374343912017509, -0.079286617457435, 0.502081027554914, 0.617397464912897,
0.658216645632926, 0.426442596169416, 0.437015933595451, -1.09407187513834,
-0.289722984650423, 0.189585827711078, 0.388488397202216, -0.814972632964376,
0.0191088328270176, -0.0788536189757766, -1.01469763732677, -0.532896761096259,
-0.319455285766885, 0.32814415425849, -1.10320849618017, -0.802227061565149,
-0.540984260212337, -1.14703007008055, -0.482835715257794, -1.23114539323637,
-0.881874890913403, -0.479104993907372, 0.722375571708869, 0.890893182481924,
1.1899980340988, 0.784338248057351, 0.6979419698913, 1.10833467622332,
0.749554401158789, 0.761841531876783, 1.38243732271989, 1.38537086804658,
0.686397638698291, 1.38660240844063, 1.90358175564482, 1.30513882860368,
0.9706833889022, 2.02786238069347, 0.633278771850179, 0.773554195390702,
1.26407209402018, 0.711780226990467, 1.12404623953355, 0.772411304871684,
0.746907192859431, 1.26546029276059, 0.754582077531832, 0.97865102792368,
1.25739249978455, 1.00030371910859, 1.20251423376581, 0.508886239812359,
1.0614400765502, 1.15560112394629, 0.810899892639843, 0.864356170995008,
1.0722853284304, 0.459017471399662, 0.622305015414975, 0.654778017554924,
0.630012469467092, 1.66357555743707, 1.51486425579301, 0.468256912570358,
1.46986769298999, 0.842853031161864, 0.527443923164085, 0.878231897515369,
1.01564664723517, 0.548373164352724, -1.54705977070176, -1.7628880927376,
-1.93886600741023, -1.75280825324115, -1.06329603003556, -1.76583856532739,
-2.03620478132805, -1.37852741943318, 0.491445103158986, 0.579237889782203,
0.581147814257234, 0.587993370694159, 0.673660535135936, 0.773224639425602,
0.472056000685565, 0.803037596940575, 0.686349802316703, 0.67297390357697,
0.877084884098423, 0.116853127650954, 0.633207695175741, 0.475407810726902,
0.410454398351338, 0.761825383439366, 0.049981065597767, 0.352528868363907,
1.08494544768163), average_Mn = c(0.550041395336084, 0.106013801445048,
0.195501740474326, 0.443055327801251, 0.166039412117922, 0.306826485641131,
0.0779488541952551, 0.253041943200378, -0.398175664467298, -0.248653116953824,
1.62701418452763, 2.12294900204613, -0.653505561079324, 1.18649551151282,
1.73706605332464, -0.663237964024895, -0.502022515338005, 1.37488148179862,
1.52730443891077, -1.02791508823558, -0.912000741847277, -1.13836183212639,
-1.0203769070181, -0.944729930532721, -1.08022080193656, -0.798637523498975,
-0.892966674264699, -0.106867747759299, -0.446077807686831, 0.260985129778798,
0.34207999245625, -0.256052324398952, -0.455901687059591, -0.124589605852459,
-0.330899017708168, -0.155941754430574, 0.179724557145222, -0.13333037309261,
-0.467033378092841, -1.02354293597083, -0.324678510862249, -0.780338971498965,
-0.638323663037884, -0.833876759864611, -1.23290374823043, -0.791651926017789,
-0.507461130613381, -0.8368428812325, -0.0557361418099642, -0.804531913988396,
-0.704346938462444, -0.483778753666885, 0.393712753506288, 0.427715184816051,
0.479775271348421, -0.28130811769396, 0.485295306906789, 0.29873238753337,
-0.32311305698219, -0.153688760742991, 0.566586154995381, 0.432231338528875,
1.06596225482086, 1.35634853919191, 3.80694326707882, 0.810475117293976,
3.0387608847462, 0.633718464176746, 1.1751890651466, 3.9845446592986,
-1.37297875557724, -1.09420268685371, -0.962614525736854, -1.29805138281804,
-1.31055976157738, -0.925719147858883, -1.23243978183704, -1.33945462492742,
-0.733417598434015, -0.930363022530595, -1.27774840900127, -0.551645520494324,
1.07748304807901, -0.396816101914801, -0.619403529151145, 0.904015824338447,
-0.287220128038401, -0.413048098445259, 0.612345039773056, 0.12520419272066,
0.144889119165393, -0.376872327860885, -0.371321461123647, 0.713414294431202,
-0.220319649249486, 1.91457944140036, 1.96765430981412, -0.121347747943895,
1.83594114293694, -0.874846076775421, 1.57970089137696, 1.93625153606136,
0.346364402583701, 1.77363591575721, 0.715776292044604, -0.11504600156397,
0.957194866002839, -0.887101387136546, -0.780049232064872, -0.336706490132965,
-0.70438883179631, -0.873817482659086, -0.284434200328209, -0.568305044660584,
-0.775993306095371, -0.771770658609636, -0.383838373137703, -0.811970593682299,
-0.73465457432939, 0.0911351344017551, 1.2707682140586, -0.777318831552788,
1.11134230637355, -0.783796841885501, 0.259370669933754, 1.73241147006831,
-0.725970777900951, -0.438118569726494, -0.278337133147783, -0.493758812846972,
-0.518179622331699, -0.389789488287258, -0.298381997017347, -0.612423478602631,
-0.429659970678406, -0.292999247051666, -0.435183828919258, -0.897633859213429,
0.507974413224703, 1.43201049921323, -0.66047577071763, 0.786340467145023,
-0.95656045144662, -0.132752976801546, 1.18466694854915), average_Zn = c(0.199618761426643,
0.591792310386536, 1.3751346661716, 0.332582639073901, 0.560402369414685,
0.11570820588524, 0.192150397195304, 1.39288706671957, -0.394083581015067,
-0.0798076017654593, 1.17669193020793, 0.607211261125105, -0.452359206037806,
0.771410079352111, 0.494544784665782, -0.625267178823271, -0.39226745167263,
1.16595745311139, 0.395578830413474, -1.34293822466577, -0.742961944779728,
-1.55476909975203, -1.21117698813619, -0.705958753206111, -1.37118754349972,
-0.917928244368175, -0.974434964905969, -1.01929394953207, -1.12532371386981,
-1.22700631035692, -1.46133511017614, -0.952449170432, -1.21027641358059,
-1.3226625940125, -1.16430485224588, -1.08770522025381, -1.18121144700268,
-1.40033431712296, -0.618587673725525, -0.91706750820117, 0.41088121270189,
-0.78430313474907, -0.409059258341318, -0.975516971079657, -1.27140585793008,
-0.198796133331597, -0.429336908924874, -0.0174222425394045,
-0.161004481467468, -0.405810552877399, -0.370285632509627, -0.89866639987887,
1.10645284167993, 1.8809570525936, 1.92254222169752, -0.0950224222539316,
1.58853988090432, 1.78386607934181, -0.817728723571643, 0.459412598781715,
1.423493404365, 1.7768774695448, -1.12006569075839, -0.751966514798299,
-0.488043380249217, -1.11596293893295, 0.273653921626916, -1.30713710349688,
-0.886375551803829, 0.0239199838971528, -1.3593629685693, -0.643843217883837,
-0.281272478019681, -1.30622537314287, -1.33112371265123, -0.909101703993645,
-1.12755493783614, -1.40003719660867, -0.290337728601771, -0.28987232858238,
-1.19056584307422, -0.193624949117038, 0.975332436429838, -0.218160046225909,
-0.132462974128803, 1.39468641578899, -0.302150834825655, -0.107400685186729,
1.125970810338, -0.256092735038377, 0.158506693687128, -0.378339394736207,
-0.349357413758689, 0.803679188551612, -0.146048595417096, -0.286845588476566,
0.158259418849209, -0.323113138916157, 0.108646496285904, -0.771878347558138,
0.0273033684358348, 0.0453235133932348, 1.28361673646552, 1.29073622845612,
1.24630790251793, 0.944986804400661, 0.981991016697687, 1.3473053430083,
0.827378725150942, 2.48208427225106, 2.69830294706435, 1.03039606923181,
2.27492560926846, 1.10744827135352, 1.35324680735362, 1.84388638700427,
1.52873550347441, 1.40071898822651, 0.260928795100606, -1.16734754082697,
-0.878145608518404, -0.00331768573206932, -0.910939824808822,
0.125144947309071, -1.67327192782442, -0.531813491778335, -0.546522958404239,
-0.0196439100156154, 0.0745476695275746, 0.794370508556172, -0.210218336601184,
0.445705743628815, 0.0718198426307307, -0.122444182892431, 0.184211311538213,
0.183194361394073, 1.12410771197491, -0.741883323524288, 0.716760255429898,
1.76025647136123, -0.00419702451761512, 1.25254232235371, -0.880576082155036,
-0.00114985682377106, 1.78721959263295), ID = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L), .Label = c("BLA14",
"BLA2", "BLA20", "BLA209", "BLA21", "BLA211", "BLA238", "BLA24",
"BLA25", "BLA283", "BLA307", "BLA31", "BLA42", "BLA47", "BLA5",
"BLA79", "BLA80"), class = "factor"), Time = structure(c(2L,
5L, 8L, 3L, 6L, 1L, 4L, 7L, 2L, 5L, 8L, 11L, 3L, 6L, 9L, 1L,
4L, 7L, 10L, 2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L, 2L, 5L, 8L, 11L,
3L, 6L, 9L, 1L, 4L, 7L, 10L, 2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L,
2L, 5L, 3L, 1L, 4L, 2L, 5L, 8L, 11L, 3L, 6L, 8L, 1L, 4L, 7L,
10L, 2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L, 5L, 8L, 11L, 2L, 3L, 6L,
9L, 4L, 7L, 10L, 1L, 2L, 5L, 3L, 1L, 4L, 2L, 5L, 8L, 3L, 6L,
1L, 4L, 7L, 2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L, 2L, 5L, 3L, 1L, 4L,
2L, 5L, 8L, 11L, 3L, 6L, 9L, 1L, 4L, 7L, 10L, 2L, 5L, 8L, 3L,
6L, 1L, 4L, 7L, 2L, 5L, 8L, 11L, 3L, 6L, 9L, 1L, 4L, 7L, 10L,
2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L), .Label = c("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11"), class = c("ordered", "factor"
))), class = "data.frame", row.names = c(NA, -145L))
here is my model:
#model
clmm(Spawn_ID ~ average_Mg + average_Mn + average_Zn + (1|ID/Time), data = data1)
warning message
Warning messages:
1: Using formula(x) is deprecated when x is a character vector of length > 1.
Consider formula(paste(x, collapse = " ")) instead.
2: no. random effects (=161) >= no. observations (=145)

Putting nested random effects in polr function

I am trying to run an ordinal logistic regression mixed model with nested random effects using the polr function from package MASS.
average_Mg, average_Mn, and average_Zn are the predictor variables. the response variable is (Spawn_ID)
I have two random effects that are added into the model "(1|Time/ID)" (time is nested into ID and each ID has a different number of time because some ID's have more observations than other IDs) (see picture)
enter image description here
data:
data1 <- structure(list(Spawn_ID = structure(c(1L, 1L, 1L, 2L, 2L, 3L,
3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L,
2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 3L, 3L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 2L, 3L, 3L,
1L, 1L, 1L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L,
1L, 1L, 2L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L), .Label = c("1",
"2", "3"), class = c("ordered", "factor")), average_Mg = c(0.0841567034686979,
0.00262726492114602, 0.353259164624795, -0.0364882169624394,
0.355390763410209, -0.151476304441742, -0.0936982567875358, 0.162382425722223,
-1.42681140542971, -1.07244649331608, -0.781118702245109, -1.21952099438996,
-1.38524008095868, -0.898983285649544, -1.01088138645042, -1.56153946055641,
-1.21949786511762, -0.887755361220044, -1.28496187594515, -1.82368718501736,
-1.44293956646485, -1.55766693069354, -1.58965315500885, -1.44731801677922,
-1.90014469879122, -1.39433781118039, -1.3954911611769, -0.866262522744268,
-0.893780676281797, -1.13725619637354, -1.50364564206296, -0.845596282408769,
-0.911535321390588, -1.41202503488084, -1.16477711028459, -0.928588557438047,
-1.0717825099406, -1.27071094552779, -0.102981887484371, -0.419552015986426,
0.207549199784127, -0.26688619247063, -0.0285388879140084, -0.104483586190019,
-0.374343912017509, -0.079286617457435, 0.502081027554914, 0.617397464912897,
0.658216645632926, 0.426442596169416, 0.437015933595451, -1.09407187513834,
-0.289722984650423, 0.189585827711078, 0.388488397202216, -0.814972632964376,
0.0191088328270176, -0.0788536189757766, -1.01469763732677, -0.532896761096259,
-0.319455285766885, 0.32814415425849, -1.10320849618017, -0.802227061565149,
-0.540984260212337, -1.14703007008055, -0.482835715257794, -1.23114539323637,
-0.881874890913403, -0.479104993907372, 0.722375571708869, 0.890893182481924,
1.1899980340988, 0.784338248057351, 0.6979419698913, 1.10833467622332,
0.749554401158789, 0.761841531876783, 1.38243732271989, 1.38537086804658,
0.686397638698291, 1.38660240844063, 1.90358175564482, 1.30513882860368,
0.9706833889022, 2.02786238069347, 0.633278771850179, 0.773554195390702,
1.26407209402018, 0.711780226990467, 1.12404623953355, 0.772411304871684,
0.746907192859431, 1.26546029276059, 0.754582077531832, 0.97865102792368,
1.25739249978455, 1.00030371910859, 1.20251423376581, 0.508886239812359,
1.0614400765502, 1.15560112394629, 0.810899892639843, 0.864356170995008,
1.0722853284304, 0.459017471399662, 0.622305015414975, 0.654778017554924,
0.630012469467092, 1.66357555743707, 1.51486425579301, 0.468256912570358,
1.46986769298999, 0.842853031161864, 0.527443923164085, 0.878231897515369,
1.01564664723517, 0.548373164352724, -1.54705977070176, -1.7628880927376,
-1.93886600741023, -1.75280825324115, -1.06329603003556, -1.76583856532739,
-2.03620478132805, -1.37852741943318, 0.491445103158986, 0.579237889782203,
0.581147814257234, 0.587993370694159, 0.673660535135936, 0.773224639425602,
0.472056000685565, 0.803037596940575, 0.686349802316703, 0.67297390357697,
0.877084884098423, 0.116853127650954, 0.633207695175741, 0.475407810726902,
0.410454398351338, 0.761825383439366, 0.049981065597767, 0.352528868363907,
1.08494544768163), average_Mn = c(0.550041395336084, 0.106013801445048,
0.195501740474326, 0.443055327801251, 0.166039412117922, 0.306826485641131,
0.0779488541952551, 0.253041943200378, -0.398175664467298, -0.248653116953824,
1.62701418452763, 2.12294900204613, -0.653505561079324, 1.18649551151282,
1.73706605332464, -0.663237964024895, -0.502022515338005, 1.37488148179862,
1.52730443891077, -1.02791508823558, -0.912000741847277, -1.13836183212639,
-1.0203769070181, -0.944729930532721, -1.08022080193656, -0.798637523498975,
-0.892966674264699, -0.106867747759299, -0.446077807686831, 0.260985129778798,
0.34207999245625, -0.256052324398952, -0.455901687059591, -0.124589605852459,
-0.330899017708168, -0.155941754430574, 0.179724557145222, -0.13333037309261,
-0.467033378092841, -1.02354293597083, -0.324678510862249, -0.780338971498965,
-0.638323663037884, -0.833876759864611, -1.23290374823043, -0.791651926017789,
-0.507461130613381, -0.8368428812325, -0.0557361418099642, -0.804531913988396,
-0.704346938462444, -0.483778753666885, 0.393712753506288, 0.427715184816051,
0.479775271348421, -0.28130811769396, 0.485295306906789, 0.29873238753337,
-0.32311305698219, -0.153688760742991, 0.566586154995381, 0.432231338528875,
1.06596225482086, 1.35634853919191, 3.80694326707882, 0.810475117293976,
3.0387608847462, 0.633718464176746, 1.1751890651466, 3.9845446592986,
-1.37297875557724, -1.09420268685371, -0.962614525736854, -1.29805138281804,
-1.31055976157738, -0.925719147858883, -1.23243978183704, -1.33945462492742,
-0.733417598434015, -0.930363022530595, -1.27774840900127, -0.551645520494324,
1.07748304807901, -0.396816101914801, -0.619403529151145, 0.904015824338447,
-0.287220128038401, -0.413048098445259, 0.612345039773056, 0.12520419272066,
0.144889119165393, -0.376872327860885, -0.371321461123647, 0.713414294431202,
-0.220319649249486, 1.91457944140036, 1.96765430981412, -0.121347747943895,
1.83594114293694, -0.874846076775421, 1.57970089137696, 1.93625153606136,
0.346364402583701, 1.77363591575721, 0.715776292044604, -0.11504600156397,
0.957194866002839, -0.887101387136546, -0.780049232064872, -0.336706490132965,
-0.70438883179631, -0.873817482659086, -0.284434200328209, -0.568305044660584,
-0.775993306095371, -0.771770658609636, -0.383838373137703, -0.811970593682299,
-0.73465457432939, 0.0911351344017551, 1.2707682140586, -0.777318831552788,
1.11134230637355, -0.783796841885501, 0.259370669933754, 1.73241147006831,
-0.725970777900951, -0.438118569726494, -0.278337133147783, -0.493758812846972,
-0.518179622331699, -0.389789488287258, -0.298381997017347, -0.612423478602631,
-0.429659970678406, -0.292999247051666, -0.435183828919258, -0.897633859213429,
0.507974413224703, 1.43201049921323, -0.66047577071763, 0.786340467145023,
-0.95656045144662, -0.132752976801546, 1.18466694854915), average_Zn = c(0.199618761426643,
0.591792310386536, 1.3751346661716, 0.332582639073901, 0.560402369414685,
0.11570820588524, 0.192150397195304, 1.39288706671957, -0.394083581015067,
-0.0798076017654593, 1.17669193020793, 0.607211261125105, -0.452359206037806,
0.771410079352111, 0.494544784665782, -0.625267178823271, -0.39226745167263,
1.16595745311139, 0.395578830413474, -1.34293822466577, -0.742961944779728,
-1.55476909975203, -1.21117698813619, -0.705958753206111, -1.37118754349972,
-0.917928244368175, -0.974434964905969, -1.01929394953207, -1.12532371386981,
-1.22700631035692, -1.46133511017614, -0.952449170432, -1.21027641358059,
-1.3226625940125, -1.16430485224588, -1.08770522025381, -1.18121144700268,
-1.40033431712296, -0.618587673725525, -0.91706750820117, 0.41088121270189,
-0.78430313474907, -0.409059258341318, -0.975516971079657, -1.27140585793008,
-0.198796133331597, -0.429336908924874, -0.0174222425394045,
-0.161004481467468, -0.405810552877399, -0.370285632509627, -0.89866639987887,
1.10645284167993, 1.8809570525936, 1.92254222169752, -0.0950224222539316,
1.58853988090432, 1.78386607934181, -0.817728723571643, 0.459412598781715,
1.423493404365, 1.7768774695448, -1.12006569075839, -0.751966514798299,
-0.488043380249217, -1.11596293893295, 0.273653921626916, -1.30713710349688,
-0.886375551803829, 0.0239199838971528, -1.3593629685693, -0.643843217883837,
-0.281272478019681, -1.30622537314287, -1.33112371265123, -0.909101703993645,
-1.12755493783614, -1.40003719660867, -0.290337728601771, -0.28987232858238,
-1.19056584307422, -0.193624949117038, 0.975332436429838, -0.218160046225909,
-0.132462974128803, 1.39468641578899, -0.302150834825655, -0.107400685186729,
1.125970810338, -0.256092735038377, 0.158506693687128, -0.378339394736207,
-0.349357413758689, 0.803679188551612, -0.146048595417096, -0.286845588476566,
0.158259418849209, -0.323113138916157, 0.108646496285904, -0.771878347558138,
0.0273033684358348, 0.0453235133932348, 1.28361673646552, 1.29073622845612,
1.24630790251793, 0.944986804400661, 0.981991016697687, 1.3473053430083,
0.827378725150942, 2.48208427225106, 2.69830294706435, 1.03039606923181,
2.27492560926846, 1.10744827135352, 1.35324680735362, 1.84388638700427,
1.52873550347441, 1.40071898822651, 0.260928795100606, -1.16734754082697,
-0.878145608518404, -0.00331768573206932, -0.910939824808822,
0.125144947309071, -1.67327192782442, -0.531813491778335, -0.546522958404239,
-0.0196439100156154, 0.0745476695275746, 0.794370508556172, -0.210218336601184,
0.445705743628815, 0.0718198426307307, -0.122444182892431, 0.184211311538213,
0.183194361394073, 1.12410771197491, -0.741883323524288, 0.716760255429898,
1.76025647136123, -0.00419702451761512, 1.25254232235371, -0.880576082155036,
-0.00114985682377106, 1.78721959263295), ID = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L), .Label = c("BLA14",
"BLA2", "BLA20", "BLA209", "BLA21", "BLA211", "BLA238", "BLA24",
"BLA25", "BLA283", "BLA307", "BLA31", "BLA42", "BLA47", "BLA5",
"BLA79", "BLA80"), class = "factor"), Time = structure(c(2L,
5L, 8L, 3L, 6L, 1L, 4L, 7L, 2L, 5L, 8L, 11L, 3L, 6L, 9L, 1L,
4L, 7L, 10L, 2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L, 2L, 5L, 8L, 11L,
3L, 6L, 9L, 1L, 4L, 7L, 10L, 2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L,
2L, 5L, 3L, 1L, 4L, 2L, 5L, 8L, 11L, 3L, 6L, 8L, 1L, 4L, 7L,
10L, 2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L, 5L, 8L, 11L, 2L, 3L, 6L,
9L, 4L, 7L, 10L, 1L, 2L, 5L, 3L, 1L, 4L, 2L, 5L, 8L, 3L, 6L,
1L, 4L, 7L, 2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L, 2L, 5L, 3L, 1L, 4L,
2L, 5L, 8L, 11L, 3L, 6L, 9L, 1L, 4L, 7L, 10L, 2L, 5L, 8L, 3L,
6L, 1L, 4L, 7L, 2L, 5L, 8L, 11L, 3L, 6L, 9L, 1L, 4L, 7L, 10L,
2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L), .Label = c("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11"), class = c("ordered", "factor"
))), class = "data.frame", row.names = c(NA, -145L))
Current code for model
model2<- polr(Spawn_ID ~ average_Mg + average_Mn + average_Zn + (1| ID/Time), data = data1, method = "logistic", Hess = TRUE)
error message
Error in family$linkfun(mustart) :
Argument mu must be a nonempty numeric vector
In addition: Warning messages:
1: In eval(predvars, data, env) :
Incompatible methods ("Ops.factor", "Ops.ordered") for "/"
2: In Ops.factor(1, ID/Time) : ‘|’ not meaningful for factors

How to reorder x-axis based on y-axis values in R ggplot2

I am trying to reorder (I don't mind whether it is ascending or descending order) the x-axis on my errorplot based on the mean values of the y-axis. I have applied a solution based on this post, however for some reason it seems to be ignoring the reorder command. Any ideas what is happening here?
#Import data.
df <- structure(list(X_Variable = c(4L, 4L, 13L, 18L, 12L, 3L, 15L,
NA, 18L, 4L, 17L, NA, 3L, 15L, 4L, 6L, 12L, NA, 2L, 1L, NA, 15L,
1L, 6L, 1L, 12L, NA, 6L, NA, 15L, NA, 1L, 7L, 15L, 11L, NA, NA,
1L, 1L, 7L, 2L, 2L, 12L, 11L, 15L, 17L, 1L, 4L, 11L, 15L, 2L,
3L, 13L, 17L, 15L, 6L, 3L, 14L, 12L, 8L, 12L, 11L, NA, 2L, 11L,
NA, 4L, 8L, 15L, 4L, 7L, 8L, 15L, 15L, 15L, 6L, 3L, 6L, 8L, 15L,
4L, 2L, 1L, 1L, 7L, 17L, 15L, 1L, NA, 5L, 13L, 1L, 15L, 4L, 15L,
13L, 18L, 1L, 15L, 6L, NA, 6L, NA, 6L, 1L, 16L, 4L, 1L, NA, 2L,
12L, NA, 7L, 2L, 15L, 13L, 13L, 16L, NA, 7L, 2L, 4L, 15L, 11L,
15L, 2L, 5L, 13L, 2L, 9L, 7L, 6L, 15L, 15L, 11L, 3L, 15L, 13L,
NA, 4L, 8L, NA, 4L, 8L, 18L, 4L, 1L, 8L, 5L, 18L), Y_Variable = c(6L,
4L, 5L, 4L, 4L, 3L, 7L, 1L, 1L, 7L, 4L, NA, 5L, 1L, 6L, 1L, 6L,
3L, 6L, 4L, NA, 4L, 6L, 5L, 1L, 4L, 1L, 1L, 6L, 3L, 4L, 1L, 1L,
2L, 3L, 4L, 4L, 2L, 2L, 2L, 4L, 1L, 1L, 5L, 4L, 1L, 4L, 4L, 3L,
3L, 2L, 2L, 1L, 3L, NA, 2L, 4L, 1L, 2L, 2L, 6L, 3L, NA, 2L, 2L,
NA, 4L, 2L, 3L, 6L, 5L, 4L, 1L, 5L, 3L, 1L, 4L, 6L, 1L, 5L, 4L,
2L, 1L, 5L, 4L, 3L, 2L, NA, 4L, 2L, NA, 4L, 5L, 5L, 4L, 2L, 1L,
5L, 2L, 2L, 4L, 1L, 4L, 1L, 5L, 2L, 1L, 3L, NA, 2L, 2L, 2L, 5L,
1L, 1L, 4L, 2L, 2L, NA, 3L, 5L, 7L, 1L, 1L, 1L, 1L, 4L, 1L, 2L,
2L, 3L, 3L, 3L, 4L, 1L, 4L, 3L, 4L, 3L, 6L, 1L, 5L, 4L, 2L, 5L,
2L, 3L, 1L, 1L, 2L)), row.names = c(NA, -150L), class = "data.frame")
#Error plot ordered by Y-Variable.
ggplot(df, aes(x=reorder(X_Variable, Y_Variable, FUN=mean), y=Y_Variable))+
geom_point(stat="summary", fun.y="mean")+
geom_errorbar(stat="summary", fun.data="mean_se", fun.args=list(mult=1.96), width=0.1)
I only removed missing values first. The minus sign works fine on my machine.
df1<-df %>%
filter(!is.na(X_Variable), !is.na(Y_Variable))
ggplot(df1, aes(x=reorder(X_Variable, -Y_Variable, FUN=mean), y=Y_Variable))+
geom_point(stat="summary", fun.y="mean")+
geom_errorbar(stat="summary", fun.data="mean_se", fun.args=list(mult=1.96), width=0.1)
Edit: Because of missing values, X_Variable 1, 13, and 15 ranked last. Hope this helps.
df %>% group_by(X_Variable) %>%
summarise(
Y_Variable = mean(Y_Variable)) %>%
arrange(Y_Variable)
# A tibble: 18 x 2
X_Variable Y_Variable
<int> <dbl>
1 4 4.71
2 3 3.67
3 12 3.57
4 7 3.29
5 17 2.75
6 18 2.6
7 11 2.57
8 2 2.55
9 5 2.33
10 6 2.3
11 9 2
12 16 2
13 8 1.86
14 14 1
15 1 NA
16 13 NA
17 15 NA
18 NA NA
>

Merge and edit multiple legends when facets and geom_line are plotted separately

Following this guide I have plotted the following graph using the following code. I did split my dataset into one that contains the data that goes in all plots 'control', and the rest 'dfnocontrol'.
ggplot(dfnocontrol,aes(y=value,x=year)) + geom_line(data=dfnocontrol,
aes(color=survivorship),size=1.5) + facet_wrap(~density,nrow=2) +
geom_line(data=dfcontrol,aes(linetype=simulname),color='grey',size=1.5)
I have tried many ways to have only one legend, or to edit the existing two legend but nothing seems to work. scale_fill_manual() seems to be ignored, even though I don't get any error message. I was forced to use linetype to make the 'control' appear in the legend. How can I merge these two legends?
edit: these are the data for control
structure(list(year = 1:2, psize = structure(c(6L, 6L), .Label = c("all plants",
"all plants no-seedl", "seedlings", "SmallerT10", "SmallerT10 no-seedl",
"LargerT10", "10-30", "30-50", "50+"), class = "factor"), value = c(392.884450281975,
392.76842677951), simulname = structure(c(1L, 1L), .Label = c("control",
"d02s70", "d02s80", "d02s90", "d05s70", "d05s80", "d05s90", "d07s70",
"d07s80", "d07s90", "d1s70", "d1s80", "d1s90", "d2s70", "d2s80",
"d2s90", "d3s70", "d3s80", "d3s90", "d4s70", "d4s80", "d4s90",
"d5s70", "d5s80", "d5s90"), class = "factor"), survivorship = structure(c(1L,
1L), .Label = c("control", "s70", "s80", "s90"), class = "factor")), .Names = c("year",
"psize", "value", "simulname", "survivorship"), row.names = 2501:2502, class = "data.frame")
and data for the rest
structure(list(year = c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L), psize = structure(c(6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("all plants",
"all plants no-seedl", "seedlings", "SmallerT10", "SmallerT10 no-seedl",
"LargerT10", "10-30", "30-50", "50+"), class = "factor"), value = c(391.933827876557,
390.784233661738, 391.931768654094, 390.777949423224, 391.930831801103,
390.775125884957, 391.904131913644, 390.671681105517, 391.903377880798,
390.669377819171, 391.902842713777, 390.667498067697, 391.874743014214,
390.557893743236, 391.874006362415, 390.555639401299, 391.8735511448,
390.554149478021, 391.84367266143, 390.443618794749, 391.843064602404,
390.442149462261, 391.842594963982, 390.440725187945, 391.72267802326,
388.998242801555, 391.722309813432, 388.996838950063, 391.721745089041,
388.995715149179, 384.967818982887, 383.215849576989, 384.967407490871,
383.214728664341, 384.96689031843, 383.213390281481, 391.897592532656,
389.445606459513, 391.897234485415, 389.444632515097, 391.89681267375,
389.443358475326, 391.402389493961, 388.987279260992, 391.401979078947,
388.985920091544, 391.401583421483, 388.984891027315), simulname = structure(c(2L,
2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L,
10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L,
17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L,
23L, 24L, 24L, 25L, 25L), .Label = c("control", "d02s70", "d02s80",
"d02s90", "d05s70", "d05s80", "d05s90", "d07s70", "d07s80", "d07s90",
"d1s70", "d1s80", "d1s90", "d2s70", "d2s80", "d2s90", "d3s70",
"d3s80", "d3s90", "d4s70", "d4s80", "d4s90", "d5s70", "d5s80",
"d5s90"), class = "factor"), density = structure(c(2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L,
7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("control",
"d02", "d05", "d07", "d1", "d2", "d3", "d4", "d5"), class = "factor"),
survivorship = structure(c(2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L,
3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L,
4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L,
3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L), .Label = c("control",
"s70", "s80", "s90"), class = "factor")), .Names = c("year",
"psize", "value", "simulname", "density", "survivorship"), row.names = c(6081L,
6082L, 9845L, 9846L, 14345L, 14346L, 17985L, 17986L, 21797L,
21798L, 26297L, 26298L, 30567L, 30568L, 34528L, 34529L, 38744L,
38745L, 43144L, 43145L, 47519L, 47520L, 51983L, 51984L, 56483L,
56484L, 60983L, 60984L, 65483L, 65484L, 69983L, 69984L, 74483L,
74484L, 78983L, 78984L, 83483L, 83484L, 87983L, 87984L, 92483L,
92484L, 96983L, 96984L, 101483L, 101484L, 105983L, 105984L), class = "data.frame")
Since you provided no data, I will give you an example using the economics data set.
library(wesanderson) # for the colours
library(tidyverse)
data("economics")
We will need two data sets for this task. Variable unemploy will serve as our 'control' (6th column). All variables will be scaled.
First data set:
economics_gathered <- economics[, 1:5] %>% # exclude unemploy
modify_if(is.numeric, scale) %>%
gather(key, value, -date)
Second data set:
economics_control <- economics[, c(1, 6)] %>%
dplyr::rename(control = unemploy) %>%
gather(some_other_key, value, 2) %>%
mutate(value = scale(value))
Now we can plot:
ggplot() +
geom_line(data = economics_control, aes(x = date, y = value, col = some_other_key)) +
geom_line(data = economics_gathered, aes(date, value, col = key)) +
scale_colour_manual(values = c("grey", wes_palette("GrandBudapest"))) +
facet_wrap(~key, scales = "free_y")
To which the result is the plot below.
EDIT
With the data provided by the OP the following code
ggplot() +
geom_line(data = dfcontrol, aes(year, value, col = survivorship), size = 1.5) +
geom_line(data = dfnocontrol, aes(year, value, col = survivorship), size = 1.5) +
facet_wrap( ~ density, nrow = 2) +
scale_colour_manual(values = c("grey", "forestgreen", "red", "blue"))
gives this plot:
DATA
1)
dfcontrol <- structure(list(year = 1:2, psize = structure(c(6L, 6L), .Label = c("all plants",
"all plants no-seedl", "seedlings", "SmallerT10", "SmallerT10 no-seedl",
"LargerT10", "10-30", "30-50", "50+"), class = "factor"), value = c(392.884450281975,
392.76842677951), simulname = structure(c(1L, 1L), .Label = c("control",
"d02s70", "d02s80", "d02s90", "d05s70", "d05s80", "d05s90", "d07s70",
"d07s80", "d07s90", "d1s70", "d1s80", "d1s90", "d2s70", "d2s80",
"d2s90", "d3s70", "d3s80", "d3s90", "d4s70", "d4s80", "d4s90",
"d5s70", "d5s80", "d5s90"), class = "factor"), survivorship = structure(c(1L,
1L), .Label = c("control", "s70", "s80", "s90"), class = "factor")), .Names = c("year",
"psize", "value", "simulname", "survivorship"), row.names = 2501:2502, class = "data.frame")
2)
dfnocontrol <- structure(list(year = c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L), psize = structure(c(6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("all plants",
"all plants no-seedl", "seedlings", "SmallerT10", "SmallerT10 no-seedl",
"LargerT10", "10-30", "30-50", "50+"), class = "factor"), value = c(391.933827876557,
390.784233661738, 391.931768654094, 390.777949423224, 391.930831801103,
390.775125884957, 391.904131913644, 390.671681105517, 391.903377880798,
390.669377819171, 391.902842713777, 390.667498067697, 391.874743014214,
390.557893743236, 391.874006362415, 390.555639401299, 391.8735511448,
390.554149478021, 391.84367266143, 390.443618794749, 391.843064602404,
390.442149462261, 391.842594963982, 390.440725187945, 391.72267802326,
388.998242801555, 391.722309813432, 388.996838950063, 391.721745089041,
388.995715149179, 384.967818982887, 383.215849576989, 384.967407490871,
383.214728664341, 384.96689031843, 383.213390281481, 391.897592532656,
389.445606459513, 391.897234485415, 389.444632515097, 391.89681267375,
389.443358475326, 391.402389493961, 388.987279260992, 391.401979078947,
388.985920091544, 391.401583421483, 388.984891027315), simulname = structure(c(2L,
2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L,
10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L,
17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L,
23L, 24L, 24L, 25L, 25L), .Label = c("control", "d02s70", "d02s80",
"d02s90", "d05s70", "d05s80", "d05s90", "d07s70", "d07s80", "d07s90",
"d1s70", "d1s80", "d1s90", "d2s70", "d2s80", "d2s90", "d3s70",
"d3s80", "d3s90", "d4s70", "d4s80", "d4s90", "d5s70", "d5s80",
"d5s90"), class = "factor"), density = structure(c(2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L,
7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("control",
"d02", "d05", "d07", "d1", "d2", "d3", "d4", "d5"), class = "factor"),
survivorship = structure(c(2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L,
3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L,
4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L,
3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L), .Label = c("control",
"s70", "s80", "s90"), class = "factor")), .Names = c("year",
"psize", "value", "simulname", "density", "survivorship"), row.names = c(6081L,
6082L, 9845L, 9846L, 14345L, 14346L, 17985L, 17986L, 21797L,
21798L, 26297L, 26298L, 30567L, 30568L, 34528L, 34529L, 38744L,
38745L, 43144L, 43145L, 47519L, 47520L, 51983L, 51984L, 56483L,
56484L, 60983L, 60984L, 65483L, 65484L, 69983L, 69984L, 74483L,
74484L, 78983L, 78984L, 83483L, 83484L, 87983L, 87984L, 92483L,
92484L, 96983L, 96984L, 101483L, 101484L, 105983L, 105984L), class = "data.frame")

Showing data.frame as table or matrix in R

I want to show the following data.frame
df <- structure(list(Variety = structure(c(2L, 3L, 4L, 5L, 6L, 7L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L), .Label = c("F2022",
"F9917", "Hegari", "JS2002", "JS263", "PC1", "Sadabahar"), class = "factor"),
Priming = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 1L), .Label = c("CaCl2",
"Dry", "Hydro", "KCL", "KNO3", "NaCl", "Onfarm"), class = "factor"),
Letters = structure(c(1L, 3L, 10L, 11L, 10L, 19L, 27L, 5L,
28L, 11L, 18L, 20L, 9L, 1L, 22L, 14L, 30L, 26L, 24L, 3L,
22L, 9L, 16L, 10L, 15L, 25L, 6L, 7L, 17L, 30L, 18L, 13L,
20L, 27L, 19L, 29L, 23L, 2L, 8L, 12L, 6L, 31L, 8L, 22L, 4L,
32L, 21L, 33L, 2L), .Label = c("a", "at", "bcd", "bclq",
"bcq", "bd", "bds", "chlq", "ds", "e", "efg", "efgmnor",
"efgnor", "efgnr", "efgr", "eg", "fgmnor", "fmnor", "hijkl",
"hijkp", "hikl", "hklq", "ijkmp", "ijmop", "jmop", "mno",
"mnop", "mnor", "su", "t", "uv", "v", "w"), class = "factor")), .Names = c("Variety",
"Priming", "Letters"), row.names = c(NA, -49L), class = "data.frame")
as Table or matrix with Ordered Variety names along rows and Ordered Priming names along columns and showing Letter column in the main body of the table in R.
I could not figure out how to do this. Any help will be highly appreciated. Thanks in advance.
This should do it.
d <- d[order(d$Variety,d$Priming),]
dw <- reshape(data = d, idvar = 'Variety', timevar = 'Priming', direction = 'wide')
dw
You might want to edit the column names.
names(dw) <- gsub('Letters.', '', names(dw), fixed = TRUE)
Simple one
library(reshape2)
acast(data=df, formula=Variety~Priming)
CaCl2 Dry Hydro KCL KNO3 NaCl Onfarm
F2022 at mnop a hklq bds hijkl uv
F9917 a bcq hklq ds fgmnor su chlq
Hegari bcd mnor efgnr eg t ijkmp hklq
JS2002 e efg t e fmnor at bclq
JS263 efg fmnor mno efgr efgnor chlq v
PC1 e hijkp ijmop jmop hijkp efgmnor hikl
Sadabahar hijkl ds bcd bd mnop bd w

Resources