有没有办法在同一个图上有geom_line和geom_bar而不重叠?
我想将变量“ exp_j_perc”和“ cut_j_perc”作为条形,而将“ total_growth”作为线形,但是我不知道该怎么做。
我尝试了这个,但是出现了subset
一个未知参数的错误。
如果可能的话,我还将有两个不同的y轴。
df_long %>%
ggplot(aes(x = budget_year, y = value)) +
geom_bar(subset = .(variable == "exp_j_perc" | variable == "cut_j_perc"),
stat="identity", position = "dodge2", width = 0.35, alpha = 0.75)+
geom_line(subset = .(variable == "total_growth"),
size = 0.6, linetype = "longdash") +
scale_x_continuous(breaks = c(2010,2011,2012,2013,2014,2015,2016,2017,2018)) +
scale_fill_manual(values = palette) +
xlab("Year") +
theme_minimal() +
theme(panel.grid.major.x = element_blank(),
axis.text=element_text(size=10))
数据如下:
df_long <- structure(list(year = c(2010, 2010, 2011, 2012, 2014, 2014, 2015,
2016, 2018, 2010, 2010, 2011, 2012, 2014, 2014, 2015, 2016, 2018,
2010, 2010, 2011, 2012, 2014, 2014, 2015, 2016, 2018, 2010, 2010,
2011, 2012, 2014, 2014, 2015, 2016, 2018, 2010, 2010, 2011, 2012,
2014, 2014, 2015, 2016, 2018, 2010, 2010, 2011, 2012, 2014),
budget_year = c(2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
2018, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2010,
2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2010, 2011,
2012, 2013, 2014), variable = c("exp_j_perc", "exp_j_perc",
"exp_j_perc", "exp_j_perc", "exp_j_perc", "exp_j_perc", "exp_j_perc",
"exp_j_perc", "exp_j_perc", "cut_j_perc", "cut_j_perc", "cut_j_perc",
"cut_j_perc", "cut_j_perc", "cut_j_perc", "cut_j_perc", "cut_j_perc",
"cut_j_perc", "total_d", "total_d", "total_d", "total_d",
"total_d", "total_d", "total_d", "total_d", "total_d", "total_growth",
"total_growth", "total_growth", "total_growth", "total_growth",
"total_growth", "total_growth", "total_growth", "total_growth",
"exp_justification", "exp_justification", "exp_justification",
"exp_justification", "exp_justification", "exp_justification",
"exp_justification", "exp_justification", "exp_justification",
"cut_justification", "cut_justification", "cut_justification",
"cut_justification", "cut_justification"), value = c(33.33,
0, 22.45, 46.66, 50, 64, 82.35, 92.18, 100, 66.66, 100, 77.55,
53.33, 50, 36, 17.64, 7.82, 0, 1168782, 1136279.2186499,
1131940.80814086, 1151145.76656277, 1164509.2887094, 1181777.9454623,
1224866.3370881, 1265134.21792983, 1288684.9611902, NA, -2.78091049914321,
-0.381808488427549, 1.69663981400685, 1.16088878878757, 1.48291275306511,
3.64606496434037, 3.28753265743733, 1.86152132529547, 4,
0, 11, 21, 5, 16, 56, 59, 49, 8, 27, 38, 24, 5)), row.names = c(NA,
-50L), class = c("tbl_df", "tbl", "data.frame"))
快速浏览文档表明该subset
参数在那些特定功能中不存在,并说明了为什么会出现此错误。
我建议阅读这篇文章,探讨可能的,不建议的内容以及有关数据可视化的一些意见。在数据的情况下,它是可能的是两种不同的比例y轴,因为它们中的一个表示的固定值(%),因此可以其他相对于它的y轴使用易于扩展sec.axis
在参数scales_y_continuous
。需要一些预处理来分离要传递给每个`geom_ *的变量
library(dplyr)
library(tidyr)
library(ggplot2)
df_long %>%
filter(variable %in% c("exp_j_perc", "cut_j_perc", "total_growth")) %>%
pivot_wider(names_from = variable) %>%
pivot_longer(cols = c("exp_j_perc", "cut_j_perc")) %>%
ggplot(aes(x = budget_year)) +
geom_col(aes(y = value, fill = name)) +
geom_line(aes(y = (total_growth*10) + 50), size = 2, alpha = 0.7, col = "blue") +
scale_fill_discrete(name = "") +
scale_y_continuous(sec.axis = sec_axis(~ (. -50)/10, name = "total growth")) +
labs(y = "percent %")
我不喜欢这种类型的数据,但我敏锐地意识到,有些管理人员对盲目的做法是合适的,而不是。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句