# 生产指标条形图(并列条形图)
p1 <- ggplot (df1, aes (x = 年份, y = 增长率, fill = 指标)) +
geom_col (position = position_dodge (width = 0.8 ), width = 0.7 ) + # 并列柱状图
scale_fill_brewer (palette = "Set1" ,
labels = c ("GDP增长" , "能源生产增长" , "电力生产增长" )) +
labs (title = "中国能源生产相关指标年度增长率" ,
subtitle = "2012-2021年数据" ,
x = "年份" , y = "增长率(%)" ,
fill = "指标类型" ) +
theme_minimal (base_size = 12 ) +
theme (
axis.text.x = element_text (angle = 45 , hjust = 1 , size = 10 ),
legend.position = "bottom" ,
plot.title = element_text (face = "bold" , hjust = 0.5 ),
panel.grid.major.x = element_blank ()
) +
scale_y_continuous (expand = expansion (mult = c (0 , 0.1 ))) + # 调整y轴范围
geom_hline (yintercept = 0 , linetype = "solid" , color = "black" ) # 添加基准线
# 消费指标条形图(堆叠条形图)
p2 <- ggplot (df2, aes (x = 年份, y = 增长率, fill = 指标)) +
geom_col (position = "stack" ) + # 堆叠柱状图
scale_fill_manual (values = c ("#66c2a5" , "#fc8d62" , "#8da0cb" ), # 自定义颜色
labels = c ("GDP增长" , "能源消费增长" , "电力消费增长" )) +
labs (title = "中国能源消费相关指度增长率" ,
subtitle = "堆叠图展示总量关系标年" ,
x = "年份" , y = "增长率(%)" ,
fill = "" ) +
theme_minimal (base_size = 12 ) +
theme (
axis.text.x = element_text (angle = 45 , hjust = 1 , size = 10 ),
legend.position = "bottom" ,
plot.title = element_text (face = "bold" , hjust = 0.5 ),
panel.grid.major.x = element_blank ()
) +
scale_y_continuous (expand = expansion (mult = c (0 , 0.1 ))) +
geom_hline (yintercept = 0 , linetype = "solid" , color = "black" )
grid.arrange (p1,p2,ncol= 2 )