I have a data frame which looks like:
# A tibble: 200 x 7
status logTA logSALES logTA_PredBrkDwn logSALES_PredBrkDwn Model Model_PredBrkDwn
<int> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 0 11.6 12.2 -0.621 -0.710 2 2
2 0 NA NA 0.182 0.102 3 3
3 0 10.0 9.74 -0.519 -1.66 4 4
4 1 14.8 14.5 0.605 0.566 2 2
5 0 11.5 NA -0.451 -0.0159 1 1
6 0 10.1 12.3 -1.44 -0.438 1 1
Where it has logTA
and a corresponding logTA_PredBrkDwn
variable. The same goes for logSALES
and logSALES_PredBrkDwn
. It also has a status
which is just a binary variable indicating its state and a Model
which indicates 4 different models
I can plot the logTA
and the corresponding logTA_PredBrkDwn
using:
ggplot(d, aes(x = logTA, y = logTA_PredBrkDwn, colour = factor(status), alpha = factor(status))) +
geom_point(size = 1, na.rm = TRUE) +
geom_smooth(method = "loess", span = 0.8, se = TRUE)
However, I would like a plot for each of the variables, that is another plot for logSALES
and logSALES_PredBrkDwn
. So I thought about using pivot_longer
and then using facet_wrap
.
d %>%
filter(Model == 1) %>%
pivot_longer(cols = contains("PredBrkDwn"), names_to = "PredBrkDwnNames", values_to = "PreBrkDwnValues")
The above code does not work correctly. I am thinking about using 2 pivot_longer
one of the logTA
and logSALES
variables and another for the logTA_PredBrkDwn
and logSALES_PredBrkDwn
variables, then using some sort of facet_wrap
to greate the plot for each of the combinations of logTA
and logTA_PredBrkDwn
and then next to it another for logSALES
and logSALES_PredBrkDwn
.
Data:
d <- structure(list(status = c(0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L,
0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L,
0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L),
logTA = c(12.8475790617222, 14.5208154307888, 15.8107868601073,
12.0650734380581, 12.8448586727372, 12.818248353915, 15.3304672269314,
12.7881877016673, 12.6508898684145, 11.4093664434682, 10.3089193267554,
10.9411814262348, NA, 12.6556598966732, 10.4120205502422,
16.5605498522708, 13.0913782584566, 13.0728336339885, 8.72761617832107,
10.323841273138, 14.6250486863629, 13.5880355836084, 14.8730738469987,
11.8713618566486, 14.7714678792207, 10.800819700809, 13.4839351124678,
11.4291198531112, 17.6396601061174, 12.842321378456, 12.9077202437971,
9.90823576283617, 11.4285362995625, 14.0470388588129, 12.0195590602098,
10.3718961604113, 12.9960561135269, 14.6976594334298, 13.1834915456221,
NA, 13.7929550850632, 12.7183661913712, 10.9789676771472,
12.3232703094456, 13.4795062118652, 14.1039458466775, NA,
13.0609261755341, 10.7244341040279, 12.1752731739766, 16.6141677433276,
13.9224751926888, 13.9943625301187, 7.98454632985877, 12.7936023186641,
12.4602017894694, 13.1511683207725, 13.2742635266864, 10.2836408996281,
12.8933400741777, 10.9684568767816, 11.7003838058693, NA,
12.1824626635176, NA, 13.9657289450239, 17.0401695962355,
13.9116732634018, 16.0103672228963, 11.7945794046837, 9.95270619632363,
12.9274038974142, 15.8385052854982, 14.2415495173029, NA,
13.8079208286975, 14.5565713281382, 12.1373002146831, 10.5174832947906,
11.1864887040921, 11.9135656183494, 13.8491870843237, NA,
12.2108754828642, 10.5551093662675, 13.7825936937385, NA,
12.034216138584, 13.9609666923954, 13.5162901744859, 13.3657000679903,
12.057172325079, 12.8149465153553, 12.0360610098032, 13.2558536387759,
NA, 15.9413153927714, 13.0575736153231, 11.0328876613566,
12.9747539237005, 12.5400703945592, 10.5562548677332, NA,
11.7902920148945, 11.7669706059037, 13.0100431434393, 10.2006247837587,
14.0511722296455, 13.6729222752495, 13.4208583672703, 12.6080946466284,
15.4923923294515, 13.0218488785944, 11.514071585102, 11.1227138580308,
14.528591239609, 13.3334683550032, 12.5516709695903, 14.3365890778588,
11.6968041421561, 14.9275972745735, 19.3539623184646, 14.924217721494,
14.4455411393097, 14.2732150846006, 12.7781428414583, 17.0612140315812,
13.2745016595369, 12.746449554413, 15.0374858408787, 11.7164360962537,
10.7046703939794, 11.1506778798116, 15.3364812881046, 12.2902575317556,
10.726917103359, 10.5800215648855, 11.9633830589972, 15.4179214385401,
12.7255269718133, 16.9489271114416, 14.1184913643273, 13.0934308390186,
12.9701024094828, 11.5250911618691, 14.6600738239113, 11.6199935219492,
11.4868894504194, 12.0370029686193, 13.4665237631814, 10.7237245044372,
10.1302246393605, 11.4280044540379, 11.2709303339707, 11.1802041847082,
10.5869783330692, 16.4631305029143, 11.0630538205412, 12.9238319476363,
12.5435622216289, 12.3892771457482, 12.4311452437038, NA,
NA, 11.7457500039373, 14.3036014961492, 13.2696648220313,
12.9957610660107, 11.6142291515348, 13.2216637675385, 10.3626198018932,
12.7782576799127, 13.1518270294882, 10.7982481658599, 13.7572117293927,
10.4188528563442, 12.2163140710302, 17.8043169776427, 16.5151463234555,
13.5386378532284, 11.1090241335257, 17.5883256317764, 13.0705015003517,
14.949306386991, 13.4394022899571, 10.4090694519791, 13.3466342930046,
13.574122839324, 15.0120499024583, 15.8996499014428, 11.6300333229805,
13.2526199700818, 10.2559034395545, 12.9844245721043, 10.9671805304081,
12.7206150623412, 12.314500950706, NA, 12.2621948481932,
13.308153910337), logSALES = c(13.714445756626, NA, 13.1649416645353,
11.8083078140482, 13.0016116896575, 13.3832295972862, 16.282872614178,
12.5404462529975, 12.6812037318714, 10.9759060747672, 11.6924180239206,
12.5196892420078, NA, 12.9402178051242, 11.4403010072492,
NA, 11.6157527747697, 13.4417104655174, 6.90775527898214,
10.7292188021513, 15.4030587674237, 14.5748337255547, 15.4340459010939,
12.4712866916637, 15.6907493583043, 12.1650995979353, 13.5563809563297,
11.8661705336058, 14.0676479200574, 13.3960631515356, NA,
NA, 13.1152637684455, 12.5540392819853, 8.2870036878927,
12.0516677667304, 12.968902126533, NA, 10.6634788234035,
NA, 12.5325378354042, 10.0500955721722, 12.1897145789174,
NA, 15.0454694794062, 14.5374657735737, NA, 12.5092059800405,
13.0390600262726, 12.3614811054489, 13.5583843520177, NA,
13.4198250302104, NA, NA, NA, NA, 10.9813357518515, 11.200739262642,
11.7767348386105, 9.34522085417, 12.8717946710037, NA, 12.5665910401802,
NA, 12.9142635989597, 16.4022138804725, 15.2563622702192,
12.3800259526536, 12.5709414280974, 9.87842612194076, 13.1433770358121,
15.1090830866169, 13.3560278765242, NA, 14.4165029751531,
15.2438967470246, NA, 11.982985342634, 11.3092781214615,
14.1964971638921, 13.5896809022997, NA, 13.0527835491631,
11.2427951224317, 13.8856959181539, NA, 12.0581815005407,
14.3881926092683, 14.017432924305, 12.4172205357286, 12.4919618617753,
12.5253006222977, 10.6583881293716, 14.0588470026671, NA,
16.2371314127367, NA, 11.4694123526385, 14.0526785060809,
13.4226679498573, 9.65395849573819, NA, 11.7337463209148,
12.5972427316016, 12.9211011737023, 10.9289196436627, NA,
13.5599781034481, 12.8632955349, 8.62042492809475, 15.8497852632871,
10.2398883580348, 10.9356447558682, 11.0421437164574, 15.2718525341335,
NA, 13.695483005263, 13.6687609905484, 11.6243952807922,
4.53057071917427, 10.8197782844103, 15.3995801996024, 15.1138260742484,
NA, 12.2733880146684, 15.1012617160277, 14.2388296541451,
11.8483541564009, 12.9481004623573, 10.4716293883697, 9.95485735528184,
11.0582596084144, NA, 13.5907845051548, 10.9542217609598,
10.1518195152316, 12.073791899093, 14.030782408576, 13.6753506552076,
8.62025646188694, 14.0661383112483, 10.9275020054972, 13.6867067669474,
10.6600319782259, 14.5680918231783, NA, 12.9605759450032,
12.0000777521392, 11.6000881897874, 11.4268626953812, 9.84786912877312,
10.0606416160071, 12.2710454473542, 12.0009961476473, 10.5775234245737,
15.5085906862357, 12.134995460513, 11.6610178302135, 13.4853759957904,
12.5014475521107, 12.4210603222828, NA, NA, 12.2948067045174,
14.2297550145636, 14.1320361857172, 13.0843333151449, 12.1135391271253,
14.0783316411511, 11.6900167007846, 12.9770554846369, 12.5023885886221,
11.1189935926757, 12.0950405211073, 11.0413526051259, 11.3864866297306,
16.8479554983048, 14.8511234071674, 10.0613456371517, 12.8858635265051,
17.8199218796229, 13.5912138420112, 14.445294059559, 13.2233736444369,
7.83241092718792, 12.5315669878214, 12.6015951560886, 11.982929094216,
NA, 11.2624056949734, 13.8336568909219, 10.9845297200108,
12.9407000039602, 12.6779931863264, 14.4088633983127, NA,
NA, 11.5410766594924, 9.11602969250494), logTA_PredBrkDwn = c(-0.101496651987439,
0.591703992535952, 1.03180207043408, -0.558042122336837,
-0.300038379637219, -0.149290761060978, 0.356887682917734,
-0.460144924370243, -0.215240943100375, -1.23083058029353,
-0.850335561140856, -1.11116070156259, 0.0136911344031747,
-0.471029886715433, -1.11397785782108, 1.07991881807399,
-0.18451674317913, -0.131960938144285, -0.202272369540433,
-1.03884727485993, 0.447554643487055, 0.223237244499198,
0.237009425540942, -0.616108453457669, 0.682453279531904,
-0.429639177967195, 0.0239207700144146, -0.328871943659939,
0.923390598772321, -0.374012939848932, -0.321622355170667,
-0.55791612636378, -0.622815654738528, 0.396568463925676,
-0.377489532953308, -1.16373640573116, -0.470385485758385,
0.233297315916686, -0.34244859737257, 0.0136911344031747,
-0.0744787361467234, -0.241611240348782, -0.730345847516916,
-0.451295485904857, 0.0635369048341919, 0.478562890938516,
0.182226186693977, -0.115433875814312, -0.549863775606285,
-0.451806970014141, 0.788648919958513, 0.0658931147157641,
0.0662033060079226, -0.428500706312742, -0.45302466595941,
-0.451295485904857, -0.474021132067101, -0.0379468364262901,
-1.47545305548467, -0.184021557801413, -0.493111416578397,
-0.424330927019306, -0.257132226104062, -0.587049223683898,
0.0136911344031747, 0.356719409135057, 0.956808717972553,
0.377876490100509, 1.45812011141026, -0.82523248008551, -0.531695177415255,
-0.381330814406067, 0.847133020222195, 0.474412066402392,
0.182226186693977, -0.11364192296656, 0.359244031623438,
-0.634774334792758, -0.983564181547166, -0.732088224069771,
-0.546308988834036, -0.109610542181889, -0.257132226104062,
-0.48639914213802, -1.06909181628747, 0.263060131836441,
0.182226186693977, -0.500795323477605, 0.0864704981070394,
0.111213683279169, -0.0337764995355092, -0.547181318932636,
-0.237026617245618, -0.634121208428417, -0.0668445714997144,
0.0136911344031747, 0.769342987435895, -0.245696548898925,
-0.719224069373896, -0.301596162533944, -0.61006271606478,
-0.914878734594997, 0.0136911344031747, -0.595743358355018,
-0.369299448844719, -0.192711114891742, -0.743947269123888,
0.415517647220715, 0.0203109152481009, -0.0558311505280323,
-0.244739028198224, 0.475799851481556, -0.0676007229464765,
-0.491765050494043, -0.855816635926747, 0.335014207169143,
-0.159030218544361, -0.20177750541736, 0.733588747440007,
-0.484473679894506, 0.824574326563066, 0.727694732669244,
0.577419263795841, 0.233956043141969, 0.661368607504375,
-0.11893708716895, 0.481300509398122, -0.348782288213743,
-0.0789321552102714, 0.957065441625629, -0.548723212157648,
-0.425089839334703, -0.812630619121059, 0.908237539807699,
-0.490919825698387, -1.45526310637547, -1.18216592024081,
-0.578210609968341, 0.461696383534018, -0.303659699638177,
1.43832743321777, 0.2233311012596, -0.0681372706121768, -0.299672733067439,
-0.639965759201325, 0.602192279742355, -0.456004292297173,
-0.411203273351192, -0.479399298779831, 0.0224975974844448,
-0.965184672787183, -0.89108168963521, -1.19921174752371,
-0.665370152992995, -0.734279532446479, -0.976803893507088,
0.520584405048956, -0.635010456719538, -0.150419588930835,
-0.344963370669162, -0.300346739421025, -0.374366392834697,
0.182226186693977, 0.182226186693977, -0.391075882536215,
0.291063428138874, 0.0349623544358961, 0.112840249531784,
-0.841639321641198, -0.321435391951323, -0.840252898423037,
-0.295298642935245, -0.453582768310032, -0.988884750223655,
-0.042545724701179, -0.740535702296745, -0.655842834542631,
0.976345312091351, 0.835167421607719, -0.0482190948326893,
-0.752239845336515, 0.565207294781876, -0.340194709566995,
0.235633315442845, 0.0337915547761563, -0.832890305737971,
-0.00457621508826806, 0.118140724639964, 0.73839558053204,
1.33709980434269, -0.41313896126544, -0.231482884820578,
-1.10266024679411, -0.28939137936737, -0.845941504185419,
-0.342129679999702, -0.56404550416696, 0.182226186693977,
-0.405145432335422, -0.184836001207509), logSALES_PredBrkDwn = c(0.562664166253995,
0.159111080824913, 0.215055741250004, -0.887922762729619,
0.184527151339666, -0.0114008169531114, 0.674760372364353,
-0.187483509763504, -0.284549436267189, -1.26899794005921,
-0.563136621225081, -0.0932118414171999, 0.2950996003791,
0.231056097408658, -0.826211394667378, -0.0594426922936365,
-1.31108863259873, 0.369720475943528, -1.50436775785553,
-0.946337536027807, 0.576795969648447, 0.380772178351129,
0.875722507043325, -0.375680485860456, 0.35292253350487,
-0.239251738323902, 0.409636528788235, -1.142881119437, 0.310154240600919,
0.183308738658033, 0.11926503502196, -0.00035823051660652,
0.23761284920046, -0.343085057797572, -0.793955639759245,
-0.725099398022542, 0.116512644586296, -0.0117951002865566,
-0.699047940842253, 0.2950996003791, -0.5727941143091, -1.64071701002641,
-0.301191899800264, 0.0579275511444296, 0.285959426684324,
0.504496733090727, 0.101501886956213, -0.582346389196667,
-0.162434743036826, -0.804287217978368, 0.0769151745138543,
0.289615210583038, 0.0490086152481568, 0.332078301302787,
-0.20067857763644, 0.0712377697289144, -0.00180358446885875,
-1.63761875860763, -1.12527672806492, -0.962287706356296,
-1.29359766742214, -0.329393553240729, 0.0599510531074726,
-0.313190903191548, 0.2950996003791, -0.389810275261433,
0.52048542045262, 0.325270069519933, -0.738097158016635,
0.0285104467591607, -1.46028913484016, 0.310665922488446,
0.114600517901816, 0.217448000211843, 0.101501886956213,
0.620999872655391, 0.257873128257869, -0.0587692722977717,
-0.809583801275387, -0.824149234056291, 0.651111346357067,
0.175638639485879, 0.0599510531074726, 0.0569794166971493,
-1.43075159474237, 0.260072953370534, 0.101501886956213,
-0.924449650338011, 0.789958260033159, 0.532678135220767,
-0.971467695264441, -0.459475630776942, -0.112090823611161,
-0.93818115647036, 0.718666465294804, 0.2950996003791, 0.132344578603692,
0.0221695069130499, -1.27096849066692, 0.681770495473489,
0.471616403962145, -0.927223261761501, 0.2950996003791, -0.68645585708821,
-0.461594576443344, -0.300518119967886, -0.766525196583324,
-0.256305935755525, 0.191053667493182, -0.315171083468848,
-1.66715032267782, 0.775167312567506, -1.04599881800048,
-1.44507187029957, -0.493153315207868, 0.755680158852787,
-0.170047569210277, 0.571663265592129, 0.19793475974814,
-1.17266035551524, -1.57446377649075, -0.401164858997217,
0.513464579987944, 0.740793233774255, -0.127560581933436,
-0.929994963305284, 0.729363677113513, 0.746854087708903,
-0.918948910555141, 0.0109189701148658, -1.10539022019854,
-1.3423144027067, -1.59917840255014, 0.0216395656487452,
0.330105136687462, -1.40496965730845, -0.884926926762506,
-0.314169412121354, 0.645994131918474, 0.332809341396782,
-1.33534644487775, 0.481107208680634, -1.53682762464541,
0.420463996486976, -0.830693116528634, 0.508324902165282,
0.0012242775398533, -0.448398756844492, -0.223688671701306,
-0.342483462367711, -0.936522059942149, -0.990773912207875,
-1.00630150550444, -0.114696861708198, -0.770207077119994,
-1.00599625914297, 0.905479668945967, -0.789288666194634,
-1.23444314599172, 0.307586880670016, -0.757367952764277,
0.17392668699804, 0.101501886956213, 0.101501886956213, -0.901443470637005,
0.221463655928208, 0.597923445796155, -0.171295456475918,
-0.355688325123882, 0.189458705916741, -1.27086828592366,
-0.185109266696401, -0.084241760781376, -0.851743735587115,
-0.876739930949657, -0.807816582967473, -0.800810270531034,
0.46121546654924, 0.57899684000564, -1.07020706024612, -0.191494621249592,
0.848116335875492, 0.516819759515149, 0.771349552989723,
0.00528575130825762, -1.60855842566889, -0.492111828295972,
-0.105386918073147, -0.0119655321968521, 0.189254065355485,
-1.3687587510874, 0.410067343503282, -1.63639153855241, 0.0926147667621627,
-0.145369316628964, 0.811352095226498, -0.160460027694226,
0.101501886956213, -0.891094890020195, -0.717330679204269
), Model = c("3", "1", "1", "2", "2", "4", "4", "1", "3",
"1", "1", "1", "4", "2", "2", "1", "4", "1", "3", "2", "2",
"1", "3", "2", "1", "1", "3", "4", "1", "4", "1", "2", "1",
"2", "2", "2", "2", "1", "1", "4", "4", "3", "1", "2", "1",
"2", "3", "4", "4", "3", "1", "4", "3", "4", "1", "2", "2",
"3", "1", "4", "4", "4", "2", "2", "4", "3", "2", "1", "3",
"1", "4", "1", "1", "1", "3", "4", "1", "1", "2", "2", "3",
"4", "2", "3", "3", "1", "3", "4", "3", "2", "4", "2", "2",
"1", "1", "4", "1", "2", "3", "2", "1", "2", "4", "2", "4",
"4", "1", "2", "1", "4", "3", "4", "4", "4", "1", "3", "2",
"3", "1", "4", "4", "2", "2", "4", "1", "4", "4", "3", "4",
"1", "4", "4", "3", "1", "1", "1", "1", "1", "4", "4", "3",
"2", "4", "4", "2", "2", "1", "4", "1", "1", "2", "2", "1",
"1", "2", "2", "4", "3", "3", "1", "3", "1", "3", "3", "4",
"1", "2", "4", "1", "1", "3", "3", "1", "2", "4", "1", "1",
"1", "2", "4", "3", "3", "2", "3", "3", "3", "3", "2", "1",
"1", "4", "1", "3", "1", "2", "3", "1", "3", "2", "1"), Model_PredBrkDwn = c("3",
"1", "1", "2", "2", "4", "4", "1", "3", "1", "1", "1", "4",
"2", "2", "1", "4", "1", "3", "2", "2", "1", "3", "2", "1",
"1", "3", "4", "1", "4", "1", "2", "1", "2", "2", "2", "2",
"1", "1", "4", "4", "3", "1", "2", "1", "2", "3", "4", "4",
"3", "1", "4", "3", "4", "1", "2", "2", "3", "1", "4", "4",
"4", "2", "2", "4", "3", "2", "1", "3", "1", "4", "1", "1",
"1", "3", "4", "1", "1", "2", "2", "3", "4", "2", "3", "3",
"1", "3", "4", "3", "2", "4", "2", "2", "1", "1", "4", "1",
"2", "3", "2", "1", "2", "4", "2", "4", "4", "1", "2", "1",
"4", "3", "4", "4", "4", "1", "3", "2", "3", "1", "4", "4",
"2", "2", "4", "1", "4", "4", "3", "4", "1", "4", "4", "3",
"1", "1", "1", "1", "1", "4", "4", "3", "2", "4", "4", "2",
"2", "1", "4", "1", "1", "2", "2", "1", "1", "2", "2", "4",
"3", "3", "1", "3", "1", "3", "3", "4", "1", "2", "4", "1",
"1", "3", "3", "1", "2", "4", "1", "1", "1", "2", "4", "3",
"3", "2", "3", "3", "3", "3", "2", "1", "1", "4", "1", "3",
"1", "2", "3", "1", "3", "2", "1")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -200L))
EDIT:
This seems to get the pivot_wider
working:
d %>%
filter(Model == 1) %>%
select(Model, Model_PredBrkDwn, everything()) %>%
pivot_longer(cols = 4:ncol(.), names_to = "results", values_to = "values")