【R】ggplot2_堆积图

博客提及柱状堆积图的数据格式,原本是2000 - 2019年的数据,为截图删了部分,中间作物数据需竖着放。还提到要调整堆积顺序,把Other放在最下面,可在第一行的fill()函数里设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

柱状堆积图的数据格式,原本全是2000-2019年的数据,为了一次性截到图删了一些,但是基本的格式是这样的,中间那行作物要这么竖着放
在这里插入图片描述
先画一下基本的图

setwd("C:/Users/Nengneng/Desktop")
library(ggplot2)
library(cowplot)
data <- read.csv("yield2000-2019.csv",header = T)
ggplot(data=data, aes(x=Year, y=Yiled, fill=Zuowu)) + 
  geom_bar(stat="identity")

在这里插入图片描述

# 加geom_col(width = 1) 柱状图变成直方图,去掉主子之间的间隔
ggplot(data=data, aes(x=Year, y=Yiled, fill=Zuowu)) + 
  geom_bar(stat="identity")+geom_col(width = 1)

在这里插入图片描述

# 去掉灰色背景网格
ggplot(data=data, aes(x=Year, y=Yiled, fill=Zuowu)) + 
  geom_bar(stat="identity")+geom_col(width = 1)+
  theme(panel.background = element_blank(),axis.line = element_line())

在这里插入图片描述

# 加一条y=6的水平线,要加垂直线的话改成xintercept就好啦
ggplot(data=data, aes(x=Year, y=Yiled, fill=Zuowu)) + 
  geom_bar(stat="identity")+geom_col(width = 1)+
  theme(panel.background = element_blank(),axis.line = element_line())+
  geom_hline(yintercept = 6,size =1,lty="dashed",color="red")

在这里插入图片描述

# 改变柱子的颜色及透明度
ggplot(data=data, aes(x=Year, y=Yiled, fill=Zuowu)) + 
  geom_bar(stat="identity")+geom_col(width = 1)+
  theme(panel.background = element_blank(),axis.line = element_line())+
  geom_hline(yintercept = 6,size =1,lty="dashed",color="red")+
 scale_fill_manual(values=alpha(c('#87CEFF','#6CA6CD','#4A708B',"#9FB6CD"), 0.9))

在这里插入图片描述

# 改纵坐标标签,横坐标如果改的话,加在labs()里面就行
ggplot(data=data, aes(x=Year, y=Yiled, fill=Zuowu)) + 
  geom_bar(stat="identity")+geom_col(width = 1)+
  theme(panel.background = element_blank(),axis.line = element_line())+
  geom_hline(yintercept = 6,size =1,lty="dashed",color="red")+
  scale_fill_manual(values=alpha(c('#87CEFF','#6CA6CD','#4A708B',"#9FB6CD"), 0.9))+
  labs(y="Yield (million tons)")

在这里插入图片描述

# 去掉图例的标题
ggplot(data=data, aes(x=Year, y=Yiled, fill=Zuowu)) + 
  geom_bar(stat="identity")+geom_col(width = 1)+
  theme(panel.background = element_blank(),axis.line = element_line())+
  geom_hline(yintercept = 6,size =1,lty="dashed",color="red")+
  scale_fill_manual(values=alpha(c('#87CEFF','#6CA6CD','#4A708B',"#9FB6CD"), 0.9))+
  labs(y="Yield (million tons)")+
  guides(fill=guide_legend(title=NULL))

在这里插入图片描述

#把图例放成一横行,放在图的上面
ggplot(data=data, aes(x=Year, y=Yiled, fill=Zuowu)) + 
  geom_bar(stat="identity")+geom_col(width = 1)+
  theme(panel.background = element_blank(),axis.line = element_line())+
  geom_hline(yintercept = 6,size =1,lty="dashed",color="red")+
  scale_fill_manual(values=alpha(c('#87CEFF','#6CA6CD','#4A708B',"#9FB6CD"), 0.9))+
  labs(y="Yield (million tons)")+
  guides(fill=guide_legend(title=NULL,nrow  = 1,byrow = F))+
  theme(legend.position = 'top')

在这里插入图片描述

# 调整图例的大小,以及图例的位置。(x,y)这个位置是相对于整个画面来说的,(00)就是左下角原点的位置,(10)是右下角
ggplot(data=data, aes(x=Year, y=Yiled, fill=Zuowu)) + 
  geom_bar(stat="identity")+geom_col(width = 1)+
  theme(panel.background = element_blank(),axis.line = element_line())+
  geom_hline(yintercept = 6,size =1,lty="dashed",color="red")+
  scale_fill_manual(values=alpha(c('#87CEFF','#6CA6CD','#4A708B',"#9FB6CD"), 0.9))+
  labs(y="Yield (million tons)")+
  guides(fill=guide_legend(title=NULL,nrow  = 1,byrow = F))+
  theme(legend.position = 'top')+
  theme(legend.key.size = unit(8, "pt"),legend.position = c(0.29, 0.99))

在这里插入图片描述

# 改变图例的字体大小,“bold”是加粗的意思
ggplot(data=data, aes(x=Year, y=Yiled, fill=Zuowu)) + 
  geom_bar(stat="identity")+geom_col(width = 1)+
  theme(panel.background = element_blank(),axis.line = element_line())+
  geom_hline(yintercept = 6,size =1,lty="dashed",color="red")+
  scale_fill_manual(values=alpha(c('#87CEFF','#6CA6CD','#4A708B',"#9FB6CD"), 0.9))+
  labs(y="Yield (million tons)")+
  guides(fill=guide_legend(title=NULL,nrow  = 1,byrow = F))+
  theme(legend.position = 'top')+
  theme(legend.key.size = unit(8, "pt"),legend.position = c(0.29, 0.99))+
  theme(legend.text = element_text( size =8, face="bold"))

在这里插入图片描述

# 去掉图和x轴之间的间距:scale_y_continuous(expand=c(0,0))
  如果要去掉和Y轴的间距,scale_x_continuous(expand=c(0,0))
  去掉间距之后原点处的y轴显示0,想去掉这个的话,scale_y_continuous(breaks=c(2, 4,6)),breaks可以改变y轴的显示
ggplot(data=data, aes(x=Year, y=Yiled, fill=Zuowu)) + 
  geom_bar(stat="identity")+geom_col(width = 1)+
  theme(panel.background = element_blank(),axis.line = element_line())+
  geom_hline(yintercept = 6,size =1,lty="dashed",color="red")+
  scale_fill_manual(values=alpha(c('#87CEFF','#6CA6CD','#4A708B',"#9FB6CD"), 0.9))+
  labs(y="Yield (million tons)")+
  guides(fill=guide_legend(title=NULL,nrow  = 1,byrow = F))+
  theme(legend.position = 'top')+
  theme(legend.key.size = unit(8, "pt"),legend.position = c(0.29, 0.99))+
  theme(legend.text = element_text( size =8, face="bold"))+
  scale_y_continuous(breaks=c(2, 4,6),expand=c(0,0))

但是想换一下堆积的顺序,把Other放在最下面,在第一行的fill()函数里面设置

ggplot(data=data, aes(x=Year, y=Yiled,                      fill=factor(Zuowu,levels=c("Maize","Wheat","Rice","Other")))) + 
  geom_bar(stat="identity")+geom_col(width = 1)+
  theme(panel.background = element_blank(),axis.line = element_line())+
  geom_hline(yintercept = 6,size =1,lty="dashed",color="red")+
  scale_fill_manual(values=alpha(c('#87CEFF','#6CA6CD','#4A708B',"#9FB6CD"), 0.9))+
  labs(y="Yield (million tons)")+
  guides(fill=guide_legend(title=NULL,nrow  = 1,byrow = F))+
  theme(legend.position = 'top')+
  theme(legend.key.size = unit(8, "pt"),legend.position = c(0.29, 0.99))+
  theme(legend.text = element_text( size =8, face="bold"))+
  scale_y_continuous(breaks=c(2, 4,6),expand=c(0,0))

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值