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] 삭제
몇 마디 만하겠습니다