Patthebug
Patthebug

Reputation: 4787

Legend showing an unexpected black line with geom_vline

I have been trying to add an additional legend after looking at some of the answers provided at StackOverflow but somehow I can't make this work. I'm using the following code:

 x_breaks <- seq(as.Date("2010/1/1"), as.Date("2015/4/1"), "months")
    x_labels <- as.character(x_breaks, format="%b%y")

    vLines <- data.frame('Date'=as.Date('2014/1/1'))
    vLines <- rbind(vLines,vLines,vLines,vLines)
    vLines$Date[1] <- as.Date('2010/6/7')
    vLines$Date[2] <- as.Date('2012/1/1')
    vLines$Date[3] <- as.Date('2012/10/1')
    vLines$Date[4] <- as.Date('2013/1/1')

    vLines$grp <- c('Complex Cases','Breach of Contract Cases','PI, PD and WD cases','All other civil cases')  

    p <- ggplot(toPlot[1:296,], aes(x=Date, y=value, fill=OrderType)) + scale_x_date(breaks=x_breaks, labels=x_labels) +
  geom_area(stat="identity",alpha=0.6) +
#   scale_y_continuous(labels = percent_format()) +
  ggtitle('Some Title') + theme_bw() + ylab('Transactions') + xlab('') +
  theme(axis.text.y=element_text(hjust=0, angle=0), 
        axis.text.x = element_text(hjust=1, angle=45),
        panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.major.x=element_line(color='grey90',linetype='dashed'),
        panel.grid.major.y=element_line(color='grey90',linetype='dashed'),
        plot.title=element_text(size=20),
        axis.text=element_text(size=15),
        legend.text=element_text(size=20),
        legend.key=element_blank(),
        legend.title=element_blank()) +
  scale_fill_manual(values=c("#a6cee3","#1f78b4","#b2df8a","#33a02c","#fb9a99","#e31a1c","#fdbf6f","#ff7f00","#cab2d6","#6a3d9a")) +
  geom_vline(data=vLines,aes(xintercept=as.numeric(Date),color=grp,linetype=grp),size=1,show_guide=TRUE)
p

to get the following plot: enter image description here

The output of dput(head(toPlot)) is:

structure(list(Date = structure(c(14853, 14914, 15034, 15187, 
15309, 15340), class = "Date"), OrderType = structure(c(1L, 1L, 
1L, 1L, 1L, 1L), .Label = c("Delivery", "eFiling", "Filing", 
"ProcessServing", "Research"), class = "factor"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("Orders", "Revenue"), class = "factor"), 
    value = c(1, 1, 1, 1, 18, 37)), .Names = c("Date", "OrderType", 
"variable", "value"), row.names = c(NA, 6L), class = "data.frame")

How do I remove this black ugly line from the legend?

The dataset that I'm using may be found here.

Any pointers on this would be highly appreciated.

Upvotes: 4

Views: 966

Answers (3)

sefleuria
sefleuria

Reputation: 56

I don't have the chance to test this, but this may be a useful pointer for you. I usually start with the Zev Ross cheatsheet, and have modified some of the tips there to my first guess for removing shapes inside of the legend:

guides(fill = guide_legend(override.aes = list(shape=NA)))

Source: http://zevross.com/blog/2014/08/04/beautiful-plotting-in-r-a-ggplot2-cheatsheet-3/

Upvotes: 1

mucio
mucio

Reputation: 7119

What about show_guide=FALSE in:

geom_vline(data=vLines,aes(xintercept=as.numeric(Date),color=grp,linetype=grp),size=1,show_guide=FALSE)

As @eipi10 mentioned in the comment this solution doesn't work in this case, because it will hide also the vline legend.

When ggplot draws a legend it tries to use all possible properties (fill, color, linetype), so drawing the fill legend you get also the vline lines properties (color and linetype).

What can you do? As you can see in the @eipi10's answer you can add a guides command to tell to ggplot that for the fill scale the colour property (the one used by the vline legend) has to be invisible (NA).

guides(fill=guide_legend(override.aes=list(colour=NA)))

But you can achieve the same adding the guide in 'scale_fill_manual':

scale_fill_manual(values=c("#a6cee3","#1f78b4","#b2df8a","#33a02c","#fb9a99","#e31a1c","#fdbf6f","#ff7f00","#cab2d6","#6a3d9a")), 
                    guide=guide_legend(override.aes = list(colour=NA)) +

Another solution would be to draw the geom_area after geom_vline, as suggested here: how to remove line from fill scale legend using geom_vline and geom_histogram r ggplot2

This is a nice solution, you can try also to do it without plotting twice the vline, but this won't work very well in your case because you have the alpha=0.6 value. So also for this solution you need to use override.aes, having something like this:

scale_fill_manual(values=c("#a6cee3","#1f78b4","#b2df8a","#33a02c","#fb9a99","#e31a1c","#fdbf6f","#ff7f00","#cab2d6","#6a3d9a"),
                    guide=guide_legend(override.aes=list(alpha=1))) +
geom_vline(data=vLines,aes(xintercept=as.numeric(Date),color=grp,linetype=grp),size=1,show_guide=TRUE)+
geom_area(stat="identity",alpha=0.6) 

Or invert the two geom and add:

guides(fill=guide_legend(override.aes=list(alpha=1)))

I wrote this just because I spent some time trying to figure out this problem, why to use the guide_legend(override.aes) and if there are other solutions. Writing was a good way to this settle down.

Upvotes: 3

eipi10
eipi10

Reputation: 93811

I'm not sure why ggplot is adding black vertical lines to the fill legend. However, you can get rid of them by adding this to your plot code:

  guides(fill=guide_legend(override.aes=list(colour=NA)))

Upvotes: 2

Related Questions