【问题标题】:Legend showing an unexpected black line with geom_vline图例显示带有 geom_vline 的意外黑线
【发布时间】:2015-07-10 14:16:45
【问题描述】:

在查看 StackOverflow 提供的一些答案后,我一直在尝试添加一个额外的图例,但不知何故我无法完成这项工作。我正在使用以下代码:

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

如何从图例中删除这条黑色丑陋的线?

我使用的数据集可以在here找到。

对此的任何指示将不胜感激。

【问题讨论】:

    标签: r ggplot2 legend


    【解决方案1】:

    我没有机会对此进行测试,但这可能对您有用。我通常从 Zev Ross 备忘单开始,并修改了那里的一些技巧,以使我第一次猜测删除图例中的形状:

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

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

    【讨论】:

      【解决方案2】:

      show_guide=FALSE 呢:

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

      正如@eipi10 在评论中提到的那样,这个解决方案在这种情况下不起作用,因为它也会隐藏 vline 图例。

      ggplot 绘制图例时,它会尝试使用所有可能的属性(填充、颜色、线型),因此绘制fill 图例时您还可以获得vline 线属性(颜色和线型)。

      你能做什么?正如您在@eipi10 的回答中看到的那样,您可以添加一个guides 命令来告诉ggplot 对于填充比例,颜色属性(vline 图例使用的属性)必须是不可见的(NA)。

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

      但是您可以在“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)) +
      

      另一种解决方案是在geom_vline 之后绘制geom_area,如下所示:how to remove line from fill scale legend using geom_vline and geom_histogram r ggplot2

      这是一个不错的解决方案,您也可以尝试在不绘制两次 vline 的情况下执行此操作,但这在您的情况下效果不佳,因为您有 alpha=0.6 值。所以同样对于这个解决方案,你需要使用override.aes,有这样的东西:

      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) 
      

      或者倒置两个geom并添加:

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

      我写这篇文章只是因为我花了一些时间试图弄清楚这个问题,为什么要使用guide_legend(override.aes) 以及是否有其他解决方案。写作是安定下来的好方法。

      【讨论】:

      • 去掉了fill图例中的垂直线,但也删除了vline颜色图例。
      • 是的,图例也被删除了。
      • 我看到了,想看看有没有其他解决办法
      • @mucio - 非常感谢,这是非常有用的信息。
      【解决方案3】:

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

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-03
      • 1970-01-01
      • 2021-11-16
      • 2019-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多