两个变量是否可以重叠,一个变量是否可以在r中使用facet_wrap单独表示?

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

首先,这是我的dataframe dput代码。

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))

在我的数据框架中,有一些列叫做principal、interest和balance。我想通过facet_wrap为这些列绘制一个geom_line图,并打印出两个图作为结果。
两个图的x轴都将使用Date列。对于第一个带有facet_wrap函数的图,我想同时覆盖Principals和Interests。在第二个图中,我想只表示Balance。是否可以实现我试图表示的图?

eyh26e7m

eyh26e7m1#

library(tidyverse)
annual_amortization %>%
  pivot_longer(-Date) %>%
  mutate(facet = if_else(name == "Balance", "Balance", "P&I") %>% 
           forcats::fct_rev()) %>% # to reverse the facet order from alphabetical
  ggplot(aes(Date, value, color = name)) +
  geom_line() +
  facet_wrap(~facet, scales = "free_y")

相关问题