首页 > 编程知识 正文

r语言ggplot2分组堆叠柱状,ggplot堆叠柱状

时间:2023-05-05 03:16:46 阅读:275794 作者:4013

柱状堆积图的数据格式,原本全是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)这个位置是相对于整个画面来说的,(0,0)就是左下角原点的位置,(1,0)是右下角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))

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。