Remove line and background in two legends

I made this plot:

#data
sal<-data.frame(x=factor(c(2,2,1,1),labels=c('House','Work')))
sal$id<-factor(c(2,1,2,1),labels=c('t0','t1'))
sal$val<-c(305,280,290,180)
sal$lim1<-c(75,75,150,150)
sal$lim2<-c(150,150,300,300)

#plot
p<-ggplot(sal,aes(id,y=val,fill=id))+
  geom_bar(stat="identity",position="dodge",width=.75)+
  scale_fill_brewer(palette='Set1',type='qual',name='Time')+
  theme(axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        panel.background=element_rect(fill="transparent",colour=NA),
        panel.grid.minor=element_line(colour='grey',linetype=2), 
        panel.grid.major=element_line(colour='grey'),
        plot.background=element_rect(fill="transparent",colour=NA),
        axis.ticks=element_blank(),
        axis.text.x=element_blank(),
        legend.position='bottom')+
  geom_hline(aes(yintercept=lim1,lty='lim1'),lwd=1,show_guide=T)+
  geom_hline(aes(yintercept=lim2,lty='lim2'),lwd=1,show.guide=T)+
  scale_linetype_manual(name="Recommendations",values=1:2,
                        labels=c('R1','R2'))
p+facet_grid(.~x)+theme(strip.background=element_rect(colour='white',fill='transparent'),
                        strip.text.x=element_text(size=15,face='bold'))

      

I need to erase a line in the legend Time

that appears inside the colors. And at the same time, I want a transparent background in the legend Recommendations

.

+3


source to share


1 answer


you can try this with a little modification to your code and help from @Henrik

ggplot(sal,aes(x=id,y=val,fill=id))+
    geom_bar(stat="identity",position="dodge",width=.75) +
        scale_fill_brewer(palette='Set1',type='qual',name='Time')+
            theme(axis.title.x=element_blank(),
axis.title.y=element_blank(),
panel.background=element_rect(fill="transparent",colour=NA),
panel.grid.minor=element_line(colour='grey',linetype=2), 
panel.grid.major=element_line(colour='grey'),
plot.background=element_rect(fill="transparent",colour=NA),
axis.ticks=element_blank(),
axis.text.x=element_blank(),
legend.position='bottom')+
    facet_grid(.~x)+theme(strip.background=element_rect(colour='white',fill='transparent'), strip.text.x=element_text(size=15,face='bold'))+
        geom_hline(aes(yintercept=lim1,lty='lim1'),lwd=0,show_guide=T)+
            geom_hline(aes(yintercept=lim2,lty='lim2'),lwd=0,show.guide=T)+
                geom_hline(aes(yintercept=lim1,lty='lim1'),lwd=1,show_guide=T)+
                    geom_hline(aes(yintercept=lim2,lty='lim2'),lwd=1,show.guide=F)+
                        scale_linetype_manual(name="Recommendations",values=c(3,1), labels=c('R1','R2'))+
                            theme(legend.key = element_rect(fill = "transparent"))+
                                guides(fill = guide_legend(override.aes = list(linetype = 0)), color = guide_legend(override.aes = list(linetype = 0)))
#

      



enter image description here

+1


source







All Articles