[英]ggplot2 two dataframes and two axes data not plotting
我已經查看了類似的問題,並試圖從具有溫度和降水的氣候圖中遵循一個示例。
我在正確的軌道上,因為兩個軸似乎都有正確的限制。 由於某種原因,人口比例的條形圖沒有顯示出來。
我沒有收到任何錯誤,我想我只是錯過了一些東西。 任何幫助表示感謝。
這是我的代碼:
## plot per capita spending and old people increase on same plot
##calculate a and b for scaling second axis
ylim.prim=range(TA1_summ$Total.Health.Expenditure.per.Capita.in.Dollars)
ylim.sec=range(Prop75pls_df$Prop_75pls)
## formula to scale axes for double axes
b <- diff(ylim.prim)/diff(ylim.sec)
a <- ylim.prim[1] - b*ylim.sec[1]
##
ggplot()+
geom_point(data=TA1_summ,aes(x=Year,y=Total.Health.Expenditure.per.Capita.in.Dollars))+
geom_col(data=Prop75pls_df,aes(x=Year,y=Prop_75pls))+
#
scale_y_continuous("per capita health care spending (CDN)", sec.axis = sec_axis(~ (. - a)/b, name = "Proportion of population 75+")) +
theme_bw()
這是圖表,請注意人均支出的分數在那里,但比例 75+ 的條不存在。
以下是2個數據框。 謝謝
> dput(TA1_summ)
structure(list(Year = 1975:2021, Total.Health.Expenditure.in.Millions.of.Dollars = c(12199.4,
14049.8, 15450, 17106.8, 19169.7, 22298.4, 26276.7, 30759.1,
34038.6, 36743.1, 39842.4, 43338.1, 46789.2, 50960.1, 56096.2,
61092.9, 66437.4, 69853.4, 71519.1, 73159.6, 74237.4, 75082.3,
78741.4, 84066.9, 90467.9, 98609.9, 107201.8, 115055.9, 123591.3,
131570.3, 140489.5, 151037.6, 162992.3, 171964.7, 182026, 192956,
199383.5, 207501.6, 212366.5, 218591.2, 228095.8, 237351.6, 246090.5,
255913.1, 267215.6, 301454.8, 308043.3), Total.Health.Expenditure.per.Capita.in.Dollars = c(527.1,
599.1, 651.2, 713.9, 792.1, 909.6, 1058.7, 1224.6, 1341.9, 1434.9,
1541.8, 1660.4, 1769.2, 1902.1, 2056.6, 2206.2, 2369.6, 2462.1,
2493.3, 2522.7, 2533.5, 2535.7, 2633, 2787.8, 2975.8, 3213.5,
3455.8, 3668.9, 3905.7, 4119.2, 4357.1, 4637.2, 4955.8, 5172.3,
5412.8, 5674.4, 5806.3, 5977.4, 6053.3, 6168.4, 6388.7, 6573.1,
6733.8, 6904.4, 7108, 7931.9, 8018.5), Total.Health.Expenditure.in.Constant.1997.Millions..of.Dollars = structure(c(22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L,
36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L), .Label = c("", "100,842.1", "105,685.2",
"110,371.9", "114,160.9", "119,135.3", "125,204.7", "128,118.1",
"131,863.3", "137,580.0", "138,330.6", "141,924.8", "142,284.2",
"144,006.1", "148,241.5", "154,367.0", "158,689.7", "162,505.5",
"165,289.9", "177,972.0", "180,029.3", "39,684.0", "40,766.5",
"41,609.5", "42,940.6", "44,203.5", "46,676.2", "51,123.1", "53,484.5",
"55,455.0", "57,311.8", "59,558.8", "62,192.5", "63,808.3", "66,399.6",
"69,198.5", "71,242.7", "73,922.6", "75,123.8", "75,381.6", "75,804.7",
"76,063.2", "76,161.2", "78,741.4", "82,555.7", "86,940.8", "91,035.0",
"96,707.7"), class = "factor"), Total.Health.Expenditure..per.Capita.in.Constant..1997.Dollars = structure(c(2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
18L, 23L, 24L, 21L, 20L, 19L, 17L, 22L, 25L, 26L, 27L, 28L, 29L,
30L, 31L, 32L, 33L, 34L, 35L, 36L, 38L, 37L, 41L, 39L, 40L, 42L,
43L, 44L, 45L, 46L, 47L, 48L), .Label = c("", "1,714.7", "1,738.5",
"1,753.8", "1,791.9", "1,826.5", "1,903.9", "2,059.8", "2,129.4",
"2,186.2", "2,238.1", "2,304.7", "2,382.8", "2,412.7", "2,478.4",
"2,536.9", "2,572.1", "2,572.8", "2,595.8", "2,613.9", "2,627.9",
"2,633.0", "2,636.6", "2,647.9", "2,737.7", "2,859.8", "2,966.7",
"3,117.5", "3,215.6", "3,339.8", "3,455.5", "3,540.6", "3,657.7",
"3,806.9", "3,853.5", "3,921.1", "4,028.3", "4,045.9", "4,055.7",
"4,063.7", "4,088.4", "4,152.1", "4,275.0", "4,342.3", "4,384.3",
"4,396.8", "4,682.8", "4,686.3"), class = "factor"), Total.Health.Expenditure.as.a.Percentage.of.GDP = c(7,
7, 7, 7, 6.8, 7.1, 7.1, 7.9, 8.1, 8, 8, 8.2, 8.1, 8.1, 8.4, 8.8,
9.5, 9.7, 9.6, 9.2, 8.9, 8.7, 8.7, 8.9, 9, 8.9, 9.4, 9.6, 9.8,
9.9, 9.9, 10.1, 10.3, 10.4, 11.6, 11.6, 11.2, 11.4, 11.2, 11,
11.5, 11.7, 11.5, 11.5, 11.6, 13.7, 12.7)), row.names = c(NA,
47L), na.action = structure(48:105, .Names = c("48", "49", "50",
"51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61",
"62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72",
"73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83",
"84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94",
"95", "96", "97", "98", "99", "100", "101", "102", "103", "104",
"105"), class = "omit"), class = "data.frame")
> dput( Prop75pls_df)
structure(list(Year = c(1975, 1976, 1977, 1978, 1979, 1980, 1981,
1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992,
1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
2015, 2016, 2017, 2018, 2019, 2020, 2021), Prop_75pls = c(2.95766554965851,
3.00127503220918, 3.05129670837176, 3.11304063523594, 3.18934390585209,
3.25769147676869, 3.33837995428998, 3.41631016897725, 3.50054745341369,
3.59622982947609, 3.6940577447635, 3.79014384913296, 3.89859027580637,
4.00409456787977, 4.10658248874934, 4.2127362212327, 4.28299210465241,
4.34725032514741, 4.39672019981841, 4.44576578342527, 4.55353117703572,
4.67241226584368, 4.79907907522439, 4.91693361767564, 5.03853805953953,
5.15658755870346, 5.67448038744973, 5.78812317405195, 5.91193068088551,
6.01461053319038, 6.12839020321239, 6.25496029096157, 6.34749433891701,
6.4232424596923, 6.47606470566458, 6.54317971748121, 6.61162617975518,
6.67251883104279, 6.73754838318347, 6.81701708941406, 6.89445520796233,
7.00259186733946, 7.12988965237494, 7.2674973568116, 7.42862666992543,
7.59432265315821, 7.82893516903733)), class = "data.frame", row.names = c("1975",
"1976", "1977", "1978", "1979", "1980", "1981", "1982", "1983",
"1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991",
"1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999",
"2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007",
"2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015",
"2016", "2017", "2018", "2019", "2020", "2021"))
您必須對軸上的數據使用相同的轉換:
ggplot()+
geom_col(data=Prop75pls_df,aes(x=Year,y=(a+Prop_75pls*b)), alpha=.25)+
geom_point(data=TA1_summ,aes(x=Year,y=Total.Health.Expenditure.per.Capita.in.Dollars))+
#
scale_y_continuous("per capita health care spending (CDN)", sec.axis = sec_axis(~ (. - a)/b, name = "Proportion of population 75+")) +
theme_bw()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.