如何阻止两个ggplot图例重叠?

时间:2017-02-17 22:05:27

标签: r ggplot2 legend

随着时间的推移,我正在制作TCE浓度的线图。该图有一个阴影区域,我用geom_rect创建,一个虚线水平线,我用geom_hline创建。除了解释折线图中的线条含义之外,我想要一个解释阴影区域和虚线的含义的图例。我已经想出如何使用阴影区域执行此操作,并将其标记为“系统操作期”。但是,我不知道如何为虚线做这个。当我在geom_hline函数中设置show_guide = FALSE时,我能够在图例中创建一条水平线,但阴影区域和水平虚线在图例中重叠(即红色虚线位于薰衣草阴影框的顶部) )。如何创建不重叠的虚线图例和阴影区域图例?提前致谢!

这是我的代码:

ggplot(data= Data, aes(x = Date, y = TCE, group = Well, colour = Well)) +
  geom_rect(data = Data, 
            aes(xmin = as.Date("2016-04-01", "%Y-%m-%d"),
                xmax = as.Date("2016-12-31",  "%Y-%m-%d"),
                ymin = -Inf, 
                ymax = Inf,
                fill = "System Operating Period"),
            linetype = 0,
            alpha = 0.2,
            inherit.aes=FALSE) +
    geom_line(size = 0.75) +
    geom_point()+
    xlab("") + ylab("TCE (ug/L)") +
    scale_color_manual(values = c("navy", "#70a845", "#4aac8b", "#7262cd", "#b3923f", "#588ecd", "#cf5a2f", "cadetblue2", "#c1705d", "#c06595", "#c454bc")) +
scale_x_date(breaks = date_breaks("6 months"),
             date_labels = ("%b %Y")) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),
      panel.grid.major.x = element_blank(),
      panel.grid.major.y = element_line( size=.1, color="gray"),
      panel.grid.minor = element_blank(), 
      axis.line.x = element_line(color = "black"),
      axis.line.y = element_line(color = "black"),
      panel.background = element_rect(fill = "white"),
      legend.key=element_blank()) +
scale_fill_manual("", breaks = "System Operating Period", values = "lavender") +
geom_hline(aes(yintercept=120), colour = "#990000", linetype = "dashed", show_guide = TRUE)
  scale_linetype_manual(labels = "Standard")

这是我的一些数据:

         Date          Well  TCE
208 2012-04-12 Recovery Well  260
209 2012-05-21 Recovery Well 1790
210 2012-06-12 Recovery Well 2160
211 2012-07-20 Recovery Well 1480
212 2012-08-06 Recovery Well 1550
213 2012-09-19 Recovery Well 1170
214 2012-10-03 Recovery Well  782
215 2012-11-14 Recovery Well  989
216 2013-05-09 Recovery Well 1060
217 2013-06-18 Recovery Well 1610
218 2013-07-18 Recovery Well 1540
219 2013-08-16 Recovery Well 1220
220 2013-09-09 Recovery Well 1130
221 2013-10-14 Recovery Well 1040
222 2013-11-12 Recovery Well  834
223 2014-05-23 Recovery Well  869
224 2014-06-18 Recovery Well 1250
225 2014-07-14 Recovery Well  447
226 2014-08-18 Recovery Well  728
227 2014-09-23 Recovery Well  695
228 2014-10-21 Recovery Well  984
229 2014-11-13 Recovery Well  801
230 2015-04-24 Recovery Well  782
231 2015-05-12 Recovery Well 1110
232 2015-06-18 Recovery Well 1030
233 2015-07-10 Recovery Well 1250
234 2015-08-12 Recovery Well 1260
235 2015-09-14 Recovery Well 1090
236 2015-10-02 Recovery Well  943
237 2015-11-04 Recovery Well  735
238 2016-04-14 Recovery Well  806
239 2016-05-02 Recovery Well 1030
240 2016-06-07 Recovery Well  711
241 2016-07-06 Recovery Well  939
242 2016-08-15 Recovery Well  766
243 2016-09-12 Recovery Well  773
244 2016-10-11 Recovery Well  770
245 2016-11-11 Recovery Well  805
246 2016-12-07 Recovery Well  707

这是我制作的图表的图片: enter image description here

1 个答案:

答案 0 :(得分:1)

您可以将其传递给linetype美学:

ggplot(data= Data, aes(x = Date, y = TCE, group = Well, colour = Well)) +
    geom_rect(data = Data, 
              aes(xmin = as.Date("2016-04-01", "%Y-%m-%d"),
                  xmax = as.Date("2016-12-31",  "%Y-%m-%d"),
                  ymin = -Inf, 
                  ymax = Inf,
                  fill = "System Operating Period"),
              linetype = 0,
              alpha = 0.2,
              inherit.aes=FALSE) +
    geom_line(size = 0.75) +
    geom_point()+
    xlab("") + ylab("TCE (ug/L)") +
    scale_color_manual(values = c("navy", "#70a845", "#4aac8b", "#7262cd", "#b3923f", "#588ecd", "#cf5a2f", "cadetblue2", "#c1705d", "#c06595", "#c454bc")) +
    scale_x_date(breaks = date_breaks("6 months"),
                 date_labels = ("%b %Y")) +
    theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),
          panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_line( size=.1, color="gray"),
          panel.grid.minor = element_blank(), 
          axis.line.x = element_line(color = "black"),
          axis.line.y = element_line(color = "black"),
          panel.background = element_rect(fill = "white"),
          legend.key=element_blank()) +
    scale_fill_manual("", breaks = "System Operating Period", values = "lavender") +
    geom_hline(aes(yintercept=120, linetype = 'Standard'), colour = "#990000") +
    scale_linetype_manual(NULL, values = 'dashed', labels = "Standard")

plot with three legends

您可能希望更新订单和标签,但至少可以使用。