【问题标题】:Order of the legend in ggplotggplot中图例的顺序
【发布时间】:2023-05-07 02:18:02
【问题描述】:

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

> 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有什么办法可以改变图例的顺序吗?

【问题讨论】:

    标签: r ggplot2


    【解决方案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))
    

    给你:

    【讨论】: