R语言 scale_x_date绘制数据中不存在的x轴值

aor9mmx1  于 2023-06-27  发布在  其他
关注(0)|答案(1)|浏览(128)

已经打了好几个小时了这是我的数据:

> dput(df)
structure(list(Date = structure(c(19967, 19973, 20332, 20338, 
20697, 20703, 21062, 21068, 21428, 21434, 21793, 21799, 22158, 
22164, 22523, 22529, 22889, 22895, 23254, 23260, 23619, 23625, 
23984, 23990, 24350, 24356, 24715, 24721, 25080, 19967, 19973, 
20332, 20338, 20697, 20703, 21062, 21068, 21428, 21434, 21793, 
21799, 19731, 19913, 20097, 20278, 20462, 20643, 20827, 21008, 
21192, 21374, 21558, 21739, 21923, 22104, 19967, 19973, 20332, 
20338, 20697, 20703, 21062, 21068, 21428, 21434, 21793, 21799, 
22158, 22164, 22523, 22529, 22889, 22895, 23254, 23260, 23619, 
23625, 23984, 23990, 19731, 19913, 20097, 20278, 20462, 20643, 
20827, 21008, 21192, 21374, 21558, 21739, 19731, 19913, 20097, 
20278, 20462, 20643, 20827, 21008, 21192, 21374, 21558, 21739, 
21923, 22104, 19731, 19913, 20097, 20278, 20462, 20643, 20827, 
21008, 21192, 21374, 21558, 21739, 21923, 22104, 22288, 22469, 
22653, 22835, 23019, 23200, 23384, 23565, 23749, 23930, 19731, 
19913, 20097, 20278, 20462, 20643, 20827, 21008, 21192, 21374, 
21558, 21739, 21923, 22104, 22288, 22469, 22653, 22835, 23019, 
23200, 23384, 23565, 23749, 23930, 24114, 24296, 24480, 24661, 
24845, 19731, 19913, 20097, 20278, 20462, 20643, 20827, 21008, 
21192, 21374, 21558, 21739, 21923, 22104, 22288, 22469, 22653, 
22835, 23019, 23200, 23384, 23565, 23749, 23930, 24114, 24296, 
24480, 24661, 24845, 25026, 25210, 25391, 25575, 25757, 25941, 
26122, 19731, 19913, 20097, 20278, 20462, 20643, 20827, 21008, 
21192, 21374, 21558, 21739, 21923, 22104, 22288, 22469, 22653, 
22835, 23019, 23200, 23384, 23565, 23749, 23930, 24114, 24296, 
24480, 24661, 24845, 25026, 25210, 25391, 25575, 25757, 25941, 
26122, 26306, 26487, 26671, 26852, 27036, 27218, 27402, 27583, 
27767, 27948), class = "Date"), Rate = c(0.0425, 0.0425, 0.05, 
0.05, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.01, 0.01, 0.03, 0.03, 0.03, 0.03, 0.03, 
0.03, 0.03, 0.03, 0.03, 0.03, 0.01, 0.01, 0.01, 0.01, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.03625, 0.03625, 
0.04125, 0.04125, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 
0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 
0.12, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 
0.0075, 0.0175, 0.0175, 0.0175, 0.0175, 0.0175, 0.0175, 0.03625, 
0.03625, 0.04125, 0.04125, 0.04125, 0.04125, 0.04125, 0.04125, 
0.0475, 0.0475, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.0425, 0.0425, 0.05, 0.05, 
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 
0.05, 0.05, 0.05, 0.035, 0.035, 0.035, 0.035, 0.035, 0.035, 0.035, 
0.035, 0.035, 0.035, 0.035, 0.035, 0.04875, 0.04875, 0.04875, 
0.04875, 0.04875, 0.04875, 0.04875, 0.04875, 0.04875, 0.04875, 
0.04875, 0.04875, 0.04875, 0.04875, 0.04875, 0.04875, 0.04875, 
0.04875, 0.04875, 0.04875, 0.04875, 0.04875, 0.04875, 0.04875, 
0.03625, 0.03625, 0.04125, 0.04125, 0.04125, 0.04125, 0.04125, 
0.04125, 0.04375, 0.04375, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05), Amort = c(0, 
0, 0, 0, 0, 0, 0, 4.545455, 4.545455, 4.545455, 4.545455, 4.545455, 
4.545455, 4.545455, 4.545455, 4.545455, 4.545455, 4.545455, 4.545455, 
4.545455, 4.545455, 4.545455, 4.545455, 4.545455, 4.545455, 4.545455, 
4.545455, 4.545455, 4.545455, 0, 0, 10, 10, 10, 10, 10, 10, 10, 
10, 10, 10, 0, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 10, 10, 
10, 10, 10, 0, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 
4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 
0, 0, 0, 0, 0, 0, 4.55, 4.55, 4.55, 4.55, 4.55, 4.55, 4.55, 4.55, 
4.55, 4.55, 4.55, 4.55, 4.55, 4.55, 4.55, 4.55, 4.55, 4.55, 4.55, 
4.55, 4.55, 4.55, 0, 0, 0, 0, 0, 0, 0, 0, 3.57142857142857, 3.57142857142857, 
3.57142857142857, 3.57142857142857, 3.57142857142857, 3.57142857142857, 
3.57142857142857, 3.57142857142857, 3.57142857142857, 3.57142857142857, 
3.57142857142857, 3.57142857142857, 3.57142857142857, 3.57142857142857, 
3.57142857142857, 3.57142857142857, 3.57142857142857, 3.57142857142857, 
3.57142857142857, 3.57142857142857, 3.57142857142857, 3.57142857142857, 
3.57142857142857, 3.57142857142857, 3.57142857142857, 3.57142857142857, 
3.57142857142857, 3.57142857142857, 0, 0, 2.2727, 2.2727, 2.2727, 
2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 
2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 
2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 
2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 
2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 2.2727, 
2.2727), Residual = c(100, 100, 100, 100, 100, 100, 100, 95.45, 
90.91, 86.36, 81.82, 77.27, 72.73, 68.18, 63.64, 59.09, 54.55, 
50, 45.45, 40.91, 36.36, 31.82, 27.27, 22.73, 18.18, 13.64, 9.09, 
4.55, 0, 100, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0, 100, 
96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0, 100, 100, 100, 
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 90, 80, 
70, 60, 50, 40, 30, 20, 10, 0, 100, 100, 90, 80, 70, 60, 50, 
40, 30, 20, 10, 0, 100, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 
16, 8, 0, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 
100, 100, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0, 100, 100, 
100, 100, 100, 100, 100, 95.45, 90.91, 86.36, 81.82, 77.27, 72.73, 
68.18, 63.64, 59.09, 54.55, 50, 45.45, 40.91, 36.36, 31.82, 27.27, 
22.73, 18.18, 13.64, 9.09, 4.55, 0, 100, 100, 100, 100, 100, 
100, 100, 100, 96.428571, 92.857143, 89.285714, 85.714286, 82.142857, 
78.571429, 75, 71.428571, 67.857143, 64.285714, 60.714286, 57.142857, 
53.571429, 50, 46.428571, 42.857143, 39.285714, 35.714286, 32.142857, 
28.571429, 25, 21.428571, 17.857143, 14.285714, 10.714286, 7.142857, 
3.5714285714, 0, 100, 100, 97.72727, 95.45455, 93.18182, 90.90909, 
88.63636, 86.36364, 84.09091, 81.81818, 79.54545, 77.27273, 75, 
72.72727, 70.45455, 68.18182, 65.90909, 63.63636, 61.36364, 59.09091, 
56.81818, 54.54545, 52.27273, 50, 47.72727, 45.45455, 43.18182, 
40.90909, 38.63636, 36.36364, 34.09091, 31.81818, 29.54545, 27.27273, 
25, 22.72727, 20.45455, 18.18182, 15.90909, 13.63636, 11.36364, 
9.09091, 6.81818, 4.54545, 2.27273, 0), Amount = c(2.13, 2.13, 
2.5, 2.5, 7.5, 7.5, 7.5, 12.05, 11.7, 11.36, 11.02, 10.68, 10.34, 
10, 9.66, 9.32, 8.98, 8.64, 8.3, 7.95, 7.61, 7.27, 6.93, 6.59, 
6.25, 5.91, 5.57, 5.23, 4.89, 0.5, 0.5, 11.5, 11.35, 11.2, 11.05, 
10.9, 10.75, 10.6, 10.45, 10.3, 10.15, 0.38, 4.38, 8.36, 8.33, 
11.6, 11.24, 10.88, 10.52, 10.16, 9.8, 9.44, 9.08, 8.72, 8.36, 
1.81, 1.81, 2.06, 2.06, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 16, 15.4, 
14.8, 14.2, 13.6, 13, 12.4, 11.8, 11.2, 10.6, 0.5, 0.5, 10.5, 
10.45, 10.4, 10.35, 10.3, 10.25, 10.2, 10.15, 10.1, 10.05, 0.38, 
4.38, 8.36, 8.33, 8.3, 8.27, 8.24, 8.21, 8.42, 8.35, 8.28, 8.21, 
8.14, 8.07, 1.81, 1.81, 2.06, 2.06, 2.06, 2.06, 2.06, 2.06, 2.38, 
2.38, 2.5, 2.5, 2.5, 2.5, 12.5, 12.25, 12, 11.75, 11.5, 11.25, 
11, 10.75, 10.5, 10.25, 2.13, 2.13, 2.5, 2.5, 2.5, 2.5, 2.5, 
7.05, 6.93, 6.82, 6.7, 6.59, 6.48, 6.36, 6.25, 6.14, 6.02, 5.91, 
5.8, 5.68, 5.57, 5.45, 5.34, 5.23, 5.11, 5, 4.89, 4.77, 4.66, 
1.75, 1.75, 1.75, 1.75, 1.75, 1.75, 1.75, 1.75, 5.3214, 5.2589, 
5.1964, 5.1339, 5.6607, 5.5736, 5.4866, 5.3995, 5.3125, 5.2254, 
5.1384, 5.0513, 4.9643, 4.8772, 4.7902, 4.7031, 4.616, 4.529, 
4.4419, 4.3549, 4.2678, 4.1808, 4.0937, 4.0067, 3.9196, 3.8326, 
3.7455, 3.6585, 1.8125, 1.8125, 4.33523, 4.28835, 4.24148, 4.1946, 
4.14773, 4.10085, 4.16193, 4.11222, 4.31818, 4.26136, 4.20455, 
4.14773, 4.09091, 4.03409, 3.97727, 3.92045, 3.86364, 3.80682, 
3.75, 3.69318, 3.63636, 3.57955, 3.52273, 3.46591, 3.40909, 3.35227, 
3.29545, 3.23864, 3.18182, 3.125, 3.06818, 3.01136, 2.95455, 
2.89773, 2.84091, 2.78409, 2.72727, 2.67045, 2.61364, 2.55682, 
2.5, 2.44318, 2.38636, 2.32955), ticker = c("AE38R", "AE38R", 
"AE38R", "AE38R", "AE38R", "AE38R", "AE38R", "AE38R", "AE38R", 
"AE38R", "AE38R", "AE38R", "AE38R", "AE38R", "AE38R", "AE38R", 
"AE38R", "AE38R", "AE38R", "AE38R", "AE38R", "AE38R", "AE38R", 
"AE38R", "AE38R", "AE38R", "AE38R", "AE38R", "AE38R", "AL29R", 
"AL29R", "AL29R", "AL29R", "AL29R", "AL29R", "AL29R", "AL29R", 
"AL29R", "AL29R", "AL29R", "AL29R", "AL30R", "AL30R", "AL30R", 
"AL30R", "AL30R", "AL30R", "AL30R", "AL30R", "AL30R", "AL30R", 
"AL30R", "AL30R", "AL30R", "AL30R", "AL35R", "AL35R", "AL35R", 
"AL35R", "AL35R", "AL35R", "AL35R", "AL35R", "AL35R", "AL35R", 
"AL35R", "AL35R", "AL35R", "AL35R", "AL35R", "AL35R", "AL35R", 
"AL35R", "AL35R", "AL35R", "AL35R", "AL35R", "AL35R", "AL35R", 
"GD29", "GD29", "GD29", "GD29", "GD29", "GD29", "GD29", "GD29", 
"GD29", "GD29", "GD29", "GD29", "GD30", "GD30", "GD30", "GD30", 
"GD30", "GD30", "GD30", "GD30", "GD30", "GD30", "GD30", "GD30", 
"GD30", "GD30", "GD35", "GD35", "GD35", "GD35", "GD35", "GD35", 
"GD35", "GD35", "GD35", "GD35", "GD35", "GD35", "GD35", "GD35", 
"GD35", "GD35", "GD35", "GD35", "GD35", "GD35", "GD35", "GD35", 
"GD35", "GD35", "GD38", "GD38", "GD38", "GD38", "GD38", "GD38", 
"GD38", "GD38", "GD38", "GD38", "GD38", "GD38", "GD38", "GD38", 
"GD38", "GD38", "GD38", "GD38", "GD38", "GD38", "GD38", "GD38", 
"GD38", "GD38", "GD38", "GD38", "GD38", "GD38", "GD38", "GD41", 
"GD41", "GD41", "GD41", "GD41", "GD41", "GD41", "GD41", "GD41", 
"GD41", "GD41", "GD41", "GD41", "GD41", "GD41", "GD41", "GD41", 
"GD41", "GD41", "GD41", "GD41", "GD41", "GD41", "GD41", "GD41", 
"GD41", "GD41", "GD41", "GD41", "GD41", "GD41", "GD41", "GD41", 
"GD41", "GD41", "GD41", "GD46", "GD46", "GD46", "GD46", "GD46", 
"GD46", "GD46", "GD46", "GD46", "GD46", "GD46", "GD46", "GD46", 
"GD46", "GD46", "GD46", "GD46", "GD46", "GD46", "GD46", "GD46", 
"GD46", "GD46", "GD46", "GD46", "GD46", "GD46", "GD46", "GD46", 
"GD46", "GD46", "GD46", "GD46", "GD46", "GD46", "GD46", "GD46", 
"GD46", "GD46", "GD46", "GD46", "GD46", "GD46", "GD46", "GD46", 
"GD46"), outstdToday = c(3609358821, 3609358821, 3609358821, 
3609358821, 3609358821, 3609358821, 3609358821, 3609358821, 3609358821, 
3609358821, 3609358821, 3609358821, 3609358821, 3609358821, 3609358821, 
3609358821, 3609358821, 3609358821, 3609358821, 3609358821, 3609358821, 
3609358821, 3609358821, 3609358821, 3609358821, 3609358821, 3609358821, 
3609358821, 3609358821, 1097363750, 1097363750, 1097363750, 1097363750, 
1097363750, 1097363750, 1097363750, 1097363750, 1097363750, 1097363750, 
1097363750, 1097363750, 6765707714, 6765707714, 6765707714, 6765707714, 
6765707714, 6765707714, 6765707714, 6765707714, 6765707714, 6765707714, 
6765707714, 6765707714, 6765707714, 6765707714, 9535989869, 9535989869, 
9535989869, 9535989869, 9535989869, 9535989869, 9535989869, 9535989869, 
9535989869, 9535989869, 9535989869, 9535989869, 9535989869, 9535989869, 
9535989869, 9535989869, 9535989869, 9535989869, 9535989869, 9535989869, 
9535989869, 9535989869, 9535989869, 9535989869, 2635028874, 2635028874, 
2635028874, 2635028874, 2635028874, 2635028874, 2635028874, 2635028874, 
2635028874, 2635028874, 2635028874, 2635028874, 16090612138, 
16090612138, 16090612138, 16090612138, 16090612138, 16090612138, 
16090612138, 16090612138, 16090612138, 16090612138, 16090612138, 
16090612138, 16090612138, 16090612138, 20501717800, 20501717800, 
20501717800, 20501717800, 20501717800, 20501717800, 20501717800, 
20501717800, 20501717800, 20501717800, 20501717800, 20501717800, 
20501717800, 20501717800, 20501717800, 20501717800, 20501717800, 
20501717800, 20501717800, 20501717800, 20501717800, 20501717800, 
20501717800, 20501717800, 11405065289, 11405065289, 11405065289, 
11405065289, 11405065289, 11405065289, 11405065289, 11405065289, 
11405065289, 11405065289, 11405065289, 11405065289, 11405065289, 
11405065289, 11405065289, 11405065289, 11405065289, 11405065289, 
11405065289, 11405065289, 11405065289, 11405065289, 11405065289, 
11405065289, 11405065289, 11405065289, 11405065289, 11405065289, 
11405065289, 10482111279, 10482111279, 10482111279, 10482111279, 
10482111279, 10482111279, 10482111279, 10482111279, 10482111279, 
10482111279, 10482111279, 10482111279, 10482111279, 10482111279, 
10482111279, 10482111279, 10482111279, 10482111279, 10482111279, 
10482111279, 10482111279, 10482111279, 10482111279, 10482111279, 
10482111279, 10482111279, 10482111279, 10482111279, 10482111279, 
10482111279, 10482111279, 10482111279, 10482111279, 10482111279, 
10482111279, 10482111279, 2091997126, 2091997126, 2091997126, 
2091997126, 2091997126, 2091997126, 2091997126, 2091997126, 2091997126, 
2091997126, 2091997126, 2091997126, 2091997126, 2091997126, 2091997126, 
2091997126, 2091997126, 2091997126, 2091997126, 2091997126, 2091997126, 
2091997126, 2091997126, 2091997126, 2091997126, 2091997126, 2091997126, 
2091997126, 2091997126, 2091997126, 2091997126, 2091997126, 2091997126, 
2091997126, 2091997126, 2091997126, 2091997126, 2091997126, 2091997126, 
2091997126, 2091997126, 2091997126, 2091997126, 2091997126, 2091997126, 
2091997126)), row.names = c(NA, -240L), class = c("tbl_df", "tbl", 
"data.frame"))

> range(df$Date)
[1] "2024-01-09" "2046-07-09"

当生成这样的ggplot对象时:

df %>% 
  filter(Date >= "2024-01-09") %>% 
  mutate(
    outBalance =  outstdToday * Residual / 100
    ) %>% 
  group_by(month = lubridate::floor_date(Date, "month"), ticker) %>% 
  summarise(
    Amount = sum(Amount*outstdToday/100)
  ) %>% 
  ggplot(aes(x=month, y=Amount, fill = ticker)) + 
  geom_bar(stat = "identity") +
  scale_fill_manual(values = cbPalette) +
  scale_x_date(date_breaks="6 month", date_labels="%m-%Y") +
  scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6),
                     breaks = breaks_extended(6)) +
  tidyquant::theme_tq() +
  theme(axis.text.x = element_text(angle = 90),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        title = element_text(size=12, face='bold'),
        legend.position = c(0.90, .815)
        )

我得到了太细的条,x轴从04 - 2023开始,而数据从2024 - 01 - 01开始(在group_by语句中进行了一些转换之后)。

我需要从01 - 2024开始绘图,并得到更大的条。如果我在geom_bar中设置width为90,它们就足够大了,但是它们重叠了。

有人能给我点线索吗?
谢谢

u3r8eeie

u3r8eeie1#

不确定这是否是您正在寻找的,但也许不是使用floor_date()到最近的一个月,而是将它们地板到6个月的间隔?即

library(tidyverse)
library(scales)
#install.packages("tidyquant")

df %>% 
  filter(Date >= "2024-01-09") %>% 
  mutate(
    outBalance =  outstdToday * Residual / 100
  ) %>% 
  group_by(month = lubridate::floor_date(Date, "6 months"), ticker) %>% 
  summarise(
    Amount = sum(Amount*outstdToday/100)
  ) %>%
  ungroup() %>%
  ggplot(aes(x=month, y=Amount, fill = ticker)) + 
  geom_bar(stat = "identity") +
  #scale_fill_manual(values = cbPalette) +
  scale_x_date(date_breaks="6 months",
               date_labels="%m-%Y") +
  scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6),
                     breaks = breaks_extended(6)) +
  tidyquant::theme_tq() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        title = element_text(size=12, face='bold'),
        legend.position = c(0.90, .65)
)
#> `summarise()` has grouped output by 'month'. You can override using the
#> `.groups` argument.
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo

如果您使用以下命令保存图形:ggsave()时,钢筋之间的间隙更均匀:

ggsave("example.png", width = 10, height = 7.5)

创建于2023 - 06 - 21使用reprex v2.0.2

相关问题