R语言 我怎样才能使两个不同的范围的y轴没有调整到y值

djmepvbi  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(96)

首先,这是我的dput(annual_amortizatoin)结果。

structure(list(Date = 2010:2040, Principal = c(684.87465504954, 
975.003026846142, 1050.6950457127, 1132.26323271654, 1220.16377006145, 
1314.8882545613, 1416.96644696816, 1526.9692347381, 1645.51182480402, 
1773.25718421189, 1910.91974786238, 2059.26941409425, 2219.13585045513, 
2391.41313374055, 2577.06475025127, 2777.12898423355, 2992.72472463801, 
3225.05772267236, 3475.42733514413, 3745.23379130666, 4035.98602384922, 
4349.31010782725, 4686.95835472869, 5050.81911253626, 5442.92732659417, 
5865.47592034218, 6320.82805956582, 6811.53036875204, 7340.32717346504, 
7910.17584839468, 2071.69357387502), Interest = c(5608.05592192545, 
7415.57107578718, 7339.87905692061, 7258.31086991678, 7170.41033257186, 
7075.68584807202, 6973.60765566516, 6863.60486789522, 6745.0622778293, 
6617.31691842143, 6479.65435477093, 6331.30468853906, 6171.43825217819, 
5999.16096889277, 5813.50935238204, 5613.44511839977, 5397.84937799531, 
5165.51637996096, 4915.14676748919, 4645.34031132666, 4354.5880787841, 
4041.26399480606, 3703.61574790463, 3339.75499009705, 2947.64677603915, 
2525.09818229114, 2069.7460430675, 1579.04373388128, 1050.24692916828, 
480.398254238642, 25.949951783305), Balance = c(99315.1253449505, 
98340.1223181043, 97289.4272723916, 96157.1640396751, 94937.0002696136, 
93622.1120150523, 92205.1455680842, 90678.1763333461, 89032.664508542, 
87259.4073243302, 85348.4875764678, 83289.2181623735, 81070.0823119184, 
78678.6691781778, 76101.6044279266, 73324.475443693, 70331.750719055, 
67106.6929963826, 63631.2656612385, 59886.0318699319, 55850.0458460826, 
51500.7357382554, 46813.7773835267, 41762.9582709904, 36320.0309443963, 
30454.5550240541, 24133.7269644883, 17322.1965957362, 9981.86942227119, 
2071.69357387652, 0)), class = "data.frame", row.names = c(NA, 
-31L))
R Console

然后我用三条重叠的geom_lines制作了ggplot()。

annual_amortization_chart_1 = ggplot() +
  geom_line(data = annual_amortization, aes(x = Date, y = Principal), color = "#D55E00", linewidth = 1, stat = "identity") +
  geom_line(data = annual_amortization, aes(x = Date, y = Interest), color = "#009E73", linewidth = 1, stat = "identity") +
  geom_line(data = annual_amortization, aes(x = Date, y = Balance / 10), color = "#0072B2", linewidth = 1) +
  ggtitle("Flow on Constituents of Loan Amortization Over Time",
          subtitle = "Overall, Interest and Balance decreased over time, while Principal showed an increasing") +
  scale_x_continuous(name = "Year", breaks = seq(2010, 2040, 2)) +
  scale_y_continuous(name = "Principal & Interest ($)",
                     sec.axis = sec_axis(~. * 13, name = "Balance ($)")) +
  annotate("text", x = 2015, y = 2000, label = "Principal", fontface = "bold", color = "#D55E00", size = 3.5) +
  annotate("text", x = 2015, y = 6550, label = "Interest", fontface = "bold", color = "#009E73", size = 3.5) +
  annotate("text", x = 2019, y = 7950, label = "Balance", fontface = "bold", color = "#0072B2", size = 3.5) +
  theme(plot.background = element_rect(fill = "gray96"),
        panel.background = element_rect(fill = "gray96"),
        panel.grid.major.x = element_line(color = "gray88"),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(color = "gray88", size = 0.3, linetype = 1),
        panel.grid.minor.y = element_line(color = "gray88", size = 0.3, linetype = 1),
        axis.ticks = element_blank(),
        plot.title = element_text(size = 12, color = 'black'),
        plot.subtitle = element_text(size = 10),
        axis.title.x = element_text(size = 9),
        axis.title.y = element_text(size = 9), 
        axis.text.x = element_text(size = 8),
        axis.text.y = element_text(size = 8))

annual_amortization_chart_1

从代码中,我做了三个geom_line,它们有不同的y范围。当我运行这段代码时,我将第三个geom_line图的y值调整为y = Balance / 10,所以两个图的范围没有明显差异,而且图似乎很好地重叠。但我想做的是,要知道数据的确切值,请将ggplotly()添加到该图表中。如果我调整y值并像我所做的那样将两个图表重叠,则当应用ggplotly()时,将输出调整后的y值,而不是实际的y值。
我想在不调整y值的情况下很好地覆盖两个图。换句话说,当我应用ggplotly()时,我想显示原始的y值。我们可以使用什么代码或函数来实现这一点?

edqdpe6u

edqdpe6u1#

您可以覆盖工具提示中显示的内容,例如:

plotly::ggplotly(annual_amortization_chart_1, tooltip = c("x", "text"))

然后我们只需要每一层显示你想要的内容。例如,你可以指定该系列应该使用Balance / 10作为行,但Balance作为工具提示。

aes(x = Date, y = Balance / 10, text = Balance)

相关问题