我正在为数据绘制三个序列图
> dput(test)
structure(list(Industry = structure(c(2L,3L,1L,4L,2L,4L),.Label = c("Overall","High Tech","Other Non-Manufacturing","Services (Non-Financial)"),class = "factor"),variable = structure(c(1L,3L),.Label = c("2018 % (actual)","2019 % (actual/Budgeted)","2020 % (Forecast)"),value = c(3.8,3.7,3.9,3.4,3.8,3.5,3.3,3.8)),row.names = c(3L,7L,8L,10L,14L,18L,19L,21L,25L,29L,30L,32L),class = "data.frame")
这三个系列是:2018%(实际),2019%(实际/预算),2020%(预测)
问题是我希望图例以相同的顺序显示,但图形显示的图例序列为:2020%(预测),2018%(实际),2019%(实际/预算)
ggplot(subset(test,variable!="2020 % (Forecast)")) +
aes(x=Industry,y=value,fill=factor(variable,levels = c("2018 % (actual)","2019 % (actual/Budgeted)"))) +
geom_bar(,width = 0.9,stat="identity",position = position_dodge2(width=NULL,preserve = "single"))+
scale_fill_manual(values = c("#006D9E","#00A8C8","#FDFEFE"))+
geom_text(aes(label=paste0(value,"%")),colour="black",size=3,position=position_dodge(width = 0.9),vjust=0.2,angle=90,hjust=2) +
geom_point(data=subset(test,variable=="2020 % (Forecast)"),aes(x=Industry,colour=variable),position = position_dodge(0.5),show.legend = F)+
geom_line(data=subset(test,colour=variable,group=1),position = position_dodge(1)) +
geom_text(data=subset(test,label=paste0(value,position=position_dodge(width =1),vjust= -0.25)+
labs(x=NULL,y=NULL)+
theme(axis.text.x = element_text(face="bold",color = "#626366",size = 10,vjust=0.6))+
scale_y_continuous(expand = c(0,0),labels = number_format(suffix = "%"),limits=c(0,max(test$value,na.rm = TRUE)+
max(test$value,na.rm = TRUE)/5))+
scale_x_discrete(limits=levels(test$Industry),labels = function(x) str_wrap(x,width = 5))
ggplot中有什么方法可以更改图例的顺序?