我有以下数据集:
sent_results_infl =structure(list(Date = structure(c(11688, 11719, 11747, 11778,
11808, 11839, 11869, 11931, 11961, 11992, 12022, 12053, 12084,
12112, 12143, 12173, 12204, 12234, 12296, 12326, 12357, 12387,
12418, 12449, 12478, 12509, 12539, 12570, 12600, 12662, 12692,
12723, 12753, 12784, 12815, 12843, 12874, 12904, 12935, 12965,
13027, 13057, 13088, 13118, 13149, 13180, 13208, 13239, 13269,
13300, 13330, 13361, 13361, 13422, 13453, 13483, 13514, 13545,
13573, 13604, 13634, 13665, 13695, 13757, 13787, 13818, 13848,
13879, 13910, 13939, 13970, 14000, 14031, 14061, 14092, 14123,
14153, 14184, 14214, 14245, 14276, 14304, 14335, 14365, 14396,
14426, 14457, 14488, 14518, 14549, 14579, 14610, 14641, 14669,
14700, 14730, 14761, 14791, 14822, 14853, 14883, 14914, 14944,
14975, 15006, 15034, 15065, 15095, 15126, 15156, 15187, 15218,
15248, 15279, 15309, 15340, 15371, 15400, 15431, 15461, 15492,
15522, 15553, 15584, 15614, 15645, 15675, 15706, 15737, 15765,
15796, 15826, 15857, 15887, 15918, 15949, 15979, 16010, 16040,
16071, 16102, 16130, 16161, 16191, 16222, 16252, 16283, 16314,
16344, 16375, 16405, 16436, 16495, 16526, 16587, 16617, 16679,
16709, 16770, 16801, 16861, 16892, 16953, 16983, 17045, 17075,
17136, 17167, 17226, 17257, 17318, 17348, 17410, 17440, 17501,
17532, 17591, 17622, 17683, 17713, 17775, 17805, 17866, 17897,
17956, 17987, 18048, 18078, 18140, 18170, 18231, 18262, 18322,
18353, 18414, 18444, 18506), class = "Date"), HICP = c(-0.00661386863174988,
-0.0169551097107523, -0.0169551097107523, -0.0272963507897547,
-0.0583200740267619, -0.0790025561847667, -0.0686613151057643,
-0.0583200740267619, -0.0376375918687571, -0.0376375918687571,
-0.0376375918687571, -0.0583200740267619, -0.0272963507897547,
-0.0272963507897547, -0.0583200740267619, -0.0893437972637691,
-0.0686613151057643, -0.0790025561847667, -0.0583200740267619,
-0.0686613151057643, -0.0479788329477595, -0.0686613151057643,
-0.0893437972637691, -0.110026279421774, -0.0996850383427715,
-0.0583200740267619, -0.0169551097107523, -0.0272963507897547,
-0.0376375918687571, -0.0583200740267619, -0.0272963507897547,
-0.0479788329477595, -0.0376375918687571, -0.0790025561847667,
-0.0583200740267619, -0.0479788329477595, -0.0583200740267619,
-0.0686613151057643, -0.0686613151057643, -0.0583200740267619,
-0.00661386863174988, -0.0169551097107523, -0.0376375918687571,
-0.0376375918687571, -0.0272963507897547, -0.0272963507897547,
-0.0479788329477595, -0.0272963507897547, -0.0169551097107523,
-0.0169551097107523, -0.0272963507897547, -0.0376375918687571,
-0.0376375918687571, -0.110026279421774, -0.0893437972637691,
-0.0790025561847667, -0.0893437972637691, -0.0893437972637691,
-0.0790025561847667, -0.0790025561847667, -0.0790025561847667,
-0.0790025561847667, -0.0893437972637691, -0.0583200740267619,
-0.0169551097107523, 0.0450923367632621, 0.0450923367632621,
0.0554335778422645, 0.0657748189212669, 0.0967985421582741, 0.0657748189212669,
0.107139783237277, 0.127822265395281, 0.148504747553286, 0.117481024316279,
0.0967985421582741, 0.0554335778422645, -0.0583200740267619,
-0.110026279421774, -0.161732484816786, -0.151391243737784, -0.213438690211798,
-0.213438690211798, -0.275486136685812, -0.285827377764815, -0.337533583159827,
-0.296168618843817, -0.30650985992282, -0.285827377764815, -0.2237799312908,
-0.182414966974791, -0.182414966974791, -0.192756208053793, -0.110026279421774,
-0.110026279421774, -0.0996850383427715, -0.120367520500776,
-0.0996850383427715, -0.110026279421774, -0.0790025561847667,
-0.0790025561847667, -0.0790025561847667, -0.0479788329477595,
-0.0376375918687571, -0.0272963507897547, 0.00372737244725252,
0.0140686135262549, 0.00372737244725252, 0.00372737244725252,
-0.00661386863174988, -0.0169551097107523, 0.0347510956842597,
0.0347510956842597, 0.0347510956842597, 0.0140686135262549, 0.00372737244725252,
0.00372737244725252, 0.00372737244725252, -0.00661386863174988,
-0.0272963507897547, -0.0272963507897547, -0.0272963507897547,
-0.00661386863174988, -0.00661386863174988, -0.0169551097107523,
-0.0479788329477595, -0.0479788329477595, -0.0686613151057643,
-0.0790025561847667, -0.0996850383427715, -0.151391243737784,
-0.130708761579779, -0.110026279421774, -0.110026279421774, -0.141050002658781,
-0.161732484816786, -0.203097449132796, -0.182414966974791, -0.192756208053793,
-0.192756208053793, -0.203097449132796, -0.2237799312908, -0.203097449132796,
-0.2237799312908, -0.2237799312908, -0.234121172369803, -0.234121172369803,
-0.244462413448805, -0.234121172369803, -0.244462413448805, -0.296168618843817,
-0.337533583159827, -0.285827377764815, -0.254803654527808, -0.2237799312908,
-0.2237799312908, -0.254803654527808, -0.234121172369803, -0.244462413448805,
-0.244462413448805, -0.275486136685812, -0.30650985992282, -0.275486136685812,
-0.254803654527808, -0.234121172369803, -0.2237799312908, -0.161732484816786,
-0.0996850383427715, -0.120367520500776, -0.0790025561847667,
-0.141050002658781, -0.141050002658781, -0.110026279421774, -0.130708761579779,
-0.141050002658781, -0.141050002658781, -0.130708761579779, -0.151391243737784,
-0.0686613151057643, -0.0479788329477595, -0.0583200740267619,
-0.0376375918687571, -0.120367520500776, -0.130708761579779,
-0.130708761579779, -0.0996850383427715, -0.141050002658781,
-0.172073725895788, -0.192756208053793, -0.203097449132796, -0.141050002658781,
-0.130708761579779, -0.203097449132796, -0.244462413448805, -0.244462413448805,
-0.234121172369803, -0.30650985992282), Sentiment = c(-0.0938852367974697,
0.0691173937594078, 0.0943758686649928, 0.00106674712790228,
-0.0379720802311696, -0.217818998857206, -0.178742379820515,
-0.0647866133003408, -0.0485608021326917, 0.0352789741606292,
-0.0114122391719694, 0.0165766278712657, 0.0543896886804695,
0.136424285585528, -0.126423612240832, -0.100531529581755, 0.0196305108281322,
0.151986931893042, -0.062770556874737, 0.0858478886295537, 0.218991412633123,
-0.0295920864985053, -0.068352014586482, -0.0484153802536735,
-0.0453342350394509, 0.0696122761973567, 0.123969187074389, -0.0479353280397374,
0.21975825052157, -0.0908256299233092, 0.123470509648093, 0.0260467543769738,
0.023352486165758, -0.135283676881311, -0.113889982312325, 0.111911330881756,
0.0645846536995613, -0.111274866305353, 0.164590940321368, -0.00620135068433694,
0.0711462791615217, 0.23739659351409, 0.236581608763319, 0.0915223016654637,
0.311425229178683, 0.260804611945108, 0.524186959084041, 0.430741956444726,
0.418061182161742, 0.358616667438093, 0.392094581491213, 0.303652910436463,
0.567716737899095, 0.351828812911797, 0.334471670260867, 0.162894404109854,
0.0549903441714543, 0.307566764669679, 0.0792500464364159, -0.0201414257251327,
0.17592545881469, 0.187908707298155, 0.0965737549632281, 0.127192111329798,
0.0485883531170079, 0.0144732706607638, 0.169701941548511, 0.0317898304650643,
0.055792691316473, 0.260446452624683, 0.187454067744547, -0.00510822625847887,
0.0216874387142927, -0.0380580979994603, -0.0537445319522331,
0.080214368703062, 0.217956762575441, 0.10751052780328, -0.194348685456748,
-0.13077680256801, -0.158711356894759, -0.16024540465173, -0.108374018472468,
-0.188344798972832, -0.0727108812259905, -0.206143242840364,
-0.171056040757928, 0.0212201622686636, 0.0327476360554462, 0.0286455432533967,
0.218747906123952, 0.168248982803017, 0.142311990183578, 0.0880917025552231,
0.186369472594704, 0.110015563434071, 0.0881007498858529, 0.0455855050815147,
0.0820322674579188, -0.0310530971483636, 0.043562813908656, 0.0614220296005266,
0.116819356320807, 0.189471045615193, 0.198233234029914, 0.2342401136776,
0.420160177320479, 0.38027022295428, 0.247444496346657, 0.287624692562276,
0.272050604474384, 0.0704459969319335, -0.0301923332968534, 0.121671037831588,
0.011999527185794, -0.235255935374234, 0.0617146264445328, 0.0439077598037887,
0.0873494613547393, 0.145661879434212, 0.0490032708689199, -0.0607386769137194,
-0.246794351967567, -0.0174456786457967, 0.0779608544999732,
-0.181561021227248, -0.188923555491947, -0.136549780331607, -0.148556207819083,
-0.029848905767454, -0.0310233277422554, -0.078176622230836,
0.187582100045137, 0.20336459705636, 0.0897814896146745, 0.0941593508173526,
0.0911962774121049, -0.0865544559756051, 0.201147247264428, -0.0263113274240022,
-0.190419135048333, 0.119754981806081, -0.133046279626944, 0.028550247517795,
-0.272633875575666, -0.0786068704076168, -0.326238760823144,
-0.202834295328555, 0.0282430565274443, -0.0611288243522725,
-0.21981093517792, -0.226027825874896, -0.212395529212002, 0.137640976549087,
0.0539718239954721, -0.043726461477507, 0.0892588892938324, -0.157234933871102,
-0.127569457070622, -0.1143778574925, 0.0726488680809197, 0.360818984615271,
0.233639042154974, 0.423333533599531, 0.227463581840224, 0.335432135735583,
0.270723416047639, 0.337354598204467, 0.218192912088412, -0.0204670270771188,
0.136811759543355, -0.048195872789368, 0.0136920366847617, 0.174384903701444,
0.226368882129195, -0.0502861282593257, -0.0920246952399918,
0.158502546484878, 0.334843204755548, 0.430035648366348, 0.355935697918495,
0.533877439468266, 0.23688943340363, 0.195568386259819, 0.392808826769226,
0.30311865521178, 0.266094214237844, 0.129063151205618, 0.134574649209369,
0.0193363730453079, 0.244741271775269, 0.296633368351017, -0.0841123055866911,
-0.555387106983362, -0.458557605201858, -0.363162800659917, -0.226364830296438
)), row.names = c(NA, -197L), class = "data.frame")
然后,我使用以下代码来绘制数据集:
colors <- c("Inflation Outlook Index" = "#4C74C9", "Inflation Rate (HICP)" = "red4")
pretty_breaks1 <- c(-0.25,-0.15,-0.05,0.0,0.05,0.15,0.25)
pretty_breaks2 <- pretty_breaks1 *10
scaled_labels1 <- pretty_breaks1 # make the tranformation you want to have
scaled_labels2 <- pretty_breaks2 # make the tranformation you want to have
ggplot(sent_results_infl) +
geom_line(aes(x= Date, y = Sentiment, colour= "Inflation Outlook Index"), size=1) +
geom_line(aes(x= Date, y = HICP, colour= "Inflation Rate (HICP)"), linetype = "dashed", size=1) +
theme_classic() +
theme(panel.border = element_rect(fill=NA)) +
geom_rect(data=recessions.df, aes(xmin=Peak, xmax=Trough, ymin=-Inf, ymax=+Inf), fill='grey', alpha=0.2) +
scale_x_date(date_breaks = "2 years" , date_labels = "%Y") +
labs(y="", x = "", color = "") +
scale_y_continuous(breaks = pretty_breaks1,
labels = scaled_labels1,
sec.axis = sec_axis(trans = ~.*10,
breaks = pretty_breaks2,
labels = scaled_labels2)) +
scale_color_manual(values = colors) +
theme(legend.position="bottom", legend.margin=margin(-13, 0, 0, 0), legend.text=element_text(size=12))
然而,我得到的是这个图表:
?
?
其中y轴的刻度不是沿轴均匀分布的。我猜这是因为最小和最大值超出了提供的数字范围。然而,这只是我的猜测。
有没有人能帮我修一下?
非常感谢!
转载请注明出处:http://www.heshunyou.com/article/20230526/1880029.html