rm(list=ls(all=TRUE))
library(ggplot2)
library(gcookbook)
library(scales)
data1<-data.frame(
value<-c(205463.75,12.31,105743.06,12979.31,63958.87,0,96530.3,2610,45029.74,2689.78,
88141,1300,69885.97,1000),
place<-c('市区合计','市区合计','天畅合计','天畅合计','院长合计','院长合计','光明合计','光明合计',
'光机合计','光机合计','景龙合计','景龙合计','启安合计','启安合计'),
category<-c('贷款余额','未代偿余额','贷款余额','未代偿余额','贷款余额','未代偿余额',
'贷款余额','未代偿余额','贷款余额','未代偿余额','贷款余额','未代偿余额',
'贷款余额','未代偿余额') )
data2<-data.frame(
ratio<-c(0.0001,0.1227,0,0.027,0.0597,0.0147,0.0143),
category1<-c('市区合计','天畅合计','院长合计','光明合计','光机合计','景龙合计','启安合计'))
p<-ggplot(data=data1,aes(x=place,y=value,fill=category))+
geom_bar(stat="identity",width=0.5,position="dodge")+
geom_text(aes(label=value,vjust = -0.8, hjust =.5), show.legend = FALSE)+
labs(title="各地区贷款情况 单位:万元", x="", y="")+
theme_bw()+theme(panel.grid =element_blank())+
theme(panel.background = element_rect(fill="lightcyan"))+
theme(legend.title=element_blank(),legend.position="bottom")+
theme(title=element_text(size=15,color="black",face="bold",hjust=0.2))
p+geom_line(data=data2,aes(x=category1,y=rescale(ratio,c(0,200000)),group=category1))+
scale_y_continuous(breaks=pretty_breaks(5),sec.axis=sec_axis(~rescale(.,c(0,0.5)),name="Ratio",labels=sprintf("%d%%",(0:5)*10)))
```p运行没有问题,添加双坐标轴运行时出现如下报错
Error: Aesthetics must be either length 1 or the same as the data (7): fill