1 柱形图
R软件中生成柱形图与饼图的基本函数是一致的,都是使用geom_bar(),不过饼图需要+coord_polar()来将柱形图转为饼图。一般来说,R软件在生成柱形图时的思路与Excel是不同的,Excel只需要一个数据框,标明标签和数量,就可以生成柱形图,但R一般需要数据框包涵每个个体的情况,以标签为划分计量来建立柱形图。以下面的数据框df1为例,在ggplot2中生成柱形图需要在geom_bar()中添加参数:stat = "identity"
> product.num <- c(3000, 9000, 15000, 21000, 27000)
> year <- 1:5
> df1 <- data.frame(year, product.num)
> df1
year product.num
1 1 3000
2 2 9000
3 3 15000
4 4 21000
5 5 27000
> ggplot(df1, aes(x=year, y=product.num)) +
+ geom_bar(stat = "identity", fill = "palegreen") + #fill填充柱形颜色
+ geom_line(size=2, color="lightcyan") + #添加直线
+ geom_point(color="palegreen3") + #标点
+ labs(title = "年产量的五年规划", x="执行年度", y="年产量/吨") + #修改坐标轴及标题
+ theme(text = element_text(family = "AdobeHeitiStd-Regular"), #更换字体
+ plot.title = element_text(hjust = 0.5)) + #将标题设置为居中
+ scale_y_continuous(breaks = df1$product.num) + #修改y轴刻度位置
+ scale_x_continuous(breaks = 1:5, labels = c("第一年", "第二年", "第三年", "第四年", "第五年")) #修改x轴刻度