我创建了一个在ggplot2中创建绘图的函数,其中一个要求是根据每个条的分数结果对条形图进行排序。为此,我重新安排了数据框,但是当我尝试将scale_x_discrete与函数变量(Geog)一起使用时,它只是不起作用:
f_OS_RegionF <- function(Geog) {
#Carrier Mean Score###########
mean_c<-mean(co_dataa$Score)
###########################
#Region=deparse(substitute(Region))
data_carr<-co_dataa
ScorexGeog_C <- cast(data_carr, as.formula(paste(Geog,"Carrier",sep="~")), mean, value="Score")
names(ScorexGeog_C)[2] <- "Score"
ScorexGeog_C[2]<-round(ScorexGeog_C[2],digits=1)
ScorexGeog_C<- ScorexGeog_C[order(ScorexGeog_C$Score), ]
write.table(ScorexGeog_C,file="ScorexGeog_C_F_Check.csv",sep=",",row.names=F, col.names=T)
d <- ggplot(ScorexGeog_C, aes_string(x=Geog, y="Score", fill=Geog))+
geom_bar(stat="identity", colour="white")+
coord_flip()+
scale_fill_manual(values = coloress)+
scale_x_discrete(limits=paste("ScorexGeog_C",Geog,sep="$"))+
theme(axis.ticks = element_blank(), axis.title.y = element_blank(),axis.text.y = element_blank(), axis.title.x = element_blank(), axis.text.x = element_blank())+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank())+
theme(legend.position = "none") +
geom_text(data=ScorexGeog_C,aes_string(x=Geog,y="Score",label="Score"),colour="white",vjust=0.5,hjust=2,size=2.5, fontface="bold")+
geom_text(data=ScorexGeog_C,aes_string(x=Geog,y=0.3,label=Geog),colour="white", angle=0, vjust=0.5, hjust=0, fontface="bold",size=2)+
geom_hline(yintercept=mean_c, colour = "gray",linetype="solid", size =1, alpha=.8)
print(d)
}