如何在不重新缩放的情况下在 ggplot 中设置第二个 y 轴?

How to set the second y-axis in ggplot without rescaling?

这是我的数据框:


df = dput(sent_results)
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), class = "Date"), IP = c(-0.595925341785417, 
-0.566320864529598, -0.487375591847412, -0.507111910017959, -0.467639273676865, 
-0.418298478250498, -0.497243750932684, -0.428166637335771, -0.487375591847412, 
-0.428166637335771, -0.566320864529598, -0.418298478250498, -0.398562160079952, 
-0.477507432762138, -0.438034796421045, -0.595925341785417, -0.595925341785417, 
-0.438034796421045, -0.566320864529598, -0.329485046483038, -0.359089523738859, 
-0.329485046483038, -0.349221364653585, -0.309748728312493, -0.329485046483038, 
-0.250539773800852, -0.230803455630306, -0.230803455630306, -0.15185818294812, 
-0.21106713745976, -0.161726342033394, -0.270276091971399, -0.270276091971399, 
-0.191330819289213, -0.21106713745976, -0.220935296545033, -0.0827810693512062, 
-0.21106713745976, -0.1222537056923, -0.0729129102659335, -0.043308433010114, 
-0.0334402739248399, 0.134318430524805, 0.0257686805867993, 0.104713953268985, 
0.104713953268985, 0.173791066865899, 0.203395544121718, 0.351417930400817, 
0.341549771315545, 0.321813453144998, 0.351417930400817, 0.351417930400817, 
0.33168161223027, 0.430363203083003, 0.578385589362102, 0.53891295302101, 
0.598121907532649, 0.647462702959016, 0.519176634850464, 0.706671657470656, 
0.706671657470656, 0.726407975641202, 0.746144293811749, 0.834957725579209, 
0.765880611982296, 0.825089566493935, 0.913902998261395, 0.894166680090848, 
0.854694043749756, 0.973111952773034, 0.65733086204429, 0.677067180214835, 
0.56851743027683, 0.529044793935736, 0.45009952125355, 0.183659225951171, 
-0.181462660203939, -0.516980069103231, -0.90183827342889, -1.10906961421963, 
-1.21761936415763, -1.33603727318091, -1.13867409147545, -1.07946513696381, 
-1.06959697787853, -1.06959697787853, -0.852497478002523, -0.891970114343616, 
-0.862365637087796, -0.852497478002523, -0.704475091723424, -0.753815887149791, 
-0.586057182700145, -0.536716387273778, -0.368957682824132, -0.359089523738859, 
-0.398562160079952, -0.319616887397766, -0.299880569227219, -0.220935296545033, 
-0.132121864777573, -0.0531765920953867, -0.102517387521753, 
-0.0137039557542944, 0.00603236241625246, -0.043308433010114, 
0.0159005215015266, -0.1222537056923, -0.0235721148395671, -0.0137039557542944, 
-0.112385546607027, -0.141990023862847, -0.112385546607027, -0.171594501118666, 
-0.24067161471558, -0.181462660203939, -0.191330819289213, -0.270276091971399, 
-0.201198978374486, -0.280144251056672, -0.250539773800852, -0.181462660203939, 
-0.368957682824132, -0.438034796421045, -0.507111910017959, -0.457771114591592, 
-0.497243750932684, -0.477507432762138, -0.378825841909405, -0.359089523738859, 
-0.339353205568312, -0.339353205568312, -0.418298478250498, -0.368957682824132, 
-0.359089523738859, -0.408430319165226, -0.280144251056672, -0.299880569227219, 
-0.329485046483038, -0.260407932886127, -0.309748728312493, -0.161726342033394, 
-0.24067161471558, -0.290012410141946, -0.21106713745976, -0.378825841909405, 
-0.280144251056672, -0.309748728312493, -0.339353205568312, -0.21106713745976, 
-0.230803455630306, 0.0455049987573461, -0.043308433010114, 0.00603236241625246, 
0.065241316927893, -0.00383579666902026, 0.0159005215015266, 
-0.0334402739248399, 0.262604498633359, 0.0257686805867993, 0.134318430524805, 
0.0751094760131657, 0.0849776350984384, 0.114582112354259, 0.233000021377538, 
0.213263703206992, 0.193527385036445, 0.242868180462812, 0.33168161223027, 
0.311945294059725, 0.47970399850937, 0.489572157594644, 0.47970399850937, 
0.765880611982296, 0.558649271191557, 0.489572157594644, 0.489572157594644, 
0.578385589362102, 0.469835839424097, 0.509308475765191, 0.519176634850464, 
0.410626884912458, 0.489572157594644, 0.459967680338824, 0.410626884912458, 
0.351417930400817, 0.341549771315545, 0.341549771315545, 0.302077134974451, 
0.0948457941837126, 0.282340816803904, -0.911706432514163, -2.53995268158425, 
-0.832761159831976, -0.378825841909405), Sentiment = c(0.010752688172043, 
0.0625, 0.0634920634920635, 0.0467289719626168, 0.0761904761904762, 
0.0747663551401869, 0.0759493670886076, 0.0306748466257669, -0.025, 
0.0136986301369863, -0.021978021978022, -0.0645161290322581, 
-0.00746268656716418, 0.0172413793103448, -0.0857142857142857, 
-0.0229007633587786, 0.0111111111111111, -0.00892857142857143, 
0.0126582278481013, 0.0144927536231884, 0.112, 0.0775862068965517, 
0.065359477124183, 0.0786516853932584, 0.032967032967033, 0.0378151260504202, 
0.0535714285714286, 0.101694915254237, 0.0967741935483871, 0.0892857142857143, 
0.115044247787611, 0.130434782608696, 0.111764705882353, 0.0754716981132075, 
0.176470588235294, 0.043859649122807, 0.173076923076923, 0.105263157894737, 
0.0441767068273092, 0.0833333333333333, 0.125, 0.114285714285714, 
0.0288461538461538, 0.0327868852459016, 0.0857142857142857, 0.111111111111111, 
0.0641025641025641, 0.142857142857143, 0.127118644067797, 0.080168776371308, 
0.135922330097087, 0.0473684210526316, 0.0772200772200772, 0.0646551724137931, 
0.0454545454545455, 0.0485436893203883, 0.0909090909090909, 0.0661764705882353, 
0.0689655172413793, 0.147286821705426, 0.16, 0.0810810810810811, 
0.0880503144654088, 0.0337552742616034, 0.0656934306569343, -0.0050251256281407, 
0.0173913043478261, 0, -0.0114942528735632, 0.00411522633744856, 
0.0168539325842697, -0.0178571428571429, 0.00727272727272727, 
-0.0201612903225806, -0.0588235294117647, -0.0463917525773196, 
-0.12037037037037, -0.0782608695652174, -0.0920502092050209, 
-0.0963855421686747, -0.0897435897435897, -0.0566801619433198, 
-0.0769230769230769, -0.0925925925925926, -0.0463320463320463, 
-0.0403225806451613, -0.0625, -0.0194552529182879, -0.0314465408805031, 
-0.00636942675159236, 0.014218009478673, -0.00602409638554217, 
-0.0132450331125828, -0.0158730158730159, -0.0261780104712042, 
-0.0284090909090909, -0.0112781954887218, -0.0363636363636364, 
-0.0130718954248366, 0.0197628458498024, -0.0206896551724138, 
-0.03125, 0.00434782608695652, -0.0194805194805195, -0.012987012987013, 
-0.0048780487804878, -0.0229885057471264, -0.0166666666666667, 
0.0132013201320132, 0.0122699386503067, 0, -0.0137614678899083, 
-0.0458015267175573, -0.0563380281690141, -0.056, -0.0531914893617021, 
-0.0540540540540541, -0.037037037037037, -0.0540540540540541, 
-0.0427350427350427, -0.00666666666666667, -0.0373831775700935, 
-0.0526315789473684, -0.0142857142857143, -0.0512820512820513, 
-0.0381679389312977, -0.0344827586206897, -0.0578512396694215, 
-0.0542635658914729, -0.0416666666666667, -0.0666666666666667, 
-0.0551181102362205, -0.0338164251207729, -0.0463576158940397, 
-0.0466666666666667, -0.00836820083682008, -0.0164835164835165, 
0, 0.00947867298578199, 0, 0, -0.00425531914893617, 0, -0.0198675496688742, 
-0.0214592274678112, -0.0062111801242236, -0.0112359550561798, 
-0.0507246376811594, -0.0133333333333333, -0.0198675496688742, 
-0.0268199233716475, 0.0652173913043478, 0.0404040404040404, 
0.0545454545454545, 0.0470588235294118, 0.0373831775700935, -0.00934579439252336, 
0.0133333333333333, 0.0392156862745098, -0.00645161290322581, 
0, 0.0133333333333333, 0.013953488372093, 0.049645390070922, 
0.0151515151515152, 0.0396825396825397, 0.0658682634730539, 0.0932203389830508, 
0.0638297872340425, 0.0859375, 0.0495049504950495, 0.0930232558139535, 
0.0867052023121387, 0.11864406779661, 0.10989010989011, 0.134453781512605, 
0.102857142857143, 0.126865671641791, 0.0609137055837563, 0.0604026845637584, 
0.0823529411764706, 0.0526315789473684, 0.0212765957446809, -0.0212765957446809, 
0.0125, 0.0148514851485149, 0.0241545893719807, 0.0101010101010101, 
0.0177514792899408, 0.0446428571428571, 0.0227272727272727, 0.0508474576271186, 
-0.0699300699300699, -0.0866425992779783, -0.0557103064066852, 
-0.0384615384615385)), row.names = c(NA, -196L), class = "data.frame")

我正在尝试在 ggplot 中绘制两条线并更改第二个 y 轴。与许多解决方案不同,我没有重新定义第二个 y 轴,所以我只需要在第二个轴上键入“数字”。我尝试了这个不成功的解决方案:

# Vector of desired breakpoints for secondary axis
sec_breaks <- c(75, 85, 95, 105, 110)
# Vector of breakpoints for the first axis
scaled_breaks <- c(-1, -0.5, 0, 0.5, 1)

g = ggplot(df) + 
  geom_line(aes(x=Date, y=Sentiment*10), color= "#4C74C9", size=1) +
  geom_line(aes(x=Date, y=IP), color= "red4", linetype = "dashed", size=1) +
  theme_classic() +
  theme(panel.border = element_rect(fill=NA)) + labs(y="% \n", x = "") +
  scale_y_continuous(breaks = c(75, 85, 95, 105, 110),
                     sec.axis = sec_axis(trans = ~.,
                                         breaks = scaled_breaks,
                                         labels = sprintf("%.3f", sec_breaks))) 
g = g + geom_rect(data=recessions.df, aes(xmin=Peak, xmax=Trough, ymin=-Inf, ymax=+Inf), fill='grey', alpha=0.2)
g

我得到了您在下面看到的内容:

相反,我希望两个 y 轴都带有数字并且间隔适当。

谁能帮帮我?

谢谢!

下面是一个如何在不更改数据的情况下变换坐标轴的示例:

library(ggplot2)

correction_factor <- 10
pretty_breaks1 <- pretty(c(df$Sentiment, df$IP / correction_factor))
pretty_breaks2 <- pretty_breaks1 * correction_factor
scaled_labels1 <- pretty_breaks1  * 10       # make the tranformation you want to have 
scaled_labels2 <- pretty_breaks2  * 10 + 95  # make the tranformation you want to have


ggplot(df, aes(x = Date)) + 
   geom_line(aes(y = Sentiment), color= "#4C74C9", size=1) +
   geom_line(aes(y = IP / correction_factor), color= "red4", linetype = "dashed", size=1) +
   theme_classic() +
   theme(panel.border = element_rect(fill=NA)) +
   labs(y="%", x = "") +
   scale_y_continuous(breaks = pretty_breaks1,
                      labels = scaled_labels1,
                      sec.axis = sec_axis(trans = ~.*correction_factor,
                                          breaks = pretty_breaks2,
                                          labels = scaled_labels2))