Home > Software design >  2 separate legends for the same aesthetic ggplot2
2 separate legends for the same aesthetic ggplot2

Time:12-07

I am trying to get 2 separate legends for different groups of variables when using "facet_wrap" in ggplot 2. Basically I want to break up the legend into 2, one with "Yes/No" and one with "Sometimes/Never" and have each one close to those variables that they correspond with.



Answer<-c("Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Yes","No","No","Yes","No","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Sometimes","Never","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never","Sometimes","Never","Never")

var<-c(rep("A",50),rep("B",50),rep("C",50),rep("D",50),rep("E",50),rep("F",50),rep("G",50),rep("H",50),rep("I",50))

cat<-c(rep("red",10),rep("blue",10),rep("green",10),rep("yellow",10),rep("orange",10),rep("red",10),rep("blue",10),rep("green",10),rep("yellow",10),rep("orange",10),
rep("red",10),rep("blue",10),rep("green",10),rep("yellow",10),rep("orange",10),
rep("red",10),rep("blue",10),rep("green",10),rep("yellow",10),rep("orange",10),
rep("red",10),rep("blue",10),rep("green",10),rep("yellow",10),rep("orange",10),
rep("red",10),rep("blue",10),rep("green",10),rep("yellow",10),rep("orange",10),
rep("red",10),rep("blue",10),rep("green",10),rep("yellow",10),rep("orange",10),
rep("red",10),rep("blue",10),rep("green",10),rep("yellow",10),rep("orange",10),
rep("red",10),rep("blue",10),rep("green",10),rep("yellow",10),rep("orange",10))

df2<-tibble(Answer,var,cat)


layout2<-"
AB##
CDE#
FGHI"
  
df2$Answer<-factor(df2$Answer,levels=c("Yes","No","Sometimes","Never"))

ggplot(df2,aes(fill=Answer,x=cat)) 
  geom_bar(stat="count",position=position_dodge()) 
 facet_manual(~var,scales="free_x",design=layout2) 
  scale_fill_manual(values = c("blue4","lightblue","thistle2","darkorchid4")) 
  labs(x="",y="Frequency") 
  theme(legend.title = element_blank())

CodePudding user response:

One option would be the ggnewscale package which allows for multiple scales and legends for the same aesthetic. To make this work you have to use two geom_bar layers, one for your A and B panels containing the Yes/No categories and one for the other panels.

library(ggplot2)
library(ggh4x)

ggplot(df2, aes(x = cat))  
  geom_bar(
    data = ~ subset(df2, var %in% c("A", "B")), aes(fill = Answer),
    stat = "count", position = position_dodge()
  )  
  scale_fill_manual(values = c("blue4", "lightblue"))  
  ggnewscale::new_scale_fill()  
  geom_bar(
    data = ~ subset(df2, !var %in% c("A", "B")), aes(fill = Answer),
    stat = "count", position = position_dodge()
  )  
  scale_fill_manual(name = "foo", values = c("thistle2", "darkorchid4"))  
  facet_manual(~var, scales = "free_x", design = layout2)  
  labs(x = "", y = "Frequency")  
  theme(legend.title = element_blank())

enter image description here

  • Related