First, this is my outcome of 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
Then I made ggplot() with three overlaid geom_lines.
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
From the code, I made three geom_lines which have different ranges of y. When I ran this code, I adjusted the y value of the third geom_line graph to y = Balance / 10, so the range of the two graphs did not differ significantly, and the graph seemed to be well overlaid. But the thing I want to do is, to know the exact value of the data by adding ggplotly() to this graph. If I adjuste the y value and overlay the two graphs as I did, when ggplotly() is applied, the adjusted y value is output, not the actual y value.
I want to overlay the two graphs well without any adjustments to the y value. In other words, when I apply ggplotly(), I want to show the original y value. What code or function can we use to do this?
You can override what is shown in the tooltips, e.g. with:
plotly::ggplotly(annual_amortization_chart_1, tooltip = c("x", "text"))
Then we just need each layer to show what you want. For instance, you could specify that that series should use Balance / 10
for the line, but Balance
for the tooltip.
aes(x = Date, y = Balance / 10, text = Balance)