Sandhya Ghildiyal
Sandhya Ghildiyal

Reputation: 273

Order of the legend in ggplot

I am plotting a three series graph for the data

> 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")

The three series are: 2018 % (Actual), 2019 % (Actual/Budgeted), 2020 % (Forecast)

The problem is I want the legend in the same sequence but the graph is showing the legend sequence as: 2020 % (Forecast),2018 % (Actual), 2019 % (Actual/Budgeted)

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))

Is there any way in ggplot to change the sequence of legends?

Upvotes: 2

Views: 692

Answers (1)

StupidWolf
StupidWolf

Reputation: 47008

You can use guides(). First we start with your plot:

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))

Now we specify guides, your line is a color legend and your bars have a fill, and you just specify the title and order insider guide_legend():

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

Gives you:

enter image description here

Upvotes: 1

Related Questions