Data sorting with R for XLConnect - r
Ok, so I have a workbook in Excel with sheets that look like this:
Date 1/2/15 1/3/15 1/4/15
Euro 3.54 2.50 #N/A
USD 3.20 3.30 3.35
Yen 2.50 2.35 2.40
The sheets are arranged exactly like this. I loaded each sheet individually via XLConnect such that:
wbFX <- loadWorkbook("fx.xlsx")
FX.high <- readWorksheet(wbFX, sheet=1)
FX.low <- readWorksheet(wbFX, sheet=2)
FX.close <- readWorksheet(wbFX, sheet=3)
One sheet is for closing prices, the other for low and the last one is closing prices.
I want to merge the currency rows (i.e: grouping Euro low, Euro high, Euro close) and create a table or dataframe such that I have:
Date 1/2/15 1/3/15 1/4/15
Euro close 3.54 2.50 #N/A
Euro low 3.20 3.30 3.35
Euro high 2.50 2.35 2.40
I only have very rudimentary knowledge of R and I'm not very familiar with the for() loop function in R. I understand the basics of it, but I'm struggling to reproduce what I want.
Suggestions would be very much appreciated!
UPDATED: Adding dput(FX.high) gives:
> dput(head(FX.high))
structure(list(Col1 = c("EUR CURNCY", "JPY CURNCY", "GBP CURNCY",
"CHF CURNCY", "AUD CURNCY", "CAD CURNCY"), X2016.08.30.00.00.00 = c(1.1192,
102.56, 1.312, 0.9815, 0.758, 1.3056), X2016.08.29.00.00.00 = c(1.1208,
102.39, 1.3172, 0.9807, 0.7582, 1.3048), X2016.08.28.00.00.00 = c(1.1341,
101.94, 1.3279, 0.9793, 0.7692, 1.3012), X2016.08.27.00.00.00 = c(1.1341,
101.94, 1.3279, 0.9793, 0.7692, 1.3012), X2016.08.26.00.00.00 = c(1.1341,
101.94, 1.3279, 0.9793, 0.7692, 1.3012), X2016.08.25.00.00.00 = c(1.1298,
100.62, 1.3264, 0.9688, 0.7639, 1.294), X2016.08.24.00.00.00 = c(1.1312,
100.61, 1.3273, 0.9684, 0.7634, 1.2958), X2016.08.23.00.00.00 = c(1.1355,
100.39, 1.3211, 0.9634, 0.7655, 1.2948), X2016.08.22.00.00.00 = c(1.1334,
100.93, 1.3157, 0.9649, 0.764, 1.2965), X2016.08.21.00.00.00 = c(1.136,
100.46, 1.3185, 0.9611, 0.7691, 1.2892), X2016.08.20.00.00.00 = c(1.136,
100.46, 1.3185, 0.9611, 0.7691, 1.2892), X2016.08.19.00.00.00 = c(1.136,
100.46, 1.3185, 0.9611, 0.7691, 1.2892), X2016.08.18.00.00.00 = c(1.1366,
100.5, 1.3173, 0.9627, 0.7723, 1.2858), X2016.08.17.00.00.00 = c(1.1316,
101.17, 1.3086, 0.9659, 0.7708, 1.2918), X2016.08.16.00.00.00 = c(1.1323,
101.29, 1.3051, 0.9735, 0.7749, 1.2934), X2016.08.15.00.00.00 = c(1.1204,
101.45, 1.2945, 0.9775, 0.7692, 1.2976), X2016.08.14.00.00.00 = c(1.1221,
102.27, 1.3035, 0.9766, 0.7725, 1.2994), X2016.08.13.00.00.00 = c(1.1221,
102.27, 1.3035, 0.9766, 0.7725, 1.2994), X2016.08.12.00.00.00 = c(1.1221,
102.27, 1.3035, 0.9766, 0.7725, 1.2994), X2016.08.11.00.00.00 = c(1.1192,
102.06, 1.3028, 0.9766, 0.775, 1.308), X2016.08.10.00.00.00 = c(1.119,
101.96, 1.3094, 0.9819, 0.7756, 1.3124), X2016.08.09.00.00.00 = c(1.1123,
102.53, 1.3049, 0.9844, 0.7687, 1.319), X2016.08.08.00.00.00 = c(1.1105,
102.66, 1.3097, 0.9842, 0.7672, 1.319), X2016.08.07.00.00.00 = c(1.1161,
102.06, 1.3175, 0.9831, 0.7664, 1.32), X2016.08.06.00.00.00 = c(1.1161,
102.06, 1.3175, 0.9831, 0.7664, 1.32), X2016.08.05.00.00.00 = c(1.1161,
102.06, 1.3175, 0.9831, 0.7664, 1.32), X2016.08.04.00.00.00 = c(1.1156,
101.67, 1.3346, 0.975, 0.7641, 1.3089), X2016.08.03.00.00.00 = c(1.1227,
101.57, 1.3372, 0.9739, 0.7616, 1.3148), X2016.08.02.00.00.00 = c(1.1234,
102.83, 1.3366, 0.9698, 0.7638, 1.3142), X2016.08.01.00.00.00 = c(1.1184,
102.68, 1.3273, 0.9703, 0.7615, 1.3127), X2016.07.31.00.00.00 = c(1.1197,
105.63, 1.3301, 0.981, 0.761, 1.3186), X2016.07.30.00.00.00 = c(1.1197,
105.63, 1.3301, 0.981, 0.761, 1.3186), X2016.07.29.00.00.00 = c(1.1197,
105.63, 1.3301, 0.981, 0.761, 1.3186), X2016.07.28.00.00.00 = c(1.1119,
105.51, 1.3248, 0.9868, 0.7549, 1.3192), X2016.07.27.00.00.00 = c(1.1065,
106.54, 1.3235, 0.995, 0.7566, 1.3253), X2016.07.26.00.00.00 = c(1.103,
105.89, 1.3176, 0.9928, 0.754, 1.3244), X2016.07.25.00.00.00 = c(1.0999,
106.72, 1.3165, 0.9897, 0.7492, 1.3242), X2016.07.24.00.00.00 = c(1.1041,
106.4, 1.3291, 0.9895, 0.7508, 1.3185), X2016.07.23.00.00.00 = c(1.1041,
106.4, 1.3291, 0.9895, 0.7508, 1.3185), X2016.07.22.00.00.00 = c(1.1041,
106.4, 1.3291, 0.9895, 0.7508, 1.3185), X2016.07.21.00.00.00 = c(1.106,
107.49, 1.3275, 0.9907, 0.7514, 1.3101), X2016.07.20.00.00.00 = c(1.103,
107.02, 1.3226, 0.9905, 0.7517, 1.3096), X2016.07.19.00.00.00 = c(1.1081,
106.53, 1.3276, 0.9878, 0.7592, 1.3054), X2016.07.18.00.00.00 = c(1.1084,
106.26, 1.3315, 0.9847, 0.7607, 1.3022), X2016.07.17.00.00.00 = c(1.1149,
106.32, 1.3481, 0.9847, 0.7676, 1.2988), X2016.07.16.00.00.00 = c(1.1149,
106.32, 1.3481, 0.9847, 0.7676, 1.2988), X2016.07.15.00.00.00 = c(1.1149,
106.32, 1.3481, 0.9847, 0.7676, 1.2988), X2016.07.14.00.00.00 = c(1.1165,
105.94, 1.3475, 0.9854, 0.7653, 1.2987), X2016.07.13.00.00.00 = c(1.112,
104.88, 1.3338, 0.9894, 0.7638, 1.3084), X2016.07.12.00.00.00 = c(1.1126,
104.99, 1.3295, 0.9894, 0.7658, 1.3133), X2016.07.11.00.00.00 = c(1.1075,
102.89, 1.3018, 0.9858, 0.7576, 1.314), X2016.07.10.00.00.00 = c(1.112,
101.28, 1.3019, 0.9866, 0.7574, 1.309), X2016.07.09.00.00.00 = c(1.112,
101.28, 1.3019, 0.9866, 0.7574, 1.309), X2016.07.08.00.00.00 = c(1.112,
101.28, 1.3019, 0.9866, 0.7574, 1.309), X2016.07.07.00.00.00 = c(1.1107,
101.41, 1.3047, 0.9792, 0.7539, 1.3021), X2016.07.06.00.00.00 = c(1.1112,
101.77, 1.3028, 0.9806, 0.7529, 1.3056), X2016.07.05.00.00.00 = c(1.1186,
102.59, 1.3291, 0.9774, 0.7545, 1.3017), X2016.07.04.00.00.00 = c(1.116,
102.8, 1.3341, 0.9759, 0.7545, 1.2926), X2016.07.03.00.00.00 = c(1.1169,
103.39, 1.335, 0.9781, 0.7503, 1.2975), X2016.07.02.00.00.00 = c(1.1169,
103.39, 1.335, 0.9781, 0.7503, 1.2975), X2016.07.01.00.00.00 = c(1.1169,
103.39, 1.335, 0.9781, 0.7503, 1.2975), X2016.06.30.00.00.00 = c(1.1155,
103.29, 1.3496, 0.9821, 0.7473, 1.3016), X2016.06.29.00.00.00 = c(1.113,
102.94, 1.3534, 0.9823, 0.7456, 1.3042), X2016.06.28.00.00.00 = c(1.1112,
102.84, 1.3419, 0.9837, 0.7415, 1.3108), X2016.06.27.00.00.00 = c(1.1084,
102.48, 1.3566, 0.9819, 0.7459, 1.312), X2016.06.26.00.00.00 = c(1.1428,
106.84, 1.5018, 0.9804, 0.7648, 1.3099), X2016.06.25.00.00.00 = c(1.1428,
106.84, 1.5018, 0.9804, 0.7648, 1.3099), X2016.06.24.00.00.00 = c(1.1428,
106.84, 1.5018, 0.9804, 0.7648, 1.3099), X2016.06.23.00.00.00 = c(1.1421,
106.17, 1.4947, 0.9602, 0.7616, 1.2847), X2016.06.22.00.00.00 = c(1.1338,
104.85, 1.4774, 0.9629, 0.7527, 1.2853), X2016.06.21.00.00.00 = c(1.135,
105.06, 1.4783, 0.9624, 0.7513, 1.2827), X2016.06.20.00.00.00 = c(1.1382,
104.85, 1.472, 0.9643, 0.7481, 1.2889), X2016.06.19.00.00.00 = c(1.1296,
104.83, 1.4388, 0.9659, 0.7411, 1.2968), X2016.06.18.00.00.00 = c(1.1296,
104.83, 1.4388, 0.9659, 0.7411, 1.2968), X2016.06.17.00.00.00 = c(1.1296,
104.83, 1.4388, 0.9659, 0.7411, 1.2968), X2016.06.16.00.00.00 = c(1.1295,
106.03, 1.4254, 0.9687, 0.744, 1.3086), X2016.06.15.00.00.00 = c(1.1298,
106.4, 1.4218, 0.9664, 0.7446, 1.2943), X2016.06.14.00.00.00 = c(1.1298,
106.42, 1.4271, 0.9669, 0.7405, 1.2873), X2016.06.13.00.00.00 = c(1.1303,
106.98, 1.4328, 0.9679, 0.7411, 1.2839), X2016.06.12.00.00.00 = c(1.1321,
107.26, 1.4473, 0.9658, 0.7438, 1.2784), X2016.06.11.00.00.00 = c(1.1321,
107.26, 1.4473, 0.9658, 0.7438, 1.2784), X2016.06.10.00.00.00 = c(1.1321,
107.26, 1.4473, 0.9658, 0.7438, 1.2784), X2016.06.09.00.00.00 = c(1.1416,
107.18, 1.4527, 0.9656, 0.7505, 1.2767), X2016.06.08.00.00.00 = c(1.1411,
107.39, 1.4601, 0.9659, 0.7482, 1.2761), X2016.06.07.00.00.00 = c(1.1381,
107.9, 1.466, 0.9719, 0.7464, 1.284), X2016.06.06.00.00.00 = c(1.1393,
107.66, 1.4529, 0.978, 0.7391, 1.2983), X2016.06.05.00.00.00 = c(1.1374,
109.14, 1.4582, 0.992, 0.7369, 1.3107), X2016.06.04.00.00.00 = c(1.1374,
109.14, 1.4582, 0.992, 0.7369, 1.3107), X2016.06.03.00.00.00 = c(1.1374,
109.14, 1.4582, 0.992, 0.7369, 1.3107), X2016.06.02.00.00.00 = c(1.122,
109.59, 1.4473, 0.9911, 0.727, 1.3144), X2016.06.01.00.00.00 = c(1.1194,
110.83, 1.4508, 0.9951, 0.7299, 1.3123), X2016.05.31.00.00.00 = c(1.1173,
111.35, 1.4725, 0.9951, 0.7267, 1.3134), X2016.05.30.00.00.00 = c(1.1145,
111.45, 1.4642, 0.9956, 0.7188, 1.3095), X2016.05.29.00.00.00 = c(1.1201,
110.45, 1.4689, 0.9949, 0.7235, 1.3068), X2016.05.28.00.00.00 = c(1.1201,
110.45, 1.4689, 0.9949, 0.7235, 1.3068), X2016.05.27.00.00.00 = c(1.1201,
110.45, 1.4689, 0.9949, 0.7235, 1.3068), X2016.05.26.00.00.00 = c(1.1217,
110.23, 1.474, 0.9928, 0.7244, 1.3037), X2016.05.25.00.00.00 = c(1.1167,
110.45, 1.4729, 0.9936, 0.7219, 1.3133), X2016.05.24.00.00.00 = c(1.1227,
110.13, 1.4642, 0.9938, 0.7228, 1.3188), X2016.05.23.00.00.00 = c(1.1243,
110.24, 1.4549, 0.9924, 0.726, 1.3174), X2016.05.22.00.00.00 = c(1.1237,
110.59, 1.4613, 0.9927, 0.725, 1.3162), X2016.05.21.00.00.00 = c(1.1237,
110.59, 1.4613, 0.9927, 0.725, 1.3162), X2016.05.20.00.00.00 = c(1.1237,
110.59, 1.4613, 0.9927, 0.725, 1.3162), X2016.05.19.00.00.00 = c(1.123,
110.38, 1.4663, 0.9923, 0.7242, 1.3154), X2016.05.18.00.00.00 = c(1.1316,
110.26, 1.4635, 0.9881, 0.7332, 1.3037), X2016.05.17.00.00.00 = c(1.1349,
109.65, 1.4524, 0.9809, 0.7366, 1.2955), X2016.05.16.00.00.00 = c(1.1342,
109.1, 1.4415, 0.9784, 0.7309, 1.2963), X2016.05.15.00.00.00 = c(1.138,
109.56, 1.4456, 0.9775, 0.7326, 1.2958), X2016.05.14.00.00.00 = c(1.138,
109.56, 1.4456, 0.9775, 0.7326, 1.2958), X2016.05.13.00.00.00 = c(1.138,
109.56, 1.4456, 0.9775, 0.7326, 1.2958), X2016.05.12.00.00.00 = c(1.1429,
109.4, 1.4531, 0.9726, 0.738, 1.2879), X2016.05.11.00.00.00 = c(1.1447,
109.38, 1.4488, 0.9762, 0.7402, 1.2942), X2016.05.10.00.00.00 = c(1.141,
109.35, 1.4478, 0.9763, 0.7374, 1.298), X2016.05.09.00.00.00 = c(1.142,
108.6, 1.448, 0.9736, 0.7385, 1.3015), X2016.05.08.00.00.00 = c(1.1479,
107.42, 1.4543, 0.973, 0.7478, 1.2952), X2016.05.07.00.00.00 = c(1.1479,
107.42, 1.4543, 0.973, 0.7478, 1.2952), X2016.05.06.00.00.00 = c(1.1479,
107.42, 1.4543, 0.973, 0.7478, 1.2952), X2016.05.05.00.00.00 = c(1.1494,
107.5, 1.4529, 0.9697, 0.7514, 1.2875), X2016.05.04.00.00.00 = c(1.1529,
107.46, 1.4572, 0.959, 0.7517, 1.2886), X2016.05.03.00.00.00 = c(1.1616,
106.68, 1.477, 0.9554, 0.7719, 1.2732), X2016.05.02.00.00.00 = c(1.1536,
106.82, 1.4696, 0.9605, 0.7672, 1.2571), X2016.05.01.00.00.00 = c(1.1459,
108.2, 1.467, 0.9672, 0.7669, 1.2587), X2016.04.30.00.00.00 = c(1.1459,
108.2, 1.467, 0.9672, 0.7669, 1.2587), X2016.04.29.00.00.00 = c(1.1459,
108.2, 1.467, 0.9672, 0.7669, 1.2587), X2016.04.28.00.00.00 = c(1.1368,
111.88, 1.4623, 0.9734, 0.7658, 1.2606), X2016.04.27.00.00.00 = c(1.1362,
111.75, 1.4622, 0.9753, 0.7765, 1.2694), X2016.04.26.00.00.00 = c(1.134,
111.47, 1.4639, 0.9767, 0.7765, 1.2688), X2016.04.25.00.00.00 = c(1.1278,
111.91, 1.452, 0.9794, 0.7729, 1.2717), X2016.04.24.00.00.00 = c(1.1309,
111.81, 1.4452, 0.9797, 0.7774, 1.2758), X2016.04.23.00.00.00 = c(1.1309,
111.81, 1.4452, 0.9797, 0.7774, 1.2758), X2016.04.22.00.00.00 = c(1.1309,
111.81, 1.4452, 0.9797, 0.7774, 1.2758), X2016.04.21.00.00.00 = c(1.1398,
109.9, 1.444, 0.9753, 0.7835, 1.275), X2016.04.20.00.00.00 = c(1.1388,
109.88, 1.441, 0.9733, 0.7829, 1.273), X2016.04.19.00.00.00 = c(1.1385,
109.49, 1.4419, 0.965, 0.7826, 1.2798), X2016.04.18.00.00.00 = c(1.1332,
108.99, 1.4291, 0.9679, 0.7759, 1.299), X2016.04.17.00.00.00 = c(1.1317,
109.73, 1.4242, 0.9688, 0.7734, 1.2903), X2016.04.16.00.00.00 = c(1.1317,
109.73, 1.4242, 0.9688, 0.7734, 1.2903), X2016.04.15.00.00.00 = c(1.1317,
109.73, 1.4242, 0.9688, 0.7734, 1.2903), X2016.04.14.00.00.00 = c(1.1295,
109.55, 1.4208, 0.9688, 0.7737, 1.2897), X2016.04.13.00.00.00 = c(1.1391,
109.41, 1.4279, 0.9672, 0.7716, 1.2828), X2016.04.12.00.00.00 = c(1.1465,
108.79, 1.4348, 0.9594, 0.769, 1.2921), X2016.04.11.00.00.00 = c(1.1447,
108.44, 1.4287, 0.9571, 0.763, 1.3016), X2016.04.10.00.00.00 = c(1.1419,
109.1, 1.4141, 0.9582, 0.7579, 1.3157), X2016.04.09.00.00.00 = c(1.1419,
109.1, 1.4141, 0.9582, 0.7579, 1.3157), X2016.04.08.00.00.00 = c(1.1419,
109.1, 1.4141, 0.9582, 0.7579, 1.3157), X2016.04.07.00.00.00 = c(1.1454,
109.9, 1.4157, 0.9581, 0.7637, 1.3181), X2016.04.06.00.00.00 = c(1.1432,
110.64, 1.4171, 0.9622, 0.7619, 1.3187), X2016.04.05.00.00.00 = c(1.1405,
111.36, 1.4279, 0.9605, 0.7632, 1.3219), X2016.04.04.00.00.00 = c(1.1413,
111.8, 1.4322, 0.9615, 0.7679, 1.3088), X2016.04.03.00.00.00 = c(1.1438,
112.58, 1.4372, 0.9626, 0.7701, 1.3147), X2016.04.02.00.00.00 = c(1.1438,
112.58, 1.4372, 0.9626, 0.7701, 1.3147), X2016.04.01.00.00.00 = c(1.1438,
112.58, 1.4372, 0.9626, 0.7701, 1.3147), X2016.03.31.00.00.00 = c(1.1412,
112.66, 1.4426, 0.9663, 0.7723, 1.3011), X2016.03.30.00.00.00 = c(1.1365,
112.81, 1.4459, 0.9672, 0.7709, 1.3081), X2016.03.29.00.00.00 = c(1.1303,
113.8, 1.4404, 0.9763, 0.7645, 1.3216), X2016.03.28.00.00.00 = c(1.122,
113.69, 1.4283, 0.9787, 0.7558, 1.3285), X2016.03.27.00.00.00 = c(1.1181,
113.32, 1.4159, 0.9788, 0.7535, 1.3285), X2016.03.26.00.00.00 = c(1.1181,
113.32, 1.4159, 0.9788, 0.7535, 1.3285), X2016.03.25.00.00.00 = c(1.1181,
113.32, 1.4159, 0.9788, 0.7535, 1.3285), X2016.03.24.00.00.00 = c(1.1188,
113.01, 1.4183, 0.9774, 0.7538, 1.3296), X2016.03.23.00.00.00 = c(1.1224,
112.91, 1.4227, 0.9766, 0.7649, 1.3219), X2016.03.22.00.00.00 = c(1.126,
112.49, 1.4398, 0.9736, 0.7643, 1.3139), X2016.03.21.00.00.00 = c(1.1288,
111.98, 1.4469, 0.9731, 0.7627, 1.3102), X2016.03.20.00.00.00 = c(1.1337,
111.76, 1.4514, 0.9717, 0.768, 1.3043), X2016.03.19.00.00.00 = c(1.1337,
111.76, 1.4514, 0.9717, 0.768, 1.3043), X2016.03.18.00.00.00 = c(1.1337,
111.76, 1.4514, 0.9717, 0.768, 1.3043), X2016.03.17.00.00.00 = c(1.1342,
112.96, 1.4503, 0.9787, 0.7657, 1.3133), X2016.03.16.00.00.00 = c(1.1242,
113.82, 1.4274, 0.9914, 0.7561, 1.3405), X2016.03.15.00.00.00 = c(1.1125,
114.14, 1.4306, 0.9896, 0.7528, 1.3402), X2016.03.14.00.00.00 = c(1.1176,
114.01, 1.4401, 0.9882, 0.7594, 1.3308), X2016.03.13.00.00.00 = c(1.121,
113.92, 1.4437, 0.9891, 0.7584, 1.3359), X2016.03.12.00.00.00 = c(1.121,
113.92, 1.4437, 0.9891, 0.7584, 1.3359), X2016.03.11.00.00.00 = c(1.121,
113.92, 1.4437, 0.9891, 0.7584, 1.3359), X2016.03.10.00.00.00 = c(1.1218,
114.45, 1.4317, 1.0093, 0.7512, 1.3398), X2016.03.09.00.00.00 = c(1.1035,
113.45, 1.4241, 1.0039, 0.7528, 1.3446), X2016.03.08.00.00.00 = c(1.1058,
113.52, 1.4276, 0.9971, 0.7473, 1.3425), X2016.03.07.00.00.00 = c(1.1026,
114.09, 1.4284, 1.0012, 0.7485, 1.3377), X2016.03.06.00.00.00 = c(1.1043,
114.26, 1.4248, 0.9989, 0.7443, 1.3471), X2016.03.05.00.00.00 = c(1.1043,
114.26, 1.4248, 0.9989, 0.7443, 1.3471), X2016.03.04.00.00.00 = c(1.1043,
114.26, 1.4248, 0.9989, 0.7443, 1.3471), X2016.03.03.00.00.00 = c(1.0973,
114.27, 1.4194, 0.9983, 0.7374, 1.3473), X2016.03.02.00.00.00 = c(1.0881,
114.56, 1.4093, 1.0009, 0.7301, 1.3499), X2016.03.01.00.00.00 = c(1.0894,
114.19, 1.4018, 1.0009, 0.7192, 1.3552), X2016.02.29.00.00.00 = c(1.0963,
113.99, 1.3946, 1.0038, 0.7168, 1.3587), X2016.02.28.00.00.00 = c(1.1068,
114, 1.4043, 0.9989, 0.7257, 1.3565), X2016.02.27.00.00.00 = c(1.1068,
114, 1.4043, 0.9989, 0.7257, 1.3565), X2016.02.26.00.00.00 = c(1.1068,
114, 1.4043, 0.9989, 0.7257, 1.3565), X2016.02.25.00.00.00 = c(1.105,
113.02, 1.3997, 0.9952, 0.7244, 1.3735), X2016.02.24.00.00.00 = c(1.1046,
112.27, 1.4028, 0.9953, 0.7213, 1.3859), X2016.02.23.00.00.00 = c(1.1053,
113.05, 1.4156, 1.0002, 0.7259, 1.3821), X2016.02.22.00.00.00 = c(1.1135,
113.39, 1.4332, 1.0004, 0.7247, 1.3813), X2016.02.21.00.00.00 = c(1.1139,
113.38, 1.4409, 0.9968, 0.7162, 1.3847), X2016.02.20.00.00.00 = c(1.1139,
113.38, 1.4409, 0.9968, 0.7162, 1.3847), X2016.02.19.00.00.00 = c(1.1139,
113.38, 1.4409, 0.9968, 0.7162, 1.3847), X2016.02.18.00.00.00 = c(1.115,
114.33, 1.4394, 0.9969, 0.7185, 1.3752), X2016.02.17.00.00.00 = c(1.1179,
114.51, 1.4339, 0.9942, 0.7187, 1.3899), X2016.02.16.00.00.00 = c(1.1193,
114.87, 1.4516, 0.9896, 0.7182, 1.3912), X2016.02.15.00.00.00 = c(1.1261,
114.73, 1.4567, 0.9889, 0.7172, 1.3867), X2016.02.14.00.00.00 = c(1.1334,
113.54, 1.457, 0.9791, 0.7129, 1.3965), X2016.02.13.00.00.00 = c(1.1334,
113.54, 1.457, 0.9791, 0.7129, 1.3965), X2016.02.12.00.00.00 = c(1.1334,
113.54, 1.457, 0.9791, 0.7129, 1.3965), X2016.02.11.00.00.00 = c(1.1376,
113.6, 1.4564, 0.9762, 0.7153, 1.4016), X2016.02.10.00.00.00 = c(1.1311,
115.26, 1.4578, 0.982, 0.7125, 1.3999), X2016.02.09.00.00.00 = c(1.1338,
115.85, 1.4516, 0.9875, 0.7096, 1.396), X2016.02.08.00.00.00 = c(1.1216,
117.53, 1.4547, 0.9973, 0.7129, 1.3978), X2016.02.07.00.00.00 = c(1.1246,
117.43, 1.4592, 0.9985, 0.7219, 1.3919), X2016.02.06.00.00.00 = c(1.1246,
117.43, 1.4592, 0.9985, 0.7219, 1.3919), X2016.02.05.00.00.00 = c(1.1246,
117.43, 1.4592, 0.9985, 0.7219, 1.3919), X2016.02.04.00.00.00 = c(1.1239,
118.24, 1.4668, 1.0074, 0.7243, 1.3798), X2016.02.03.00.00.00 = c(1.1146,
120.04, 1.4649, 1.0196, 0.7189, 1.4103), X2016.02.02.00.00.00 = c(1.094,
121.04, 1.4446, 1.0224, 0.7129, 1.4082), X2016.02.01.00.00.00 = c(1.0913,
121.49, 1.4445, 1.025, 0.7121, 1.4062), X2016.01.31.00.00.00 = c(1.0949,
121.69, 1.4413, 1.0257, 0.7141, 1.4109), X2016.01.30.00.00.00 = c(1.0949,
121.69, 1.4413, 1.0257, 0.7141, 1.4109), X2016.01.29.00.00.00 = c(1.0949,
121.69, 1.4413, 1.0257, 0.7141, 1.4109), X2016.01.28.00.00.00 = c(1.0968,
118.99, 1.4408, 1.0178, 0.7129, 1.4123), X2016.01.27.00.00.00 = c(1.0917,
119.07, 1.4355, 1.0189, 0.7082, 1.4157), X2016.01.26.00.00.00 = c(1.0874,
118.62, 1.4367, 1.0199, 0.7021, 1.4326), X2016.01.25.00.00.00 = c(1.0857,
118.86, 1.4332, 1.0184, 0.7032, 1.4293), X2016.01.24.00.00.00 = c(1.0877,
118.88, 1.4363, 1.0166, 0.7046, 1.4301), X2016.01.23.00.00.00 = c(1.0877,
118.88, 1.4363, 1.0166, 0.7046, 1.4301), X2016.01.22.00.00.00 = c(1.0877,
118.88, 1.4363, 1.0166, 0.7046, 1.4301), X2016.01.21.00.00.00 = c(1.0921,
117.81, 1.4249, 1.0147, 0.7018, 1.4541), X2016.01.20.00.00.00 = c(1.0976,
117.69, 1.4219, 1.0058, 0.6926, 1.469), X2016.01.19.00.00.00 = c(1.0939,
118.11, 1.434, 1.0082, 0.6957, 1.4589), X2016.01.18.00.00.00 = c(1.0942,
117.44, 1.4323, 1.0073, 0.6928, 1.466), X2016.01.17.00.00.00 = c(1.0985,
118.27, 1.4428, 1.0061, 0.7002, 1.4554), X2016.01.16.00.00.00 = c(1.0985,
118.27, 1.4428, 1.0061, 0.7002, 1.4554), X2016.01.15.00.00.00 = c(1.0985,
118.27, 1.4428, 1.0061, 0.7002, 1.4554), X2016.01.14.00.00.00 = c(1.0943,
118.28, 1.4445, 1.0092, 0.6997, 1.4397), X2016.01.13.00.00.00 = c(1.0888,
118.38, 1.4476, 1.0107, 0.7049, 1.438), X2016.01.12.00.00.00 = c(1.09,
118.07, 1.456, 1.0047, 0.7021, 1.4315), X2016.01.11.00.00.00 = c(1.097,
118.02, 1.4604, 1.0023, 0.7036, 1.4246), X2016.01.10.00.00.00 = c(1.0934,
118.83, 1.4645, 1.0052, 0.7077, 1.4178), X2016.01.09.00.00.00 = c(1.0934,
118.83, 1.4645, 1.0052, 0.7077, 1.4178), X2016.01.08.00.00.00 = c(1.0934,
118.83, 1.4645, 1.0052, 0.7077, 1.4178), X2016.01.07.00.00.00 = c(1.094,
118.76, 1.4641, 1.0081, 0.7086, 1.417), X2016.01.06.00.00.00 = c(1.0799,
119.17, 1.4682, 1.0121, 0.7172, 1.4109), X2016.01.05.00.00.00 = c(1.0839,
119.7, 1.4726, 1.0125, 0.7215, 1.4019), X2016.01.04.00.00.00 = c(1.0946,
120.47, 1.4816, 1.0063, 0.7305, 1.3983), X2016.01.03.00.00.00 = c(1.0867,
120.55, 1.476, 1.0083, 0.7304, 1.3856), X2016.01.02.00.00.00 = c(1.0867,
120.55, 1.476, 1.0083, 0.7304, 1.3856), X2016.01.01.00.00.00 = c(1.0867,
120.55, 1.476, 1.0083, 0.7304, 1.3856)), .Names = c("Col1", "X2016.08.30.00.00.00",
"X2016.08.29.00.00.00", "X2016.08.28.00.00.00", "X2016.08.27.00.00.00",
"X2016.08.26.00.00.00", "X2016.08.25.00.00.00", "X2016.08.24.00.00.00",
"X2016.08.23.00.00.00", "X2016.08.22.00.00.00", "X2016.08.21.00.00.00",
"X2016.08.20.00.00.00", "X2016.08.19.00.00.00", "X2016.08.18.00.00.00",
"X2016.08.17.00.00.00", "X2016.08.16.00.00.00", "X2016.08.15.00.00.00",
"X2016.08.14.00.00.00", "X2016.08.13.00.00.00", "X2016.08.12.00.00.00",
"X2016.08.11.00.00.00", "X2016.08.10.00.00.00", "X2016.08.09.00.00.00",
"X2016.08.08.00.00.00", "X2016.08.07.00.00.00", "X2016.08.06.00.00.00",
"X2016.08.05.00.00.00", "X2016.08.04.00.00.00", "X2016.08.03.00.00.00",
"X2016.08.02.00.00.00", "X2016.08.01.00.00.00", "X2016.07.31.00.00.00",
"X2016.07.30.00.00.00", "X2016.07.29.00.00.00", "X2016.07.28.00.00.00",
"X2016.07.27.00.00.00", "X2016.07.26.00.00.00", "X2016.07.25.00.00.00",
"X2016.07.24.00.00.00", "X2016.07.23.00.00.00", "X2016.07.22.00.00.00",
"X2016.07.21.00.00.00", "X2016.07.20.00.00.00", "X2016.07.19.00.00.00",
"X2016.07.18.00.00.00", "X2016.07.17.00.00.00", "X2016.07.16.00.00.00",
"X2016.07.15.00.00.00", "X2016.07.14.00.00.00", "X2016.07.13.00.00.00",
"X2016.07.12.00.00.00", "X2016.07.11.00.00.00", "X2016.07.10.00.00.00",
"X2016.07.09.00.00.00", "X2016.07.08.00.00.00", "X2016.07.07.00.00.00",
"X2016.07.06.00.00.00", "X2016.07.05.00.00.00", "X2016.07.04.00.00.00",
"X2016.07.03.00.00.00", "X2016.07.02.00.00.00", "X2016.07.01.00.00.00",
"X2016.06.30.00.00.00", "X2016.06.29.00.00.00", "X2016.06.28.00.00.00",
"X2016.06.27.00.00.00", "X2016.06.26.00.00.00", "X2016.06.25.00.00.00",
"X2016.06.24.00.00.00", "X2016.06.23.00.00.00", "X2016.06.22.00.00.00",
"X2016.06.21.00.00.00", "X2016.06.20.00.00.00", "X2016.06.19.00.00.00",
"X2016.06.18.00.00.00", "X2016.06.17.00.00.00", "X2016.06.16.00.00.00",
"X2016.06.15.00.00.00", "X2016.06.14.00.00.00", "X2016.06.13.00.00.00",
"X2016.06.12.00.00.00", "X2016.06.11.00.00.00", "X2016.06.10.00.00.00",
"X2016.06.09.00.00.00", "X2016.06.08.00.00.00", "X2016.06.07.00.00.00",
"X2016.06.06.00.00.00", "X2016.06.05.00.00.00", "X2016.06.04.00.00.00",
"X2016.06.03.00.00.00", "X2016.06.02.00.00.00", "X2016.06.01.00.00.00",
"X2016.05.31.00.00.00", "X2016.05.30.00.00.00", "X2016.05.29.00.00.00",
"X2016.05.28.00.00.00", "X2016.05.27.00.00.00", "X2016.05.26.00.00.00",
"X2016.05.25.00.00.00", "X2016.05.24.00.00.00", "X2016.05.23.00.00.00",
"X2016.05.22.00.00.00", "X2016.05.21.00.00.00", "X2016.05.20.00.00.00",
"X2016.05.19.00.00.00", "X2016.05.18.00.00.00", "X2016.05.17.00.00.00",
"X2016.05.16.00.00.00", "X2016.05.15.00.00.00", "X2016.05.14.00.00.00",
"X2016.05.13.00.00.00", "X2016.05.12.00.00.00", "X2016.05.11.00.00.00",
"X2016.05.10.00.00.00", "X2016.05.09.00.00.00", "X2016.05.08.00.00.00",
"X2016.05.07.00.00.00", "X2016.05.06.00.00.00", "X2016.05.05.00.00.00",
"X2016.05.04.00.00.00", "X2016.05.03.00.00.00", "X2016.05.02.00.00.00",
"X2016.05.01.00.00.00", "X2016.04.30.00.00.00", "X2016.04.29.00.00.00",
"X2016.04.28.00.00.00", "X2016.04.27.00.00.00", "X2016.04.26.00.00.00",
"X2016.04.25.00.00.00", "X2016.04.24.00.00.00", "X2016.04.23.00.00.00",
"X2016.04.22.00.00.00", "X2016.04.21.00.00.00", "X2016.04.20.00.00.00",
"X2016.04.19.00.00.00", "X2016.04.18.00.00.00", "X2016.04.17.00.00.00",
"X2016.04.16.00.00.00", "X2016.04.15.00.00.00", "X2016.04.14.00.00.00",
"X2016.04.13.00.00.00", "X2016.04.12.00.00.00", "X2016.04.11.00.00.00",
"X2016.04.10.00.00.00", "X2016.04.09.00.00.00", "X2016.04.08.00.00.00",
"X2016.04.07.00.00.00", "X2016.04.06.00.00.00", "X2016.04.05.00.00.00",
"X2016.04.04.00.00.00", "X2016.04.03.00.00.00", "X2016.04.02.00.00.00",
"X2016.04.01.00.00.00", "X2016.03.31.00.00.00", "X2016.03.30.00.00.00",
"X2016.03.29.00.00.00", "X2016.03.28.00.00.00", "X2016.03.27.00.00.00",
"X2016.03.26.00.00.00", "X2016.03.25.00.00.00", "X2016.03.24.00.00.00",
"X2016.03.23.00.00.00", "X2016.03.22.00.00.00", "X2016.03.21.00.00.00",
"X2016.03.20.00.00.00", "X2016.03.19.00.00.00", "X2016.03.18.00.00.00",
"X2016.03.17.00.00.00", "X2016.03.16.00.00.00", "X2016.03.15.00.00.00",
"X2016.03.14.00.00.00", "X2016.03.13.00.00.00", "X2016.03.12.00.00.00",
"X2016.03.11.00.00.00", "X2016.03.10.00.00.00", "X2016.03.09.00.00.00",
"X2016.03.08.00.00.00", "X2016.03.07.00.00.00", "X2016.03.06.00.00.00",
"X2016.03.05.00.00.00", "X2016.03.04.00.00.00", "X2016.03.03.00.00.00",
"X2016.03.02.00.00.00", "X2016.03.01.00.00.00", "X2016.02.29.00.00.00",
"X2016.02.28.00.00.00", "X2016.02.27.00.00.00", "X2016.02.26.00.00.00",
"X2016.02.25.00.00.00", "X2016.02.24.00.00.00", "X2016.02.23.00.00.00",
"X2016.02.22.00.00.00", "X2016.02.21.00.00.00", "X2016.02.20.00.00.00",
"X2016.02.19.00.00.00", "X2016.02.18.00.00.00", "X2016.02.17.00.00.00",
"X2016.02.16.00.00.00", "X2016.02.15.00.00.00", "X2016.02.14.00.00.00",
"X2016.02.13.00.00.00", "X2016.02.12.00.00.00", "X2016.02.11.00.00.00",
"X2016.02.10.00.00.00", "X2016.02.09.00.00.00", "X2016.02.08.00.00.00",
"X2016.02.07.00.00.00", "X2016.02.06.00.00.00", "X2016.02.05.00.00.00",
"X2016.02.04.00.00.00", "X2016.02.03.00.00.00", "X2016.02.02.00.00.00",
"X2016.02.01.00.00.00", "X2016.01.31.00.00.00", "X2016.01.30.00.00.00",
"X2016.01.29.00.00.00", "X2016.01.28.00.00.00", "X2016.01.27.00.00.00",
"X2016.01.26.00.00.00", "X2016.01.25.00.00.00", "X2016.01.24.00.00.00",
"X2016.01.23.00.00.00", "X2016.01.22.00.00.00", "X2016.01.21.00.00.00",
"X2016.01.20.00.00.00", "X2016.01.19.00.00.00", "X2016.01.18.00.00.00",
"X2016.01.17.00.00.00", "X2016.01.16.00.00.00", "X2016.01.15.00.00.00",
"X2016.01.14.00.00.00", "X2016.01.13.00.00.00", "X2016.01.12.00.00.00",
"X2016.01.11.00.00.00", "X2016.01.10.00.00.00", "X2016.01.09.00.00.00",
"X2016.01.08.00.00.00", "X2016.01.07.00.00.00", "X2016.01.06.00.00.00",
"X2016.01.05.00.00.00", "X2016.01.04.00.00.00", "X2016.01.03.00.00.00",
"X2016.01.02.00.00.00", "X2016.01.01.00.00.00"), row.names = c(NA,
6L), class = "data.frame")
I haven't tested this, since I don't have your data files, so let me know if the code below gives you what you wanted. The steps are as follows:
Get the names of the three worksheets in the Excel workbook (I'm assuming they'll be something like "close", "low", and "high").
Read each worksheet into a list of data frames using lapply. The data in your worksheets is transposed (i.e., the variables are in rows instead of columns), so we we transpose the data so that each variable is in a column, and we also add an extra column that tells us which worksheet the data originally came from.
Combine the three data frames into a single data frame.
"melt" so that the three different currencies will be stacked in long format.
The final output, df, should be a data frame in "long" format that's ready for analysis.
library(reshape2)
library(XLConnect)
wbFX <- loadWorkbook("fx.xlsx")
# Get names of worksheets
sheets = getSheets(wbFX)
# Read the three worksheets into a list of data frames
df = lapply(sheets, function(sh) {
dat = as.data.frame(t(readWorksheet(wbFX, sheet=sh)))
dat$Date = as.Date(dat$Date)
dat$Price_Type = sh
dat
})
# Combine each list element into a single data frame
df = do.call(rbind, df)
# Melt to long format
df = melt(df, id.var=c("Date", "Price_Type"), variable.name="Currency", value.name="Price")
Maybe try
library(reshape2)
lst <- lapply(list(high, low, close), melt, id.vars = 1, variable.name = "var")
df <- Reduce(function(...) merge(..., by = c("Date", "var")), lst )
names(df) <- c("currency", "date", "high", "low", "close")
recast(df, currency+variable~date, id.var = 1:2)
which should give you something like
# currency variable 1/2/15 1/3/15 1/4/15
# 1 Euro high ... ... ...
# 2 Euro low ... ... ...
# 3 Euro close ... ... ...
Related
Plot time series without straight lines
I am trying to plot some time series but since I have data only for the summer I get these straight lines. Any idea how to fix that? The code I used: Any idea would be helpful! ggplot(ba, aes(x=date1, y=pc1)) + geom_line(color="turquoise4") + theme_minimal() + labs(x="", y="Loading", title="Correlation of PC1 and original series") + theme(plot.title = element_text(hjust=0.5, size=20, face="bold"))+ scale_x_date(date_labels= ("%Y")) And the plot: The data: structure(list(date1 = structure(c(10712, 10713, 10714, 10715, 10716, 10717, 10718, 10719, 10720, 10721, 10722, 10723, 10724, 10725, 10726, 10727, 10728, 10729, 10730, 10731, 10732, 10733, 10734, 10735, 10736, 10737, 10738, 10739, 10740, 10741, 10742, 10743, 10744, 10745, 10746, 10747, 10748, 10749, 10750, 10751, 10752, 10753, 10754, 10755, 10756, 10757, 10758, 10759, 10760, 10761, 10762, 10763, 10764, 10765, 10766, 10767, 10768, 10769, 10770, 10771, 10772, 10773, 10774, 10775, 10776, 10777, 10778, 10779, 10780, 10781, 10782, 10783, 10784, 10785, 10786, 10787, 10788, 10789, 10790, 10791, 10792, 10793, 10794, 10795, 10796, 10797, 10798, 10799, 10800, 10801, 10802, 10803, 10804, 10805, 10806, 10807, 10808, 10809, 10810, 10811, 10812, 10813, 10814, 10815, 10816, 10817, 10818, 10819, 10820, 10821, 10822, 10823, 10824, 10825, 10826, 10827, 10828, 10829, 10830, 10831, 10832, 10833, 10834, 10835, 10836, 10837, 10838, 10839, 10840, 10841, 10842, 10843, 10844, 10845, 10846, 10847, 10848, 10849, 10850, 10851, 10852, 10853, 10854, 10855, 10856, 10857, 10858, 10859, 10860, 10861, 10862, 10863, 10864, 11078, 11079, 11080, 11081, 11082, 11083, 11084, 11085, 11086, 11087, 11088, 11089, 11090, 11091, 11092, 11093, 11094, 11095, 11096, 11097, 11098, 11099, 11100, 11101, 11102, 11103, 11104, 11105, 11106, 11107, 11108, 11109, 11110, 11111, 11112, 11113, 11114, 11115, 11116, 11117, 11118, 11119, 11120, 11121, 11122, 11123, 11124), class = "Date"), pc1 = c(2.64462123197862, 2.4380313244096, 2.21417935009087, 2.02249236956036, 1.75829175459456, 1.58770371446918, 1.62230139615394, 1.73502227021784, 1.75083678213192, 1.64509065138032, 1.57921033180313, 1.70228767677341, 1.77303175099386, 1.78384290706931, 1.86580160595479, 1.9106874120324, 1.73936455049801, 1.50577059168685, 1.24226003967481, 1.07813468676617, 1.06276891964951, 1.09622663209529, 1.07692457712675, 0.978692818737612, 1.06365064520783, 1.2525349982313, 1.08237838015766, 0.645239033194787, 0.479482241789711, 0.683701830568681, 0.792197472275541, 0.631531270886538, 0.520337262457156, 0.667200695099021, 0.767559380073353, 0.7856163663635, 0.737745147101418, 0.654712633988225, 0.440140874164089, 0.111631055132755, -0.22450806112272, -0.444238159039355, -0.584576558346287, -0.444097467542865, -0.227821057355029, -0.120935149111578, -0.0932195161137341, 0.037283855810637, 0.206479031035409, 0.173515424607062, 0.234536409515456, 0.317957256707112, 0.290090191780606, 0.0607339338833623, -0.27556992053308, -0.3586166955826, -0.3534130521313, -0.501651666926942, -0.571570071652576, -0.79110428934397, -0.985635595643097, -0.994138228085185, -0.839909782593256, -0.699274458194957, -0.580683825031177, -0.530811870371419, -0.4746353951302, -0.489386570992314, -0.787222651887671, -1.1059671054324, -1.17983265148469, -1.1058432515423, -0.970485807735322, -0.679713450749357, -0.516950863200668, -0.495312393712548, -0.673645368786615, -0.792675131421433, -0.692021409445821, -0.611096320716252, -0.676712376641795, -0.723244566814595, -0.621986199057006, -0.563969216349158, -0.649311354664407, -0.679237194242732, -0.624476984795223, -0.738344795218295, -0.877867797047079, -0.879375052767018, -0.84262582765393, -0.845707036138972, -0.959691974084994, -1.06904324062176, -0.97905489332525, -0.847145240762566, -0.86837819324592, -0.935323976060101, -0.796486491787169, -0.461073031709012, -0.275818888900351, -0.513613296467615, -0.786611502858454, -0.799843667083875, -0.632676241199403, -0.468611824279096, -0.534017599627378, -0.501551518704511, -0.239313348556757, -0.208935210151003, -0.510483950549102, -0.62974750963569, -0.399113422985878, -0.072812659658845, 0.0377885597304766, -0.0102829082610216, -0.0571349366394233, -0.101917027852624, -0.202941574141862, -0.22849727264844, -0.125157862652187, 0.168703915373856, 0.43626132948925, 0.446099489882147, 0.435379929023588, 0.236210503991287, -0.122289033919648, -0.288101855449495, -0.186400543130663, 0.0316721901308679, 0.121240481805255, -0.0753698973566349, -0.384779730900963, -0.531179497125517, -0.373632181420806, -0.0148926315001478, 0.146040939981569, 0.13371186468668, 0.200262938351445, 0.465073170745138, 0.506805629621484, 0.345398737766814, 0.171110245173291, 0.176555396235594, 0.262743070740985, 0.398601589660576, 0.433248104072272, 0.453883432665361, 0.604637145172226, 0.843278371818699, 1.13506306230201, 1.42652005730684, 1.63221068108998, 1.86442509826484, 1.97067279998339, 2.0139860665512, 2.13720187260212, 2.31355711206366, 2.32477728002809, 2.36236228869303, 2.24108767618426, 2.12991693141636, 2.11677885248848, 2.01466853738993, 1.77967782944265, 1.48938981000699, 1.34042958586002, 1.33016846412245, 1.31770813627339, 1.26104969519401, 1.37385446004522, 1.61517275597383, 1.84510291043685, 1.91280500843462, 1.84897419443657, 1.52674793906846, 1.29429812528379, 1.06717755247561, 0.910500917679731, 0.904461327314293, 1.05380123048097, 1.08631739987863, 1.04843964584885, 1.10153891962662, 1.15936307711726, 1.20129772010444, 1.18746954945955, 1.00056619329093, 0.725225823060771, 0.573790799694267, 0.655776789864271, 0.780033607405981, 0.664875593837605, 0.452000300833336, 0.394589410057676, 0.402170545544567, 0.403979206396259, 0.395485848597801, 0.433314713756909, 0.437960603442615)), row.names = c(NA, 200L), class = "data.frame")strong text
1) Using the input in the Note at the end expand the dates to include the missing ones using NA's for them. Then plot. library(ggplot2) library(zoo) z <- read.zoo(ba) zz <- merge(z, zoo(, seq(start(z), end(z), 1))) autoplot(zz) + xlab("") 2) Another approach is to use distinct facets for each year. library(ggplot2) breaks <- unique(as.Date(cut(ba$date1, "month"))) ba2 <- transform(ba, year = as.integer(format(date1, "%Y"))) p <- ggplot(ba2, aes(date1, pc1)) + geom_line() + facet_grid(cols = vars(year), scales = "free_x", space = "free_x") p + scale_x_date(breaks = breaks, date_labels = "%b") (continued after image) or to remove strip text and only place the year below each facet use p from above with breaks <- as.Date(tapply(format(ba$date1), format(ba$date1, "%Y"), min)) p + scale_x_date(breaks = breaks, date_labels = "%Y") + theme(strip.text.x = element_blank()) 3) The facet idea could also be implemented in lattice. library(lattice) ba3 <- transform(ba, year = format(date1, "%Y")) xyplot(pc1 ~ date1 | year, ba3, type = "l", scales = list(x = list(relation = "free")), layout = c(NA, 1)) Note There was a problem with the dput output in the question so the following was used. ba <- structure(list(date1 = structure(c(10712, 10713, 10714, 10715, 10716, 10717, 10718, 10719, 10720, 10721, 10722, 10723, 10724, 10725, 10726, 10727, 10728, 10729, 10730, 10731, 10732, 10733, 10734, 10735, 10736, 10737, 10738, 10739, 10740, 10741, 10742, 10743, 10744, 10745, 10746, 10747, 10748, 10749, 10750, 10751, 10752, 10753, 10754, 10755, 10756, 10757, 10758, 10759, 10760, 10761, 10762, 10763, 10764, 10765, 10766, 10767, 10768, 10769, 10770, 10771, 10772, 10773, 10774, 10775, 10776, 10777, 10778, 10779, 10780, 10781, 10782, 10783, 10784, 10785, 10786, 10787, 10788, 10789, 10790, 10791, 10792, 10793, 10794, 10795, 10796, 10797, 10798, 10799, 10800, 10801, 10802, 10803, 10804, 10805, 10806, 10807, 10808, 10809, 10810, 10811, 10812, 10813, 10814, 10815, 10816, 10817, 10818, 10819, 10820, 10821, 10822, 10823, 10824, 10825, 10826, 10827, 10828, 10829, 10830, 10831, 10832, 10833, 10834, 10835, 10836, 10837, 10838, 10839, 10840, 10841, 10842, 10843, 10844, 10845, 10846, 10847, 10848, 10849, 10850, 10851, 10852, 10853, 10854, 10855, 10856, 10857, 10858, 10859, 10860, 10861, 10862, 10863, 10864, 11078, 11079, 11080, 11081, 11082, 11083, 11084, 11085, 11086, 11087, 11088, 11089, 11090, 11091, 11092, 11093, 11094, 11095, 11096, 11097, 11098, 11099, 11100, 11101, 11102, 11103, 11104, 11105, 11106, 11107, 11108, 11109, 11110, 11111, 11112, 11113, 11114, 11115, 11116, 11117, 11118, 11119, 11120, 11121, 11122, 11123, 11124), class = "Date"), pc1 = c(2.64462123197862, 2.4380313244096, 2.21417935009087, 2.02249236956036, 1.75829175459456, 1.58770371446918, 1.62230139615394, 1.73502227021784, 1.75083678213192, 1.64509065138032, 1.57921033180313, 1.70228767677341, 1.77303175099386, 1.78384290706931, 1.86580160595479, 1.9106874120324, 1.73936455049801, 1.50577059168685, 1.24226003967481, 1.07813468676617, 1.06276891964951, 1.09622663209529, 1.07692457712675, 0.978692818737612, 1.06365064520783, 1.2525349982313, 1.08237838015766, 0.645239033194787, 0.479482241789711, 0.683701830568681, 0.792197472275541, 0.631531270886538, 0.520337262457156, 0.667200695099021, 0.767559380073353, 0.7856163663635, 0.737745147101418, 0.654712633988225, 0.440140874164089, 0.111631055132755, -0.22450806112272, -0.444238159039355, -0.584576558346287, -0.444097467542865, -0.227821057355029, -0.120935149111578, -0.0932195161137341, 0.037283855810637, 0.206479031035409, 0.173515424607062, 0.234536409515456, 0.317957256707112, 0.290090191780606, 0.0607339338833623, -0.27556992053308, -0.3586166955826, -0.3534130521313, -0.501651666926942, -0.571570071652576, -0.79110428934397, -0.985635595643097, -0.994138228085185, -0.839909782593256, -0.699274458194957, -0.580683825031177, -0.530811870371419, -0.4746353951302, -0.489386570992314, -0.787222651887671, -1.1059671054324, -1.17983265148469, -1.1058432515423, -0.970485807735322, -0.679713450749357, -0.516950863200668, -0.495312393712548, -0.673645368786615, -0.792675131421433, -0.692021409445821, -0.611096320716252, -0.676712376641795, -0.723244566814595, -0.621986199057006, -0.563969216349158, -0.649311354664407, -0.679237194242732, -0.624476984795223, -0.738344795218295, -0.877867797047079, -0.879375052767018, -0.84262582765393, -0.845707036138972, -0.959691974084994, -1.06904324062176, -0.97905489332525, -0.847145240762566, -0.86837819324592, -0.935323976060101, -0.796486491787169, -0.461073031709012, -0.275818888900351, -0.513613296467615, -0.786611502858454, -0.799843667083875, -0.632676241199403, -0.468611824279096, -0.534017599627378, -0.501551518704511, -0.239313348556757, -0.208935210151003, -0.510483950549102, -0.62974750963569, -0.399113422985878, -0.072812659658845, 0.0377885597304766, -0.0102829082610216, -0.0571349366394233, -0.101917027852624, -0.202941574141862, -0.22849727264844, -0.125157862652187, 0.168703915373856, 0.43626132948925, 0.446099489882147, 0.435379929023588, 0.236210503991287, -0.122289033919648, -0.288101855449495, -0.186400543130663, 0.0316721901308679, 0.121240481805255, -0.0753698973566349, -0.384779730900963, -0.531179497125517, -0.373632181420806, -0.0148926315001478, 0.146040939981569, 0.13371186468668, 0.200262938351445, 0.465073170745138, 0.506805629621484, 0.345398737766814, 0.171110245173291, 0.176555396235594, 0.262743070740985, 0.398601589660576, 0.433248104072272, 0.453883432665361, 0.604637145172226, 0.843278371818699, 1.13506306230201, 1.42652005730684, 1.63221068108998, 1.86442509826484, 1.97067279998339, 2.0139860665512, 2.13720187260212, 2.31355711206366, 2.32477728002809, 2.36236228869303, 2.24108767618426, 2.12991693141636, 2.11677885248848, 2.01466853738993, 1.77967782944265, 1.48938981000699, 1.34042958586002, 1.33016846412245, 1.31770813627339, 1.26104969519401, 1.37385446004522, 1.61517275597383, 1.84510291043685, 1.91280500843462, 1.84897419443657, 1.52674793906846, 1.29429812528379, 1.06717755247561, 0.910500917679731, 0.904461327314293, 1.05380123048097, 1.08631739987863, 1.04843964584885, 1.10153891962662, 1.15936307711726, 1.20129772010444, 1.18746954945955, 1.00056619329093, 0.725225823060771, 0.573790799694267, 0.655776789864271, 0.780033607405981, 0.664875593837605, 0.452000300833336, 0.394589410057676, 0.402170545544567, 0.403979206396259, 0.395485848597801, 0.433314713756909, 0.437960603442615)), row.names = c(NA, -200L), class = "data.frame")
#R shiny non-numeric argument to binary operator
I am trying to make a problem more reactive to new inputs here is where the error is given Pwh_Design=reactive((input$Pwh+0.2*(input$Pso-input$Pwh))) Gtd=Pwh_Design-P_operating_point_formation/(0-D_operating_point) this is in the server. the entire app: UI: library(shiny) # Define UI for application that draws a histogram shinyUI(fluidPage( # Application title titlePanel("OTIS Gas Lift"), fluidRow(column(width = 6, numericInput(inputId = "Dmax",label = "Depth to mid perforation",value = 7500), numericInput("Pwh","Pwh",100), numericInput("Tres","Tres",182), numericInput("Pso","P Casing at Surface",870), numericInput("Psc","P Casing at Depth D1",1050), numericInput("D1","D1",7000), numericInput("P_across_valve","P_across_valve",100), numericInput("GL","Load Grad",0.5), numericInput("Pwf","Pwf",1760), numericInput("RGOR","Required GOR",400) ), column(width = 6, numericInput("FGOR","Formation GOR",200), numericInput("Twh","Twh",100), numericInput("Pko","kill pressure at Surface",920), numericInput("Pk","Kill Pressure at D2",1100), numericInput("D2","D2",7000), numericInput("T_inj","T_inj",100), numericInput("q","Desired Production Rate STB/d",600), numericInput("R","R",0.1534), numericInput("Gf","Flowing Grad before inj",0.4) )), fluidRow(submitButton(text = "Apply Changes",width = "100%")), splitLayout(tableOutput(outputId = "table"),plotOutput(outputId = "plot")))) ''' This is the server library(shiny) shinyServer(function(input, output) { Dmax=reactiveValues() #to mid perforation ft #one of those two next must be given FGOR=reactiveValues() #scf/STB Formation GOR Pwh=reactiveValues() #psig Twh=reactiveValues() #degree fahrenheit Tres=reactiveValues() #degree fahrenheit #casing and kill gradients Pso=reactiveValues() #psig Pcs=reactiveValues() #psig at 7000ft D1=reactiveValues() #ft Pko=reactiveValues() #psig Pk=reactiveValues() #psig at 7000ft D2=reactiveValues() #ft P_across_valve=reactiveValues() #psig T_inj=reactiveValues() #degree fahrenheit GL=reactiveValues() #load gradient (kill fluid grad) q=reactiveValues() Pwf=reactiveValues() #psi or BHP RGOR=reactiveValues() #scf/STB required GOR R=reactiveValues() Gf=reactiveValues() #flowing gradient before injection default value library(ggplot2) library(dplyr) #plot basics plot<-reactive(ggplot+ coord_cartesian(xlim = c(0,input$Pwf+500))+ scale_y_continuous(trans = "reverse")+ scale_x_continuous(position = "top")) #Flowing Gradient #slope Gf #point (pwf,Dmax) #get p1 at depth 4000ft p1=reactive(input$Pwf-input$Gf*(input$Dmax-4000)) flowing_gradient<-reactive(data.frame(D=c(4000,input$Dmax),P=c(p1,input$Pwf))) plot0<-plot+ geom_point(data = flowing_gradient,aes(x=P,y=D))+ geom_line(data = flowing_gradient,aes(x=P,y=D)) #casing gradient casing_gradient<-reactive(data.frame(D=c(0,input$D1),P=c(input$Pso,input$Pcs))) plot1<-plot0+geom_point(data=casing_gradient,aes(x=P,y=D))+ geom_line(data=casing_gradient,aes(x=P,y=D)) Gc=reactive((input$Pso-input$Pcs)/(0-input$D1)) #killing gradient killing_gradient<-reactive(data.frame(D=c(0,input$D2),P=c(input$Pko,input$Pk))) plot2<-plot1+geom_point(data=killing_gradient,aes(x=P,y=D))+ geom_line(data=killing_gradient,aes(x=P,y=D)) Gk=reactive((input$Pko-input$Pk)/(0-input$D2)) #balance point d1=reactive((input$Pwf-input$Pso-input$Gf*input$Dmax)/(Gc-input$Gf)) d_balance_point=d1 #operating_point d1=reactive((input$Pso-input$Pwf+input$Gf*input$Dmax-input$P_across_valve)/(input$Gf-Gc)) D_operating_point=d1 P_operating_point_casing=reactive(input$Pso+D_operating_point*Gc) P_operating_point_formation=reactive(input$Pwf-input$Gf*(input$Dmax-D_operating_point)) plot3<-plot2+geom_point(aes(x=P_operating_point_formation,y=D_operating_point)) #Tubing grad tubing_grad<-reactive(data.frame(D=c(0,D_operating_point),P=c(input$Pwh,P_operating_point_formation))) plot4<-plot3+geom_point(data = tubing_grad,aes(x=P,y=D))+ geom_line(data = tubing_grad,aes(x=P,y=D)) #Tubing_design_grad Pwh_Design=reactive((input$Pwh+0.2*(input$Pso-input$Pwh))) Gtd=Pwh_Design-P_operating_point_formation/(0-D_operating_point) #Tubing design grad tubing_design_grad<-data.frame(D=c(0,D_operating_point),P=c(Pwh_Design,P_operating_point_formation)) plot5<-plot4+geom_point(data = tubing_design_grad,aes(x=P,y=D))+ geom_line(data = tubing_design_grad,aes(x=P,y=D)) #first valve D_valve1=reactive((input$Pwh-input$Pko)/(Gk-input$GL)) PK_valve1=reactive(D_valve1*Gk+input$Pko) PTD_valve1=Gtd*D_valve1+Pwh_Design df<-reactive(data.frame(D=c(0,D_valve1),P=c(input$Pwh,PK_valve1))) #ploting plot6<-plot5+geom_point(data = df, aes(x=P,y=D))+ geom_line(data = df, aes(x=P,y=D)) df<-data.frame(D=c(D_valve1,D_valve1),P=c(PTD_valve1,PK_valve1)) plot7<-plot6+geom_point(data = df, aes(x=P,y=D))+ geom_line(data = df, aes(x=P,y=D)) plot<-plot7 Final_table<-data.frame(depth=D_valve1, Pc=PK_valve1, Pt=PTD_valve1) dvalve=D_valve1 PTDvalve=PTD_valve1 Pcvalve=PK_valve1 D_valve=D_valve1 PTD_valve=PTD_valve1 while (dvalve<D_operating_point) { dvalve=reactive((PTDvalve-input$Pso-dvalve*input$GL)/(Gc-input$GL)) Pcvalve=reactive(input$Pso+Gc*dvalve) if(dvalve<D_operating_point){ df<-data.frame(D=c(D_valve,dvalve),P=c(PTD_valve,Pcvalve)) plot<-plot+geom_point(data = df, aes(x=P,y=D))+ geom_line(data = df, aes(x=P,y=D)) PTDvalve=Gtd*dvalve+Pwh_Design df<-data.frame(D=c(dvalve,dvalve),P=c(PTDvalve,Pcvalve)) plot<-plot+geom_point(data = df, aes(x=P,y=D))+ geom_line(data = df, aes(x=P,y=D)) valve<-c(dvalve,Pcvalve,PTDvalve) Final_table<-rbind(Final_table,valve) D_valve=dvalve PTD_valve=PTDvalve }} output$plot<-renderPlot(plot) valve<-c(D_operating_point,P_operating_point_casing,P_operating_point_formation) Final_table<-rbind(Final_table,valve) GTemp=reactive((input$Tres-input$Twh)/input$Dmax) CT_data<-data.frame(Temp=c(61:300),Ct=c(0.998,0.996,0.994,0.991,0.989,0.987,0.985,0.983,0.981,0.979,0.977,0.975,0.973,0.971,0.969,0.967,0.965,0.963,0.961,0.959,0.957,0.955,0.953,0.951,0.949,0.947,0.945,0.943,0.941,0.939,0.938,0.936,0.934,0.932,0.93,0.928,0.926,0.924,0.923,0.921,0.919,0.917,0.915,0.914,0.912,0.91,0.908,0.906,0.905,0.903,0.901,0.899,0.898,0.896,0.894,0.893,0.891,0.889,0.887,0.886,0.884,0.882,0.881,0.879,0.877,0.876,0.874,0.872,0.871,0.869,0.686,0.866,0.864,0.863,0.861,0.86,0.858,0.856,0.855,0.853,0.852,0.85,0.849,0.847,0.845,0.844,0.842,0.841,0.839,0.838,0.836,0.835,0.833,0.832,0.83,0.829,0.827,0.826,0.825,0.823,0.822,0.82,0.819,0.817,0.816,0.814,0.813,0.812,0.81,0.809,0.807,0.806,0.805,0.803,0.802,0.8,0.799,0.798,0.796,0.795,0.794,0.792,0.791,0.79,0.788,0.787,0.786,0.784,0.783,0.782,0.78,0.779,0.778,0.776,0.775,0.774,0.772,0.771,0.77,0.769,0.767,0.766,0.765,0.764,0.762,0.761,0.76,0.759,0.757,0.756,0.755,0.754,0.752,0.751,0.75,0.749,0.748,0.746,0.745,0.744,0.743,0.742,0.74,0.739,0.738,0.737,0.736,0.735,0.733,0.732,0.731,0.73,0.729,0.728,0.727,0.725,0.724,0.723,0.722,0.721,0.72,0.719,0.718,0.717,0.715,0.714,0.713,0.712,0.711,0.71,0.709,0.708,0.707,0.706,0.705,0.704,0.702,0.701,0.7,0.699,0.698,0.697,0.696,0.695,0.694,0.693,0.692,0.691,0.69,0.689,0.688,0.687,0.686,0.685,0.684,0.683,0.682,0.681,0.68,0.679,0.678,0.677,0.676,0.675,0.674,0.673,0.672,0.671,0.67,0.699,0.668,0.667,0.666,0.665,0.664,0.663,0.662,0.662,0.661,0.66)) Final_table<-Final_table %>% mutate(reactive(Pbt=input$Pc*(1-input$R)+Pt*input$R), reactive(TEMP=input$Twh+GTemp*depth), Temp=ceiling(TEMP)) Final_table<-merge(x=Final_table,y=CT_data,by = "Temp",all.x = T) Final_table<-Final_table%>%select(depth,Pc,Pt,Pbt,TEMP,Ct) Final_table<-Final_table %>% mutate(Pb=Pbt*Ct, Pvo=reactive(Pb/(1-input$R))) output$table<-tableOutput(Final_table)}) I tried making any variable with reactive assignment included in () but it gives this error4 "Warning: Error in : Operation not allowed without an active reactive context. You tried to do something that can only be done from inside a reactive consumer. 58: Error : Operation not allowed without an active reactive context. You tried to do something that can only be done from inside a reactive consumer." I am pretty sure i did it to only the reactive assigned variables when recalling them EDIT After I tried to solve the problem it gives: Warning: Error in data.frame: arguments imply differing number of rows: 1, 0 1: runApp whenever I press the action button the new server library(shiny) library(ggplot2) library(dplyr) shinyServer(function(input, output) { plot<-reactiveVal(NULL) Final_table<-reactiveVal(data.frame()) observeEvent(input$submit,{ plot(ggplot()+ coord_cartesian(xlim = c(0,input$Pwf+500))+ scale_y_continuous(trans = "reverse")+ scale_x_continuous(position = "top")) #Flowing Gradient #slope Gf #point (pwf,Dmax) #get p1 at depth 4000ft p1=input$Pwf-input$Gf*(input$Dmax-4000) flowing_gradient<-data.frame(D=c(4000,input$Dmax),P=c(p1,input$Pwf)) plot(plot()+ geom_point(data = flowing_gradient,aes(x=P,y=D))+ geom_line(data = flowing_gradient,aes(x=P,y=D))) #casing gradient casing_gradient<-data.frame(D=c(0,input$D1),P=c(input$Pso,input$Pcs)) plot(plot()+geom_point(data=casing_gradient,aes(x=P,y=D))+ geom_line(data=casing_gradient,aes(x=P,y=D))) Gc=(input$Pso-input$Pcs)/(0-input$D1) #killing gradient killing_gradient<-data.frame(D=c(0,input$D2),P=c(input$Pko,input$Pk)) plot(plot()+geom_point(data=killing_gradient,aes(x=P,y=D))+ geom_line(data=killing_gradient,aes(x=P,y=D))) Gk=(input$Pko-input$Pk)/(0-input$D2) #balance point d1=(input$Pwf-input$Pso-input$Gf*input$Dmax)/(Gc-input$Gf) d_balance_point=d1 #operating_point d1=(input$Pso-input$Pwf+input$Gf*input$Dmax-input$P_across_valve)/(input$Gf-Gc) D_operating_point=d1 P_operating_point_casing=input$Pso+D_operating_point*Gc P_operating_point_formation=input$Pwf-input$Gf*(input$Dmax-D_operating_point) plot(plot()+geom_point(aes(x=P_operating_point_formation,y=D_operating_point))) #Tubing grad tubing_grad<-data.frame(D=c(0,D_operating_point),P=c(input$Pwh,P_operating_point_formation)) plot(plot()+geom_point(data = tubing_grad,aes(x=P,y=D))+ geom_line(data = tubing_grad,aes(x=P,y=D))) #Tubing_design_grad Pwh_design=input$Pwh+0.2*(input$Pso-input$Pwh) Gtd=(Pwh_design-P_operating_point_formation)/(0-D_operating_point) #Tubing design grad tubing_design_grad<-data.frame(D=c(0,D_operating_point),P=c(Pwh_design,P_operating_point_formation)) plot(plot()+geom_point(data = tubing_design_grad,aes(x=P,y=D))+ geom_line(data = tubing_design_grad,aes(x=P,y=D))) #first valve D_valve1=(input$Pwh-input$Pko)/(Gk-input$GL) PK_valve1=D_valve1*Gk+input$Pko PTD_valve1=Gtd*D_valve1+Pwh_design df<-data.frame(D=c(0,D_valve1),P=c(input$Pwh,PK_valve1)) #ploting plot(plot()+geom_point(data = df, aes(x=P,y=D))+ geom_line(data = df, aes(x=P,y=D))) df<-data.frame(D=c(D_valve1,D_valve1),P=c(PTD_valve1,PK_valve1)) plot(plot()+geom_point(data = df, aes(x=P,y=D))+ geom_line(data = df, aes(x=P,y=D))) Final_table(data.frame(depth=D_valve1, Pc=PK_valve1, Pt=PTD_valve1)) dvalve=D_valve1 PTDvalve=PTD_valve1 Pcvalve=PK_valve1 D_valve=D_valve1 PTD_valve=PTD_valve1 while (dvalve<D_operating_point) { dvalve=(PTDvalve-input$Pso-dvalve*input$GL)/(Gc-input$GL) Pcvalve=input$Pso+Gc*dvalve if(dvalve<D_operating_point){ df<-data.frame(D=c(D_valve,dvalve),P=c(PTD_valve,Pcvalve)) plot(plot()+geom_point(data = df, aes(x=P,y=D))+ geom_line(data = df, aes(x=P,y=D))) PTDvalve=Gtd*dvalve+Pwh_design df<-data.frame(D=c(dvalve,dvalve),P=c(PTDvalve,Pcvalve)) plot(plot()+geom_point(data = df, aes(x=P,y=D))+ geom_line(data = df, aes(x=P,y=D))) valve<-c(dvalve,Pcvalve,PTDvalve) Final_table(rbind(Final_table(),valve)) D_valve=dvalve PTD_valve=PTDvalve }} valve<-c(D_operating_point,P_operating_point_casing,P_operating_point_formation) Final_table(rbind(Final_table(),valve)) GTemp=(input$Tres-input$Twh)/input$Dmax CT_data<-data.frame(Temp=c(61:300),Ct=c(0.998,0.996,0.994,0.991,0.989,0.987,0.985,0.983,0.981,0.979,0.977,0.975,0.973,0.971,0.969,0.967,0.965,0.963,0.961,0.959,0.957,0.955,0.953,0.951,0.949,0.947,0.945,0.943,0.941,0.939,0.938,0.936,0.934,0.932,0.93,0.928,0.926,0.924,0.923,0.921,0.919,0.917,0.915,0.914,0.912,0.91,0.908,0.906,0.905,0.903,0.901,0.899,0.898,0.896,0.894,0.893,0.891,0.889,0.887,0.886,0.884,0.882,0.881,0.879,0.877,0.876,0.874,0.872,0.871,0.869,0.686,0.866,0.864,0.863,0.861,0.86,0.858,0.856,0.855,0.853,0.852,0.85,0.849,0.847,0.845,0.844,0.842,0.841,0.839,0.838,0.836,0.835,0.833,0.832,0.83,0.829,0.827,0.826,0.825,0.823,0.822,0.82,0.819,0.817,0.816,0.814,0.813,0.812,0.81,0.809,0.807,0.806,0.805,0.803,0.802,0.8,0.799,0.798,0.796,0.795,0.794,0.792,0.791,0.79,0.788,0.787,0.786,0.784,0.783,0.782,0.78,0.779,0.778,0.776,0.775,0.774,0.772,0.771,0.77,0.769,0.767,0.766,0.765,0.764,0.762,0.761,0.76,0.759,0.757,0.756,0.755,0.754,0.752,0.751,0.75,0.749,0.748,0.746,0.745,0.744,0.743,0.742,0.74,0.739,0.738,0.737,0.736,0.735,0.733,0.732,0.731,0.73,0.729,0.728,0.727,0.725,0.724,0.723,0.722,0.721,0.72,0.719,0.718,0.717,0.715,0.714,0.713,0.712,0.711,0.71,0.709,0.708,0.707,0.706,0.705,0.704,0.702,0.701,0.7,0.699,0.698,0.697,0.696,0.695,0.694,0.693,0.692,0.691,0.69,0.689,0.688,0.687,0.686,0.685,0.684,0.683,0.682,0.681,0.68,0.679,0.678,0.677,0.676,0.675,0.674,0.673,0.672,0.671,0.67,0.699,0.668,0.667,0.666,0.665,0.664,0.663,0.662,0.662,0.661,0.66)) Final_table(Final_table() %>% mutate(Pbt=Pc*(1-input$R)+Pt*input$R, TEMP=input$Twh+GTemp*depth, Temp=ceiling(TEMP))) Final_table(merge(x=Final_table(),y=CT_data,by = "Temp",all.x = T)) Final_table(Final_table()%>%select(depth,Pc,Pt,Pbt,TEMP,Ct)) Final_table(Final_table() %>% mutate(Pb=Pbt*Ct, Pvo=Pb/(1-input$R))) }) output$plot<-renderPlot({ isolate(plot()) }) output$table<-renderTable({ isolate(Final_table()) }) })
Here's a MRE up to the first plot to ilustrate how to deal with reactive objects. Notice the use of () when accessing a reactive value. library(shiny) library(tidyverse) # Define UI for application that draws a histogram ui <- fluidPage( # Application title titlePanel("OTIS Gas Lift"), fluidRow(column(width = 6, numericInput(inputId = "Dmax",label = "Depth to mid perforation",value = 7500), numericInput("Pwh","Pwh",100), numericInput("Tres","Tres",182), numericInput("Pso","P Casing at Surface",870), numericInput("Psc","P Casing at Depth D1",1050), numericInput("D1","D1",7000), numericInput("P_across_valve","P_across_valve",100), numericInput("GL","Load Grad",0.5), numericInput("Pwf","Pwf",1760), numericInput("RGOR","Required GOR",400) ), column(width = 6, numericInput("FGOR","Formation GOR",200), numericInput("Twh","Twh",100), numericInput("Pko","kill pressure at Surface",920), numericInput("Pk","Kill Pressure at D2",1100), numericInput("D2","D2",7000), numericInput("T_inj","T_inj",100), numericInput("q","Desired Production Rate STB/d",600), numericInput("R","R",0.1534), numericInput("Gf","Flowing Grad before inj",0.4) )), fluidRow(actionButton('submit' ,label = "Apply Changes",width = "100%")), splitLayout(tableOutput(outputId = "table"),plotOutput(outputId = "plot")) ) library(shiny) server <- function(input, output) { #define plot object that can be accessed inside of reactive() or observe() plot_rv <- reactiveVal(NULL) flowing_gradient_rv <- reactiveVal(NULL) #inside this observer we can create regular local objects (only accesible inside the observer) like when creating functions. #notice the code wrapped in curly braces {} to be able to pass multiple lines of code into `observeEvent()` second argument. #input$submit corresponds to the action button created in the ui. observeEvent(input$submit, { plot_rv(ggplot() + coord_cartesian(xlim = c(0, input$Pwf + 500)) + scale_y_continuous(trans = "reverse") + scale_x_continuous(position = "top")) #Flowing Gradient #slope Gf #point (pwf,Dmax) #get p1 at depth 4000ft #input values are now available without calling reactive since we already are "inside" the observer p1 <- input$Pwf - input$Gf * (input$Dmax - 4000) flowing_gradient <- data.frame(D = c(4000, input$Dmax), P = c(p1, input$Pwf)) #to use flowing_gradient outside this observer we use flowing_gradient reactiveVal flowing_gradient_rv(flowing_gradient) plot_rv(plot_rv() + geom_point(data = flowing_gradient,aes(x=P,y=D))+ geom_line(data = flowing_gradient,aes(x=P,y=D)) ) }) #to show the plots output$plot <- renderPlot({ plot_rv() }) output$table <- renderTable( flowing_gradient_rv() ) } shinyApp(ui, server) Edit: The edited app currently working. library(shiny) library(tidyverse) # Define UI for application that draws a histogram ui <- fluidPage( # Application title titlePanel("OTIS Gas Lift"), fluidRow( column( width = 6, numericInput(inputId = "Dmax", label = "Depth to mid perforation", value = 7500), numericInput("Pwh", "Pwh", 100), numericInput("Tres", "Tres", 182), numericInput("Pso", "P Casing at Surface", 870), numericInput("Psc", "P Casing at Depth D1", 1050), numericInput("D1", "D1", 7000), numericInput("P_across_valve", "P_across_valve", 100), numericInput("GL", "Load Grad", 0.5), numericInput("Pwf", "Pwf", 1760), numericInput("RGOR", "Required GOR", 400) ), column( width = 6, numericInput("FGOR", "Formation GOR", 200), numericInput("Twh", "Twh", 100), numericInput("Pko", "kill pressure at Surface", 920), numericInput("Pk", "Kill Pressure at D2", 1100), numericInput("D2", "D2", 7000), numericInput("T_inj", "T_inj", 100), numericInput("q", "Desired Production Rate STB/d", 600), numericInput("R", "R", 0.1534), numericInput("Gf", "Flowing Grad before inj", 0.4) ) ), fluidRow(actionButton("submit", label = "Apply Changes", width = "100%")), splitLayout(tableOutput(outputId = "table"), plotOutput(outputId = "plot")) ) library(shiny) library(shiny) library(ggplot2) library(dplyr) server <- function(input, output) { plot <- reactiveVal(NULL) Final_table <- reactiveVal(data.frame()) observeEvent(input$submit, { plot(ggplot() + coord_cartesian(xlim = c(0, input$Pwf + 500)) + scale_y_continuous(trans = "reverse") + scale_x_continuous(position = "top")) # Flowing Gradient # slope Gf # point (pwf,Dmax) # get p1 at depth 4000ft p1 <- input$Pwf - input$Gf * (input$Dmax - 4000) flowing_gradient <- data.frame(D = c(4000, input$Dmax), P = c(p1, input$Pwf)) plot(plot() + geom_point(data = flowing_gradient, aes(x = P, y = D)) + geom_line(data = flowing_gradient, aes(x = P, y = D))) # casing gradient casing_gradient <- data.frame(D = c(0, input$D1), P = c(input$Pso, input$Pcs)) plot(plot() + geom_point(data = casing_gradient, aes(x = P, y = D)) + geom_line(data = casing_gradient, aes(x = P, y = D))) Gc <- (input$Pso - input$Psc) / (0 - input$D1) # killing gradient killing_gradient <- data.frame(D = c(0, input$D2), P = c(input$Pko, input$Pk)) plot(plot() + geom_point(data = killing_gradient, aes(x = P, y = D)) + geom_line(data = killing_gradient, aes(x = P, y = D))) Gk <- (input$Pko - input$Pk) / (0 - input$D2) # balance point d1 <- (input$Pwf - input$Pso - input$Gf * input$Dmax) / (Gc - input$Gf) d_balance_point <- d1 # operating_point d1 <- (input$Pso - input$Pwf + input$Gf * input$Dmax - input$P_across_valve) / (input$Gf - Gc) D_operating_point <- d1 P_operating_point_casing <- input$Pso + D_operating_point * Gc P_operating_point_formation <- input$Pwf - input$Gf * (input$Dmax - D_operating_point) plot(plot() + geom_point(aes(x = P_operating_point_formation, y = D_operating_point))) # Tubing grad tubing_grad <- data.frame(D = c(0, D_operating_point), P = c(input$Pwh, P_operating_point_formation)) plot(plot() + geom_point(data = tubing_grad, aes(x = P, y = D)) + geom_line(data = tubing_grad, aes(x = P, y = D))) # Tubing_design_grad Pwh_design <- input$Pwh + 0.2 * (input$Pso - input$Pwh) Gtd <- (Pwh_design - P_operating_point_formation) / (0 - D_operating_point)# Tubing design grad tubing_design_grad <- data.frame(D = c(0, D_operating_point), P = c(Pwh_design, P_operating_point_formation)) plot(plot() + geom_point(data = tubing_design_grad, aes(x = P, y = D)) + geom_line(data = tubing_design_grad, aes(x = P, y = D))) # first valve D_valve1 <- (input$Pwh - input$Pko) / (Gk - input$GL) PK_valve1 <- D_valve1 * Gk + input$Pko PTD_valve1 <- Gtd * D_valve1 + Pwh_design df <- data.frame(D = c(0, D_valve1), P = c(input$Pwh, PK_valve1)) # ploting plot(plot() + geom_point(data = df, aes(x = P, y = D)) + geom_line(data = df, aes(x = P, y = D))) df <- data.frame(D = c(D_valve1, D_valve1), P = c(PTD_valve1, PK_valve1)) plot(plot() + geom_point(data = df, aes(x = P, y = D)) + geom_line(data = df, aes(x = P, y = D))) Final_table(data.frame( depth = D_valve1, Pc = PK_valve1, Pt = PTD_valve1 )) dvalve <- D_valve1 PTDvalve <- PTD_valve1 Pcvalve <- PK_valve1 D_valve <- D_valve1 PTD_valve <- PTD_valve1 while (dvalve < D_operating_point) { dvalve <- (PTDvalve - input$Pso - dvalve * input$GL) / (Gc - input$GL) Pcvalve <- input$Pso + Gc * dvalve if (dvalve < D_operating_point) { df <- data.frame(D = c(D_valve, dvalve), P = c(PTD_valve, Pcvalve)) plot(plot() + geom_point(data = df, aes(x = P, y = D)) + geom_line(data = df, aes(x = P, y = D))) PTDvalve <- Gtd * dvalve + Pwh_design df <- data.frame(D = c(dvalve, dvalve), P = c(PTDvalve, Pcvalve)) plot(plot() + geom_point(data = df, aes(x = P, y = D)) + geom_line(data = df, aes(x = P, y = D))) valve <- c(dvalve, Pcvalve, PTDvalve) Final_table(rbind(Final_table(), valve)) D_valve <- dvalve PTD_valve <- PTDvalve } } valve <- c(D_operating_point, P_operating_point_casing, P_operating_point_formation) Final_table(rbind(Final_table(), valve)) GTemp <- (input$Tres - input$Twh) / input$Dmax CT_data <- data.frame(Temp = c(61:300), Ct = c(0.998, 0.996, 0.994, 0.991, 0.989, 0.987, 0.985, 0.983, 0.981, 0.979, 0.977, 0.975, 0.973, 0.971, 0.969, 0.967, 0.965, 0.963, 0.961, 0.959, 0.957, 0.955, 0.953, 0.951, 0.949, 0.947, 0.945, 0.943, 0.941, 0.939, 0.938, 0.936, 0.934, 0.932, 0.93, 0.928, 0.926, 0.924, 0.923, 0.921, 0.919, 0.917, 0.915, 0.914, 0.912, 0.91, 0.908, 0.906, 0.905, 0.903, 0.901, 0.899, 0.898, 0.896, 0.894, 0.893, 0.891, 0.889, 0.887, 0.886, 0.884, 0.882, 0.881, 0.879, 0.877, 0.876, 0.874, 0.872, 0.871, 0.869, 0.686, 0.866, 0.864, 0.863, 0.861, 0.86, 0.858, 0.856, 0.855, 0.853, 0.852, 0.85, 0.849, 0.847, 0.845, 0.844, 0.842, 0.841, 0.839, 0.838, 0.836, 0.835, 0.833, 0.832, 0.83, 0.829, 0.827, 0.826, 0.825, 0.823, 0.822, 0.82, 0.819, 0.817, 0.816, 0.814, 0.813, 0.812, 0.81, 0.809, 0.807, 0.806, 0.805, 0.803, 0.802, 0.8, 0.799, 0.798, 0.796, 0.795, 0.794, 0.792, 0.791, 0.79, 0.788, 0.787, 0.786, 0.784, 0.783, 0.782, 0.78, 0.779, 0.778, 0.776, 0.775, 0.774, 0.772, 0.771, 0.77, 0.769, 0.767, 0.766, 0.765, 0.764, 0.762, 0.761, 0.76, 0.759, 0.757, 0.756, 0.755, 0.754, 0.752, 0.751, 0.75, 0.749, 0.748, 0.746, 0.745, 0.744, 0.743, 0.742, 0.74, 0.739, 0.738, 0.737, 0.736, 0.735, 0.733, 0.732, 0.731, 0.73, 0.729, 0.728, 0.727, 0.725, 0.724, 0.723, 0.722, 0.721, 0.72, 0.719, 0.718, 0.717, 0.715, 0.714, 0.713, 0.712, 0.711, 0.71, 0.709, 0.708, 0.707, 0.706, 0.705, 0.704, 0.702, 0.701, 0.7, 0.699, 0.698, 0.697, 0.696, 0.695, 0.694, 0.693, 0.692, 0.691, 0.69, 0.689, 0.688, 0.687, 0.686, 0.685, 0.684, 0.683, 0.682, 0.681, 0.68, 0.679, 0.678, 0.677, 0.676, 0.675, 0.674, 0.673, 0.672, 0.671, 0.67, 0.699, 0.668, 0.667, 0.666, 0.665, 0.664, 0.663, 0.662, 0.662, 0.661, 0.66)) Final_table(Final_table() %>% mutate( Pbt = Pc * (1 - input$R) + Pt * input$R, TEMP = input$Twh + GTemp * depth, Temp = ceiling(TEMP) )) Final_table(merge(x = Final_table(), y = CT_data, by = "Temp", all.x = T)) Final_table(Final_table() %>% select(depth, Pc, Pt, Pbt, TEMP, Ct)) Final_table(Final_table() %>% mutate( Pb = Pbt * Ct, Pvo = Pb / (1 - input$R) )) }) output$plot <- renderPlot({ plot() }) output$table <- renderTable({ Final_table() }) } shinyApp(ui, server)
Formattable R package: conditionaly formatting the cells' content on the basis of a numeric threshold
I have a dataframe like the following mydata <- structure(list(Wife = c(15.972, 12.715, 8.333, 6.276, 2.179, -1.408, -1.649, -4.647, -7.039, -5.299, -7.411, -9.776, -9.612 ), Alternating = c(-2.622, -0.548, -1.331, 3.9, -1.802, 2.08, 1.481, 9.53, 7.709, -0.953, -4.823, -4.878, -5.245), Husband = c(-7.012, -5.823, -3.99, -3.324, -5.828, -4.869, -3.941, 0.515, 9.551, -0.868, 4.843, 24.544, -5.812), Jointly = c(-8.283, -7.569, -4.048, -6.564, 4.418, 4.284, 4.157, -3.006, -7.306, 7.066, 7.086, -8.306, 19.397)), row.names = c("Laundry", "Main_meal", "Dinner", "Breakfeast", "Tidying", "Dishes", "Shopping", "Official", "Driving", "Finances", "Insurance", "Repairs", "Holidays"), class = "data.frame") which lists the chi-square adjusted standardized residuals. Using the 'formattable' R package, I managed to get the below table (more nicely formatted than in the R console): Issue I cannot find a viable option to conditionally formatting some cells according to the size of the residual. What I am after is (for example) to have in GREEN the cells whose residual is larger than +1.96, and in RED those whose residual is smaller than -1.96. I have indeed consulted the package's vignette, with no avail.
library(formattable) library(dplyr) mydata <- structure(list(Wife = c( 15.972, 12.715, 8.333, 6.276, 2.179, -1.408, -1.649, -4.647, -7.039, -5.299, -7.411, -9.776, -9.612 ), Alternating = c( -2.622, -0.548, -1.331, 3.9, -1.802, 2.08, 1.481, 9.53, 7.709, -0.953, -4.823, -4.878, -5.245 ), Husband = c( -7.012, -5.823, -3.99, -3.324, -5.828, -4.869, -3.941, 0.515, 9.551, -0.868, 4.843, 24.544, -5.812 ), Jointly = c( -8.283, -7.569, -4.048, -6.564, 4.418, 4.284, 4.157, -3.006, -7.306, 7.066, 7.086, -8.306, 19.397 )), row.names = c( "Laundry", "Main_meal", "Dinner", "Breakfeast", "Tidying", "Dishes", "Shopping", "Official", "Driving", "Finances", "Insurance", "Repairs", "Holidays" ), class = "data.frame") style_ci <- function(x) { case_when(x > 1.96 ~ "green", x < -1.96 ~ "red", TRUE ~ "black") } formattable(mydata, list( Wife = formatter("span", style = ~ style(color = style_ci(Wife))), Alternating = formatter("span", style = ~ style(color = style_ci(Alternating))), Husband = formatter("span", style = ~ style(color = style_ci(Husband))), Jointly = formatter("span", style = ~ style(color = style_ci(Jointly))) ))
Add a 3d surface at point 0 in plotly
I have a 3D plot using plotly which can take on negative and positive values. I would like to add a see through 3D flat surface at point 0 (see through here means setting alpha = 0.3 as in ggplot2) in order to emphasize the positive/negative values a little more. Alternatively adding a "grid" as in the background through the point 0 would be useful. Code: library(plotly) library(dplyr) df %>% select(date, everything()) %>% plot_ly( x = colnames(subset(., select = c(2:4))), # probably not the most efficient method y = ~date, z = data.matrix(subset(., select = c(2:4))), type = "surface", colors = c("darkblue", "yellow", "darkred") ) Data: df <- structure(list(wind = c(0.938535690307617, 0.976551234722137, 0.954551994800568, 0.920722126960754, 0.889751732349396, 0.701366603374481, 0.718878328800201, 0.685763895511627, 0.677822828292847, 0.876205325126648, 0.67054146528244, 0.738650739192963, 0.725420415401459, 0.851324200630188, 0.6589714884758, 0.880357265472412, 0.997677683830261, 0.968335390090942, 0.882899045944214, 1.09453165531158, 1.06842839717865, 0.819347560405731, 0.995919525623322, 0.695173263549805, 0.860199570655823, 0.977508246898651, 0.807886302471161, 0.778182446956635, 0.886279463768005, 0.879809498786926, 0.903579652309418, 1.09579062461853, 1.03768181800842, 0.802022874355316, 0.928451955318451, 0.744936227798462, 0.621560990810394, 0.706887602806091, 0.749234974384308, 0.746754884719849, 0.65381270647049, 0.710927248001099, 0.517793655395508, 0.477172255516052, 0.631662607192993, 0.55011123418808, 0.584086775779724, 0.617783904075623, 0.438043504953384, 0.577566087245941, 0.684398949146271, 1.00848543643951, 0.957233726978302, 0.84071296453476, 0.90149587392807, 0.888661623001099, 0.923080563545227, 0.945727407932281, 0.965020060539246, 1.07903909683228, 0.870955109596252, 0.934546709060669, 0.93314516544342, 0.926109850406647, 0.981102645397186, 0.770903468132019, 0.829349219799042, 0.980291783809662, 1.00809383392334, 0.821328639984131, 0.89130437374115, 0.886775732040405, 0.896964132785797, 1.08184945583344, 1.17395043373108, 1.11537420749664, 1.15379846096039, 1.20203125476837, 1.12583827972412, 1.10014677047729, 0.891100168228149, 1.01484513282776, 1.01270127296448, 0.895487844944, 0.877909421920776, 1.11780989170074, 1.18047833442688, 1.14531397819519, 1.28634309768677, 1.23702371120453, 1.01186645030975, 1.15563869476318, 1.0918824672699, 1.2023059129715, 1.11806273460388, 1.11690294742584, 1.13390302658081, 1.10064888000488, 1.1418149471283, 1.06309700012207), holiday = c(-0.323977619409561, -0.32814821600914, -0.335260361433029, -0.355545252561569, -0.347469061613083, -0.349574476480484, -0.331554502248764, -0.351303607225418, -0.342486947774887, -0.34791961312294, -0.342795491218567, -0.344868391752243, -0.344075173139572, -0.342763870954514, -0.365125387907028, -0.301046937704086, -0.303365021944046, -0.301353365182877, -0.316689401865005, -0.312132269144058, -0.317412197589874, -0.331865310668945, -0.306569844484329, -0.306523144245148, -0.328399240970612, -0.328934848308563, -0.340233236551285, -0.310919582843781, -0.352815061807632, -0.324335247278214, -0.333071410655975, -0.25605234503746, -0.28141376376152, -0.267955660820007, -0.261935144662857, -0.23365119099617, -0.204256281256676, -0.271868377923965, -0.266900211572647, -0.268673747777939, -0.246791422367096, -0.261210560798645, -0.275096118450165, -0.272033154964447, -0.307673662900925, -0.316463977098465, -0.315838783979416, -0.278884381055832, -0.29575651884079, -0.300951153039932, -0.275152236223221, -0.292507320642471, -0.283683449029922, -0.333169460296631, -0.323910266160965, -0.317758291959763, -0.318425863981247, -0.318803340196609, -0.316879868507385, -0.321441829204559, -0.342198520898819, -0.336640536785126, -0.334549427032471, -0.304668426513672, -0.281678169965744, -0.287939876317978, -0.278673946857452, -0.299236595630646, -0.295697629451752, -0.297617554664612, -0.300621718168259, -0.294943511486053, -0.291149199008942, -0.263380706310272, -0.264828890562057, -0.260132044553757, -0.294039487838745, -0.284087061882019, -0.27564924955368, -0.275646597146988, -0.294898957014084, -0.290615618228912, -0.264040410518646, -0.267250239849091, -0.27454400062561, -0.22650308907032, -0.220207497477531, -0.233633011579514, -0.205283910036087, -0.201104089617729, -0.235431581735611, -0.252072185277939, -0.257701843976974, -0.233586445450783, -0.239779070019722, -0.233227252960205, -0.26639312505722, -0.256356805562973, -0.248022571206093, -0.285306513309479), month_10 = c(0.109010718762875, 0.112969301640987, 0.110368527472019, 0.107052445411682, 0.0947703272104263, 0.109930463135242, 0.106371931731701, 0.114236004650593, 0.102411419153214, 0.0184143912047148, 0.0301767271012068, 0.0376978516578674, 0.0472327470779419, 0.0622735135257244, 0.043902475386858, 0.0611664243042469, 0.0618763938546181, 0.0555795393884182, 0.0623081848025322, 0.0636096075177193, 0.0675770491361618, 0.0629641935229301, 0.0288578178733587, 0.025121470913291, 0.0300221722573042, 0.0611664243042469, 0.0659252777695656, 0.0159557648003101, 0.0376978516578674, 0.032174538820982, 0.0317839048802853, 0.0626140907406807, 0.0577763170003891, 0.0571180321276188, 0.0571180321276188, 0.0590739175677299, 0.0760120898485184, 0.0670360922813416, 0.0699110627174377, 0.0717969089746475, 0.0640148967504501, 0.0670360922813416, 0.164964601397514, 0.152724280953407, 0.115071900188923, 0.129654854536057, 0.122471310198307, 0.0768138542771339, 0.0400041155517101, 0.0573667995631695, 0.0685276389122009, 0.0337212830781937, 0.0616994015872478, 0.0589617975056171, 0.059433214366436, 0.0567467175424099, 0.0661386772990227, 0.0704409778118134, 0.0611028559505939, 0.0579087659716606, 0.0622764676809311, 0.0538184903562069, 0.0553129874169827, 0.058912742882967, 0.059433214366436, 0.0575473643839359, 0.0575473643839359, 0.058104183524847, 0.055678017437458, 0.0575473643839359, 0.059433214366436, 0.0603105537593365, 0.0589617975056171, 0.0601625964045525, 0.0577935017645359, 0.0574705749750137, 0.0550192892551422, 0.0475285314023495, 0.0577935017645359, 0.0543489865958691, 0.0526755712926388, 0.0553129874169827, 0.0589617975056171, 0.0593133755028248, 0.0547068528831005, 0.0676216259598732, 0.0511428378522396, 0.0564789660274982, 0.0641900449991226, 0.0666491389274597, 0.0390096306800842, 0.058005329221487, 0.0449355207383633, 0.062143836170435, 0.0591179206967354, 0.0637796893715858, 0.0633300691843033, 0.0702810436487198, 0.0647093132138252, 0.0427459664642811), month_12 = c(0.0733551606535912, 0.0707022771239281, 0.0707631036639214, 0.0718683376908302, 0.071540854871273, 0.0993443354964256, 0.0973133370280266, 0.0668289735913277, 0.0731536969542503, 0.0717423185706139, 0.0750747397542, 0.0754749700427055, 0.0746353641152382, 0.0912141725420952, 0.100288398563862, 0.07671108096838, 0.0708827450871468, 0.0708827450871468, 0.0741974636912346, 0.0761196836829185, 0.0648202076554298, 0.114817388355732, 0.100519739091396, 0.100288398563862, 0.102664910256863, 0.0566539540886879, 0.0648743882775307, 0.0706770494580269, 0.0746353641152382, 0.0750747397542, 0.0750747397542, 0.113439425826073, 0.127338454127312, 0.114467553794384, 0.112322382628918, 0.136203452944756, 0.131634846329689, 0.144591823220253, 0.139709115028381, 0.140504062175751, 0.145617410540581, 0.166768744587898, 0.151759415864944, 0.151587069034576, 0.156320676207542, 0.131974145770073, 0.135847419500351, 0.159762278199196, 0.163790658116341, 0.158604919910431, 0.127949997782707, 0.109744042158127, 0.101787634193897, 0.0582009926438332, 0.0671374276280403, 0.0669510439038277, 0.074992410838604, 0.074992410838604, 0.0787725821137428, 0.0696783438324928, 0.0965322777628899, 0.0666131302714348, 0.0666131302714348, 0.0925824269652367, 0.0875711515545845, 0.0873847678303719, 0.0873847678303719, 0.0711322501301765, 0.0696783438324928, 0.0667495802044868, 0.0667495802044868, 0.0685289725661278, 0.0582009926438332, 0.0599979534745216, 0.0702869072556496, 0.068938173353672, 0.0610831864178181, 0.074992410838604, 0.0702869072556496, 0.0683296099305153, 0.0671374276280403, 0.0666131302714348, 0.0815844461321831, 0.070037417113781, 0.068938173353672, 0.0862899348139763, 0.0824234709143639, 0.0761196836829185, 0.0663527771830559, 0.0751885995268822, 0.072908379137516, 0.0599117167294025, 0.0743952021002769, 0.0666681602597237, 0.0667495802044868, 0.0748060271143913, 0.0800538137555122, 0.0787725821137428, 0.0741974636912346, 0.0741937384009361), date = structure(c(14610, 14611, 14612, 14613, 14614, 14615, 14616, 14617, 14618, 14619, 14620, 14621, 14622, 14623, 14624, 14625, 14626, 14627, 14628, 14629, 14630, 14631, 14632, 14633, 14634, 14635, 14636, 14637, 14638, 14639, 14640, 14641, 14642, 14643, 14644, 14645, 14646, 14647, 14648, 14649, 14650, 14651, 14652, 14653, 14654, 14655, 14656, 14657, 14658, 14659, 14660, 14661, 14662, 14663, 14664, 14665, 14666, 14667, 14668, 14669, 14670, 14671, 14672, 14673, 14674, 14675, 14676, 14677, 14678, 14679, 14680, 14681, 14682, 14683, 14684, 14685, 14686, 14687, 14688, 14689, 14690, 14691, 14692, 14693, 14694, 14695, 14696, 14697, 14698, 14699, 14700, 14701, 14702, 14703, 14704, 14705, 14706, 14707, 14708, 14709), class = "Date")), row.names = c(NA, 100L), class = "data.frame")
Adding a plane at the value of 0 for z: library(plotly) library(dplyr) # I separated your df wrangling to better understand the data df <- df %>% select(date, everything()) # Code you posted df %>% plot_ly( x = colnames(subset(., select = c(2:4))), # probably not the most efficient method y = ~date, z = data.matrix(subset(., select = c(2:4))), type = "surface", colors = c("darkblue", "yellow", "darkred") ) %>% # The surface added by using y & x from df # and making z a matrix of zeros 3 rows by 100 columns. add_surface(z = matrix(0, ncol = 3, nrow = 100), y = df$date, x = colnames(subset(df, select = c(2:4))), opacity = .8) Your plot: With added zero z plane:
maintain date class for x-axis with geom_smooth
I am trying to smooth three lines and shade the area between the min and max lines using this approach. My x-axis is a date class at the start, but I'm getting an error in the last step. Invalid input: date_trans works with objects of class Date only I'm losing the date class when smoothing into p1. How can I prevent this or otherwise pass a date to geom_ribbon in the final step? library(tidyverse) mmr <- structure(list(year = structure(c(3834, 3926, 3987, 4108, 4169, 4230, 4352, 4442, 4503, 4595, 4687, 4748, 4929, 4991, 5052, 5204, 5265, 5326, 5448, 5538, 5599, 5752, 5813, 5844, 6025, 6087, 6148, 6299, 6360, 6574, 6634, 6695, 6848, 6909, 7091, 7183, 7213, 7395, 7456, 7517, 7639, 7729, 7790, 7943, 8035, 8187, 8248, 8309, 8460, 8552, 8613, 8735, 8825, 8886, 9009, 9100, 9131, 9282, 9343, 9404, 9556, 9617, 9678, 9831, 9862, 9952, 10105, 10135, 10196, 10347, 10408, 10470, 10592, 10682, 10896, 10957, 11078, 11170, 11231, 11382, 11474, 11596, 11657, 11688, 11869, 11931, 11961, 12112, 12173, 12234, 12357, 12418, 12478, 12600, 12692, 12874, 12935, 12996, 13118, 13208, 13239, 13392, 13483, 13665, 13757, 13787, 13939), class = "Date"), mmrU = c(13.8231596, 13.7529708, 13.7148858, 13.6118164, 13.5638772, 13.5478975, 13.519933, 13.5012899, 13.474657, 13.4014166, 13.4173963, 13.3787786, 13.2882268, 13.2589306, 13.2354049, 13.1763686, 13.1470724, 13.1255442, 13.0618471, 13.032551, 13.0108008, 13.0139079, 13.0232294, 13.0352143, 13.0498623, 13.0498623, 13.0376556, 13.0232294, 13.0205662, 12.987275, 12.9739586, 12.9033814, 12.9792852, 12.99127, 13.0059181, 13.0059181, 13.0059181, 13.0205662, 13.0130202, 13.0034767, 12.9606421, 12.9406675, 12.9277949, 12.8727536, 12.8203756, 12.7728802, 12.7582321, 12.7569005, 12.7422524, 12.7382575, 12.7171414, 12.6397157, 12.601098, 12.5737993, 12.6583588, 12.7382575, 12.7979151, 12.9473257, 12.9965966, 13.0327729, 13.1191079, 13.1617205, 13.2061087, 13.332171, 13.39609, 13.4586773, 13.6477709, 13.7676189, 13.8408594, 14.0619124, 14.1604541, 14.2246394, 14.3815071, 14.4851535, 14.7996436, 14.9234866, 15.0436305, 15.3022444, 15.4277361, 15.7198102, 15.9745982, 16.2684478, 16.4362351, 16.5409911, 16.8277386, 16.9728879, 17.0634398, 17.3204472, 17.4616016, 17.5578129, 17.819814, 17.9623, 18.0631721, 18.3045327, 18.4784455, 18.6694034, 18.7612869, 18.8153517, 18.9250792, 18.9770134, 19.0189602, 19.0675652, 19.07955, 19.1008563, 19.0582437, 19.0292138, 18.9410589), mmr = c(12.1431454, 12.11401197, 12.05086241, 12.02612341, 11.96590348, 11.93823486, 11.8828976, 11.8601117, 11.8275604, 11.8047745, 11.7784079, 11.7461821, 11.6955796, 11.7256748, 11.6689467, 11.6396505, 11.5997012, 11.6033632, 11.5384455, 11.5144759, 11.4532202, 11.4532202, 11.5411088, 11.5198025, 11.4971645, 11.5118126, 11.5118126, 11.5345985, 11.5586865, 11.4532202, 11.4532202, 11.4692, 11.5144759, 11.5201829, 11.5494791, 11.525129, 11.5662198, 11.570405, 11.4665367, 11.4984962, 11.4345772, 11.4692, 11.4092759, 11.3893013, 11.3653317, 11.3799798, 11.4406647, 11.3014127, 11.2721165, 11.2833024, 11.2720179, 11.2161875, 11.1675824, 11.1749065, 11.1928097, 11.2546572, 11.3295252, 11.397883, 11.568574, 11.5020472, 11.5437721, 11.5792826, 11.6846601, 11.7494372, 11.8112847, 11.8868038, 12.0684401, 12.1749717, 12.2844995, 12.3747184, 12.4756571, 12.5664753, 12.7422524, 12.8274777, 13.114314, 13.2216445, 13.3164576, 13.575862, 13.7156847, 13.8555074, 14.1513989, 14.2922869, 14.4147983, 14.5559526, 14.8196183, 14.9421296, 15.0779574, 15.203132, 15.3283066, 15.4521496, 15.686519, 15.7970456, 15.9248835, 16.0292844, 16.21678, 16.3945102, 16.4693782, 16.5442463, 16.6028386, 16.661431, 16.7265336, 16.7734075, 16.8304019, 16.8407888, 16.781464, 16.7411817, 16.6532932), mmrL = c(10.68619282, 10.63692196, 10.5963068, 10.49976255, 10.45848156, 10.44250182, 10.4092107, 10.39189932, 10.38390945, 10.31732721, 10.28270445, 10.25695931, 10.20813234, 10.21612221, 10.22278043, 10.25207662, 10.26139813, 10.24719392, 10.21079563, 10.19481589, 10.17883615, 10.17883615, 10.17883615, 10.17883615, 10.1921526, 10.19481589, 10.20813234, 10.20946398, 10.22011714, 10.19481589, 10.18283109, 10.17883615, 10.17883615, 10.17883615, 10.17883615, 10.17484122, 10.16418806, 10.14554503, 10.13356023, 10.12024378, 10.09893747, 10.08828431, 10.0762995, 10.06165141, 10.04700332, 10.02436536, 9.999064105, 9.9810869, 9.936476799, 9.911175548, 9.890756995, 9.840598374, 9.815297123, 9.79798574, 9.880547718, 9.947129958, 9.997199802, 10.1149172, 10.14021845, 10.14953997, 10.18948931, 10.21345892, 10.24963527, 10.36659807, 10.42519044, 10.47179801, 10.63958525, 10.74478519, 10.82335224, 10.99261909, 11.11726527, 11.15293432, 11.27611147, 11.33869877, 11.52113411, 11.58638471, 11.6377862, 11.82075419, 11.96190854, 12.31346277, 12.45195383, 12.52985505, 12.74491568, 12.85810549, 12.93633962, 13.16971038, 13.29887992, 13.38676848, 13.61581138, 13.71834803, 13.78226698, 13.95005423, 14.03927443, 14.10159541, 14.27497556, 14.42971269, 14.57725893, 14.60788676, 14.63185637, 14.69843861, 14.72373986, 14.73439302, 14.73439302, 14.73439302, 14.73683436, 14.674469, 14.60744288 )), row.names = c(NA, -107L), class = "data.frame") # smooth lines p1 <- mmr %>% ggplot(.) + geom_smooth(aes(x=year, y=mmrL)) + geom_smooth(aes(x=year, y=mmr)) + geom_smooth(aes(x=year, y=mmrU)) + ylim(0, 25) + scale_x_date(date_breaks="2 years", date_labels = "%Y") # build plot object for rendering pp1 <- ggplot_build(p1) # extract data from the upper and lower lines df2 <- data.frame(x = pp1$data[[1]]$x, ymin = pp1$data[[2]]$y, ymax = pp1$data[[3]]$y) # use the lm data to add the ribbon to the plot p1 + geom_ribbon(data = df2, aes(x = x, ymin = ymin, ymax = ymax), fill = "grey", alpha = 0.4)
The issue is that pp1$data[[1]]$x is no longer of date format. We may instead use df2 <- data.frame(x = as.Date(pp1$data[[1]]$x, origin = "1970-01-01"), ymin = pp1$data[[1]]$y, ymax = pp1$data[[3]]$y) where I've also adjusted ymin. This gives