【R】ggplot2_堆积图

柱状堆积图的数据格式,原本全是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))

在这里插入图片描述

R语言中,使用ggplot2包可以绘制堆积柱状堆积柱状可以用于比较多个类别在不同组之间的占比或数量。下面是使用ggplot2包绘制堆积柱状的基本步骤: 首先,导入ggplot2包,并准备好要用于绘的数据。数据应该为长格式(长格式数据是指每行代表一个观察值,每列对应一个变量)。确保数据有多个类别变量以及分组变量。 接下来,使用ggplot()函数创建一个空的绘对象。指定数据源以及要使用的x和y变量。 然后,使用geom_bar()函数添加柱状层。设置stat参数为"identity"来保持堆积柱状的原始值。使用fill参数来指定填充颜色变量,这将决定不同类别的颜色。 最后,可以使用scale_fill_manual()函数手动设置颜色,也可以使用labs()函数添加标题和标签等。 绘代码的示例: ```R library(ggplot2) # 准备数据 # 假设有一个数据框df,包含类别变量category,分组变量group以及对应的数值value # 例如,可以通过下面的代码创建一个示例数据 df <- data.frame(category = rep(c("A", "B", "C"), each = 3), group = rep(c("Group 1", "Group 2", "Group 3"), 3), value = c(10, 15, 20, 8, 12, 16, 5, 7, 9)) # 创建绘对象 ggplot(data = df, aes(x = group, y = value, fill = category)) + # 添加堆积柱状 geom_bar(stat = "identity") + # 设置颜色 scale_fill_manual(values = c("#F8766D", "#00BFC4", "#C77CFF")) + # 添加标题和标签 labs(title = "堆积柱状", x = "分组", y = "数值") ``` 这段代码将绘制一个堆积柱状,x轴为分组变量,y轴为数值变量,不同类别的柱状块使用不同的颜色进行填充。可以通过设置颜色值来自定义颜色,也可以根据需要添加其他层或修改参数来进一步定制绘的样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值