图例显示带有geom_vline的意外黑线 [英] Legend showing an unexpected black line with geom_vline

查看:194
本文介绍了图例显示带有geom_vline的意外黑线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看了StackOverflow提供的一些答案后,我一直在尝试添加其他图例,但以某种方式我无法使它正常工作.我正在使用以下代码:

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

得到以下图:

dput(head(toPlot))的输出是:

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.

推荐答案

我不确定ggplot为什么在填充图例中添加黑色垂直线.但是,您可以通过将其添加到绘图代码中来摆脱它们:

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

这篇关于图例显示带有geom_vline的意外黑线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆