ggplot中图例的顺序

时间:2019-11-12 12:26:39

标签: r ggplot2

我正在为数据绘制三个序列图

> dput(test)
structure(list(Industry = structure(c(2L, 3L, 1L, 4L, 2L, 3L, 
1L, 4L, 2L, 3L, 1L, 4L), .Label = c("Overall", "High Tech", "Other Non-Manufacturing", 
"Services (Non-Financial)"), class = "factor"), variable = structure(c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("2018 % (Actual)", 
"2019 % (Actual/Budgeted)", "2020 % (Forecast)"), class = "factor"), 
    value = c(3.8, 3.7, 3.9, 3.4, 3.8, 3.7, 3.8, 3.9, 3.5, 3.3, 
    3.7, 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,y=value,colour=variable),
             position = position_dodge(0.5),show.legend = F)+
  geom_line(data=subset(test,variable=="2020 % (Forecast)"),aes(x=Industry,y=value,colour=variable,group=1),position = position_dodge(1)) + 
  geom_text(data=subset(test,variable=="2020 % (Forecast)"), aes(x=Industry,y=value,label=paste0(value, "%")),size=3, 
            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,angle=90, 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中有什么方法可以更改图例的顺序?

1 个答案:

答案 0 :(得分:1)

您可以使用guides()。首先,我们从您的情节开始:

library(tidyverse)

p = 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,y=value,colour=variable),
             position = position_dodge(0.5),show.legend = F)+
  geom_line(data=subset(test,variable=="2020 % (Forecast)"),aes(x=Industry,y=value,colour=variable,group=1),position = position_dodge(1)) + 
  geom_text(data=subset(test,variable=="2020 % (Forecast)"), aes(x=Industry,y=value,label=paste0(value, "%")),size=3, 
            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,angle=90, 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))

现在,我们指定参考线,您的行是一个颜色图例,并且您的条形都有填充,您只需指定标题和顺序内部参考者guide_legend():

p + guides(color=guide_legend(title="Forecast",order = 2),fill=guide_legend(title="Actual",order = 1))

给你:

enter image description here