我有下面的R代码和示例数据,它们在gridExtra
包的帮助下共同产生一个组合图。
现在的问题是,只要x
两个图表(f.plot
和sp.plot
}之间共享f.plot
美学,datadate
不会裁剪/删除缺失的值。
导致这种行为的原因是什么?我希望x轴上的datadate
在两个图中以相同的观察开始,因此可以一起阅读图形。
下面是图表;如您所见,顶部f.plot
datadate
将大部分留空。我希望删除此部分,以便两个图表显示# My code
f.plot = ggplot(f.DT, aes(x = datadate)) +
geom_point(aes(y = RSI_QEBIT,
colour = 'firm',
group = 1),
size = 1
) +
geom_point(aes(y = RSI_IWA.QEBIT,
colour = 'industry',
group = 2),
size = 1,
shape = 2
) +
scale_y_continuous(limits = c(10, 90),
breaks = c(10, 30, 50, 70, 90)) +
scale_colour_discrete(name = '') +
ylab('RSI') +
theme(legend.title.align = 0.5,
legend.position = 'top')
sp.plot = ggplot(f.DT, aes(x = datadate, y = Close)) +
geom_line()
gridExtra::grid.arrange(f.plot, sp.plot, nrow = 2)
# Sample Data Below
> dput(f_sp.merged[[11]][, .(datadate, RSI_QEBIT, RSI_IWA.QEBIT, Close)])
structure(list(datadate = structure(c(10987, 11016, 11047, 11077,
11108, 11138, 11169, 11200, 11230, 11261, 11291, 11322, 11353,
11381, 11412, 11442, 11473, 11503, 11534, 11565, 11595, 11626,
11656, 11687, 11687, 11718, 11746, 11777, 11807, 11807, 11838,
11838, 11868, 11868, 11899, 11899, 11930, 11930, 11960, 11960,
11991, 11991, 12021, 12021, 12052, 12052, 12083, 12083, 12111,
12111, 12142, 12142, 12172, 12172, 12203, 12203, 12233, 12233,
12264, 12264, 12295, 12295, 12325, 12325, 12356, 12356, 12386,
12386, 12417, 12417, 12448, 12448, 12477, 12477, 12508, 12508,
12538, 12538, 12569, 12569, 12599, 12599, 12630, 12630, 12630,
12661, 12661, 12661, 12691, 12691, 12691, 12722, 12722, 12722,
12752, 12752, 12752, 12783, 12783, 12783, 12814, 12814, 12814,
12842, 12842, 12842, 12873, 12873, 12873, 12903, 12903, 12903,
12934, 12934, 12934, 12964, 12964, 12964, 12995, 12995, 12995,
13026, 13026, 13026, 13056, 13056, 13056, 13087, 13087, 13087,
13117, 13117, 13117, 13148, 13148, 13148, 13179, 13179, 13179,
13207, 13207, 13207, 13238, 13238, 13238, 13268, 13268, 13268,
13299, 13299, 13299, 13329, 13329, 13329, 13360, 13360, 13360,
13360, 13391, 13391, 13391, 13391, 13421, 13421, 13452, 13452,
13482, 13482, 13513, 13513, 13544, 13544, 13572, 13572, 13603,
13603, 13633, 13633, 13664, 13664, 13694, 13694, 13725, 13725,
13756, 13756, 13786, 13786, 13817, 13817, 13847, 13847, 13878,
13878, 13909, 13909, 13938, 13938, 13969, 13969, 13999, 13999,
14030, 14030, 14060, 14060, 14091, 14091, 14122, 14122, 14152,
14152, 14183, 14183, 14213, 14213, 14244, 14244, 14275, 14275,
14303, 14303, 14334, 14334, 14364, 14364, 14395, 14395, 14425,
14425, 14456, 14456, 14487, 14487, 14517, 14517, 14548, 14548,
14578, 14578, 14609, 14609, 14640, 14640, 14668, 14668, 14699,
14699, 14729, 14729, 14760, 14760, 14790, 14790, 14821, 14821,
14852, 14852, 14882, 14882, 14913, 14913, 14943, 14943, 14974,
14974, 15005, 15005, 15033, 15033, 15064, 15064, 15094, 15094,
15125, 15125, 15155, 15155, 15186, 15186, 15217, 15217, 15247,
15247, 15278, 15278, 15308, 15308, 15339, 15339, 15370, 15370,
15399, 15399, 15430, 15430, 15460, 15460, 15491, 15491, 15521,
15521, 15552, 15552, 15583, 15583, 15613, 15613, 15644, 15644,
15674, 15674, 15705, 15705, 15736, 15736, 15764, 15764, 15795,
15795, 15825, 15825, 15856, 15856, 15886, 15886, 15917, 15917,
15948, 15948, 15978, 15978, 16009, 16009, 16039, 16039, 16070,
16070, 16101, 16101, 16129, 16129, 16160, 16160, 16190, 16190,
16221, 16221, 16251, 16251, 16282, 16282, 16313, 16313, 16343,
16343, 16374, 16374, 16404, 16404, 16435, 16435, 16466, 16466,
16494, 16494, 16525, 16525, 16555, 16555, 16586, 16586, 16616,
16616, 16647, 16647, 16678, 16678, 16708, 16708, 16739, 16739,
16769, 16769, 16800, 16800, 16831, 16831, 16860, 16860, 16891,
16891, 16921, 16921, 16952, 16952, 16982, 16982, 17013, 17013,
17044, 17044, 17074, 17074, 17105, 17105, 17135, 17135, 17166,
17166, 17197, 17197, 17225, 17225, 17256, 17256, 17286, 17286,
17317, 17317, 17347, 17347, 17378, 17378, 17409, 17409, 17439,
17439, 17470, 17470, 17500, 17500, 17531, 17531, 17562, 17562,
17590, 17590), class = "Date"), RSI_QEBIT = c(NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 52.3888128361573,
NA, NA, NA, NA, NA, 46.7695032956008, NA, NA, NA, NA, NA, 48.3242029313682,
NA, NA, NA, NA, NA, 48.6760048140719, NA, NA, NA, NA, NA, 49.5228471731341,
NA, NA, NA, NA, NA, 48.3099155797549, NA, NA, NA, NA, NA, 32.7830014371969,
NA, NA, NA, NA, NA, 13.7881826457913, NA, NA, NA, NA, NA, 64.3051398012505,
NA, NA, NA, NA, NA, 70.4927002892185, NA, NA, NA, NA, NA, 74.92845166462,
NA, NA, NA, NA, NA, 73.1828632792545, NA, NA, NA, NA, NA, 75.6616692267323,
NA, NA, NA, NA, NA, 75.7182011312054, NA, NA, NA, NA, NA, 69.8912222101431,
NA, NA, NA, NA, NA, 69.1474868532028, NA, NA, NA, NA, NA, 61.4827992057471,
NA, NA, NA, NA, NA, 67.6521251778095, NA, NA, NA, NA, NA, 60.5492221380302,
NA, NA, NA, NA, NA, 58.4405381225396, NA, NA, NA, NA, NA, 76.3950492398421,
NA, NA, NA, NA, NA, 49.7475372075983, NA, NA, NA, NA, NA, 46.7189968702056,
NA, NA, NA, NA, NA, 47.4088861622833, NA, NA, NA, NA, NA, 52.5814996148601,
NA, NA, NA, NA, NA, 48.1460913851499, NA, NA, NA, NA, NA, 43.9759786802541,
NA, NA, NA, NA, NA, 43.2804111702118, NA, NA, NA, NA, NA, 45.6344287258802,
NA, NA, NA, NA, NA, 48.2833363940706, NA, NA, NA, NA, NA, 49.9207253278975,
NA, NA, NA, NA, NA, 54.1240920348388, NA, NA, NA, NA, NA, 53.0492784801673,
NA, NA, NA, NA, NA, 48.4710534396005, NA, NA, NA, NA, NA, 52.4467450694257,
NA, NA, NA, NA, NA, 49.02932538503, NA, NA, NA, NA, NA, 52.1689803368667,
NA, NA, NA, NA, NA, 48.6667452399263, NA, NA, NA, NA, NA, 61.3320184022272,
NA, NA, NA, NA, NA, 88.248988350591, NA, NA, NA, NA, NA, 61.0085510285969,
NA, NA, NA, NA, NA, 60.7285416584857, NA, NA, NA, NA, NA, 53.8003200216599,
NA, NA, NA, NA, NA, 50.3532484143042, NA, NA, NA, NA, NA, 50.7791676198036,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), RSI_IWA.QEBIT = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 43.2784066785198, NA, NA, NA, NA, NA, 41.4609583315373,
NA, NA, NA, NA, NA, 42.3458469721745, NA, NA, NA, NA, NA, 41.3120415140818,
NA, NA, NA, NA, NA, 40.8593227567209, NA, NA, NA, NA, NA, 41.7086142937269,
NA, NA, NA, NA, NA, 44.7672762897319, NA, NA, NA, NA, NA, 44.4046311081346,
NA, NA, NA, NA, NA, 48.644862711434, NA, NA, NA, NA, NA, 28.7570184940017,
NA, NA, NA, NA, NA, 32.8349550227414, NA, NA, NA, NA, NA, 38.6602849808124,
NA, NA, NA, NA, NA, 37.2234181835582, NA, NA, NA, NA, NA, 37.7007308226884,
NA, NA, NA, NA, NA, 38.4392137669943, NA, NA, NA, NA, NA, 40.929862390392,
NA, NA, NA, NA, NA, 47.06249604465, NA, NA, NA, NA, NA, 48.1896019911677,
NA, NA, NA, NA, NA, 45.7190353308931, NA, NA, NA, NA, NA, 50.5280841142112,
NA, NA, NA, NA, NA, 50.9993266477174, NA, NA, NA, NA, NA, 53.213981475346,
NA, NA, NA, NA, NA, 69.7758391275923, NA, NA, NA, NA, NA, 69.1848216836319,
NA, NA, NA, NA, NA, 66.611224217245, NA, NA, NA, NA, NA, 65.3976647827331,
NA, NA, NA, NA, NA, 63.7159220121554, NA, NA, NA, NA, NA, 54.9970674178704,
NA, NA, NA, NA, NA, 58.9788968609735, NA, NA, NA, NA, NA, 57.9755954312683,
NA, NA, NA, NA, NA, 61.2711845632017, NA, NA, NA, NA, NA, 54.3309207552518,
NA, NA, NA, NA, NA, 51.8480532753422, NA, NA, NA, NA, NA, 39.7469823445025,
NA, NA, NA, NA, NA, 38.2532424695314, NA, NA, NA, NA, NA, 44.9509307995001,
NA, NA, NA, NA, NA, 33.7782129487646, NA, NA, NA, NA, NA, 27.7515721920479,
NA, NA, NA, NA, NA, 51.7879898400319, NA, NA, NA, NA, NA, 51.645629213428,
NA, NA, NA, NA, NA, 47.1117233066807, NA, NA, NA, NA, NA, 41.5186301720134,
NA, NA, NA, NA, NA, 38.1568028154303, NA, NA, NA, NA, NA, 42.5774975721961,
NA, NA, NA, NA, NA, 43.3358004997924, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), Close = c(16.1, 16.15, 16.9, 17.4, 18.7, 19.1,
19.1, 19.85, 19.2, 19.7, 18.8, 19.2, 19.94, 20.1, 19.85, 21.65,
20.34, 18.1, 18.87, 17.05, 15, 16.3, 14.01, NA, 14.22, 13.91,
14.6, 16.13, 10.5, 16.4, 10.11, 15.3, 9.93, 15.05, 9.15, 14.55,
9.73, 15.14, 9.37, 14.9, 8.9, 13.9, 8.91, 13.95, 9.27, 14.73,
9.86, 14.96, 10.47, 15.5, 9.71, 14.25, 10.13, 14.47, 11.43, 15.6,
12.83, 17.25, 11.69, 16.4, 12.61, 17.49, 12.81, 17.25, 13.12,
17.33, 14.35, 18.67, 16.4, 21.25, 14.03, 18.56, 14.22, 19, 13.7,
17.98, 13.18, 18.1, 13.13, 17.81, 13.98, 18.67, 14.95, 19.91,
18.85, 15.7, 20.5, 18.2, 17.93, 22.67, 18.87, 17.44, 21.23, 18.89,
20.37, 24.12, 19.05, 20.82, 24.93, 18.5, 20.75, 25.76, 18.95,
21.56, 26.75, 18.88, 20, 24.03, 17.05, 20.17, 25.3, 17.24, 20.61,
25.89, 17.5, 22.25, 27.2, 18.4, 23.4, 28.55, 18.86, 23.93, 28.39,
19.45, 25.42, 29.5, 20.58, 21.75, 25.58, 18.5, 22.84, 26.65,
21.35, 23.53, 27.41, 22.65, 24.96, 28.57, 23.2, 23.65, 26.76,
21.99, 23.1, 26.88, 23.32, 24.45, 27.22, 23.87, 23.76, 26.07,
24.8, 24.09, 26.7, 26.05, 22.32, 25.3, 25.3, 22.38, NA, NA, 25.56,
23.19, 21.94, 19.62, 21.9, 19.6, 19.39, 17.04, 19.94, 17.21,
19.85, 16.94, 19.98, 17.24, 19.45, 16.87, 19.11, 17.27, 19.92,
18.73, 20.27, 19.09, 19.51, 18.3, 18.04, 17.06, 18.64, 18.84,
18, 18.929, 18.11, 18.1, 17.62, 17.77, 17.67, 17.48, 18.35, 18.65,
19.67, 19.1, 19.75, 19.71, 20.34, 20.53, 20.5, 20.11, 17.7, 17.31,
18.85, 17.75, 15.99, 14.94, 13.01, 11.21, 11.24, 8.97, 9.35,
7.62, 10.15, 8.31, 7.26, 5.64, 7.1, 5.58, 7.75, 6.57, 9.5, 8.76,
9.18, 7.9, 8.78, 8.23, 9.4, 8.62, 11.33, 10.51, 10.26, 9.2, 10.13,
9.61, 10.15, 9.63, 10.75, 10.1, 11, 10.45, 11.75, 11.66, 11.65,
11.52, 10.09, 9.48, 9.73, 9.16, 9.93, 9.63, 10.33, 9.7, 11.34,
11.06, 12.24, 11.99, 13.28, 12.89, 12.78, 12.86, 12.62, 12.69,
12.5, 12.89, 13.41, 13.83, 13.36, 14.14, 12.78, 13.19, 12.15,
12.58, 12.4, 12.95, 11.31, 11.55, 9.47, 8.99, 10.4, 10.45, 10.5,
10.28, 10.76, 10.53, 10.05, 10.02, 10.09, 10.22, 9.35, 9.4, 8.88,
8.97, 7.36, 7.09, 6.46, 6.37, 6.39, 6.37, 6.66, 6.77, 6.63, 6.74,
5.99, 5.99, 5.17, 5.15, 4.95, 4.97, 4.55, 4.57, 4.39, 4.13, 5.18,
5.1, 5.14, 5.06, 5.29, 5.08, 5.17, 4.93, 5.92, 5.78, 5.82, 5.5,
6.06, 5.92, 6.7, 6.45, 6.56, 6.21, 6.57, 6.2, 7.2, 6.46, 7.46,
6.74, 6.69, 6.08, 7.12, 6.5, 6.85, 6.33, 7.65, 7.18, 6.95, 6.4,
6.93, 6.39, 5.87, 5.2, 4.55, 4.03, 3.72, 3.36, 3.66, 3.11, 3.4,
2.68, 4.14, 3.32, 3.8, 2.99, 4.05, 3.36, 3.33, 2.67, 3.12, 2.49,
2.09, 1.6, 1.93, 1.46, 1.14, 0.86, 1.28, 0.993, 1.14, 0.8626,
1.02, 0.7332, 1.17, 0.83, 1.01, 0.7484, 1.7, 1.31, 2.06, 1.64,
2.27, 1.75, 2.36, 1.84, 1.95, 1.5, 1.89, 1.44, 2.07, 1.59, 2.07,
1.56, 2.04, 1.52, 1.93, 1.42, 1.63, 1.25, 1.56, 1.17, 1.33, 1,
1.31, 0.985, 1.1, 0.8217, 1.02, 0.7904, 0.94, 0.72, 0.78, 0.6078,
1.27, 1.03, 1.45, 1.08, 1.1, 0.8485, 1, 0.7946, 0.97, 0.77, 0.93,
0.73)), .Names = c("datadate", "RSI_QEBIT", "RSI_IWA.QEBIT",
"Close"), row.names = c(NA, -438L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x10200f378>)
的相同范围,然后使图表具有可比性。
谢谢!
Valor total = SUM('Códigos e valores'[Valor unitário])*SUM(Csv[Quant.])
答案 0 :(得分:2)
datadate
的范围基本上来自&#34; 2000-01-31&#34;到&#34; 2018-02-28&#34;
range(f.DT$datadate)
返回&#34; 2000-01-31&#34;到&#34; 2018-02-28&#34;
这是第一个图的x轴假设该范围的原因。你可以使用xlim
甚至更好地改变它,实际上只使用没有缺失值的数据(因为那是你想要的)。
更改传递给data
的值,以便他们只考虑完整的案例:
ggplot(f.DT[complete.cases(f.DT),], aes(x = datadate)) +
geom_point(aes(y = RSI_QEBIT,
colour = 'firm',
group = 1),
size = 1
)
将为您解决问题。