我只需要在一张图片中显示分配的预算,已用预算(这两个以条形显示)和教育公共支出的执行百分比(以一行显示)之间的对比。
我设法以分开的方式正确显示了我需要的图形,换句话说,我的条形图已经准备好,我的线形图也准备就绪。但是,每个图形的数据都属于不同的数据帧,因此在尝试将它们全部合并为一个时遇到了问题。另外,我需要线形图的y轴为辅助轴。
#Education Budget Stages
Years <- c("2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018")
Allocated <- c(2769.56, 3049.32, 3447.86, 3858.57, 4333.35, 5173.91, 5294.66, 5030.17, 4928.42, 5152.78, 5288.91)
Spent <- c(1911.31, 2817.23, 3049.02, 3567.99, 3867.27, 4666.91, 4792.20, 4525.44, 4360.03, 4812.47, 4970.93)
Execution <- c(69.01, 92.39, 88.43, 92.47, 89.24, 90.2, 90.51, 89.97, 88.47, 93.4, 93.99)
df <- data.frame(Years, Allocated, Spent)
dfa <- data.frame(Years, Execution)
require(tidyr)
df.long <- gather(df, Budget_Stages, Values, -Years)
library(ggplot2)
#Evolution of Allocated and Spent Budget in Education
ged<- ggplot(data = df.long, aes(x = Years, y = Values, fill = Budget_Stages)) +
coord_cartesian(ylim=c(1500,5500)) +
theme_bw() +
geom_col(width= 0.8, colour="black", position = position_dodge(width=0.7)) +
scale_fill_manual(values=c("#F5B498", "#CECECE")) +
guides(fill=guide_legend(""))
library(grid)
library(ggrepel)
ged1 <- ged +
theme(legend.position= c(0.5,-0.3), legend.direction = "horizontal") +
theme(plot.margin = unit(c(1.5,2,1.5,2),"cm"))
#Execution of Public Expenditure in Education
ge2 <- ggplot(data=dfa, aes(x=Years, y=Execution, group=1)) +
geom_line(size=0.5, aes(color="Execution")) +
scale_y_continuous(sec.axis = dup_axis()) + geom_line(linetype="solid", color="yellow") +
geom_point(color="yellow")
print(ged1 + ged2 labs(x="", y=""))
我希望只看到一张图形,其中包含我需要的所有信息,不过R表示存在审美错误。