如何在ggplot2中将geom_vline()和geom_hline()图例与其他图例分开显示?

11
我正在尝试使用ggplot2在R中创建一个图表,该图表显示地下水保护标准的水平线以及显示建设项目开始的垂直线。我已经创建了单位和样本是否低于检测限制的图例。所有这些图例都按照我想要的显示出来,但是水平和垂直线的图例与其他图例重叠。我尝试过多种方法,包括使用show_guide、使用不同的数据框架以及override.aes = list(),但没有任何方法可以得到所需的结果。
以下是数据的样式:
head(dmr_data)
    location_id sample_date analysis_result lt_measure default_unit           param_name
154        MWH1  2004-06-02          0.0150                    mg/L   Arsenic, dissolved
155        MWH1  2004-06-02          0.0850                    mg/L    Barium, dissolved
156        MWH1  2004-06-02          0.0002          <         mg/L Beryllium, dissolved
158        MWH1  2004-06-02          0.0005          <         mg/L   Cadmium, dissolved
162        MWH1  2004-06-02          0.0020          <         mg/L      Lead, dissolved
164        MWH1  2004-06-02          0.0010          <         mg/L  Thallium, dissolved
    DMR_limit  GWPS non_detect
154   0.01000 0.010          0
155   0.17340 2.000          0
156   0.00005 0.004          1
158   0.00100 0.005          1
162   0.00500 0.015          1
164   0.00060 0.002          1

这里是绘图的代码:

 combo_plot <- function(df){ 

  limits = df

  shaded_dates <- data.frame(xmin = c(as.POSIXct("2004-06-01", format = "%Y-%m-%d"), 
                                  as.POSIXct("2013-10-01", format = "%Y-%m-%d")), 
                         xmax = c(as.POSIXct("2013-10-01", format="%Y-%m-%d"), 
                                  max(df$sample_date)),
                         ymin = c(-Inf, -Inf), 
                         ymax = c(Inf, Inf),
                         years = c("background", "compliance"))

  ggplot(data = df, aes(x = sample_date, y = analysis_result)) + 
    geom_point(data = df, aes(colour = default_unit, shape = factor(non_detect)), size = 4) + 
    geom_line(data = df, aes(colour = default_unit), size = 1) +
    facet_wrap(~ param_name, scale="free") + 

# Plot legends, labels, and titles
ggtitle(paste("Time Series Plots for Monitoring Well", 
              df$location_id[1], "\n", sep=" ")) + 
ylab("Analysis Result") +
xlab("Sample Date") + scale_x_datetime(labels = date_format("%Y")) +
theme(axis.text.x = element_text(angle = 90)) +
theme(plot.margin = unit(c(0.75, 0.75, 0.75, 0.75), "in")) + 
theme_bw() + 
scale_colour_discrete(name = "Units", guide = "legend") + 

# add rectangles for date ranges
geom_rect(data = shaded_dates, aes(xmin = xmin, ymin = ymin, xmax = xmax, 
                                   ymax = ymax, fill = years),
          alpha = 0.2, inherit.aes = FALSE) +
scale_fill_manual(values=c("blue","green")) +

# add horizontal lines for EPA MCL and Upper Prediction Limit
geom_hline(data = limits, aes(yintercept = GWPS, linetype = "GWPS"), show_guide = TRUE, size = 0.75) +
geom_hline(data = limits, aes(yintercept = DMR_limit, linetype = "DMR Limit"), show_guide = TRUE, size = 0.75) +

# create custom legend using guide
theme(axis.title.x = element_text(size = 15, vjust=-.2)) +
theme(axis.title.y = element_text(size = 15, vjust=0.3)) +
guides(colour = guide_legend("Units"), fill = guide_legend("Dates"),
       linetype = guide_legend("Limits")) +
scale_shape_manual(name = "Measure", labels = c("Non-Detect", "Detected"),
                   values = c("1" = 21, "0" = 4)) + 

# add vertical line to show start of "East Pushout" construction
geom_vline(xintercept = as.numeric(as.POSIXct("2008-08-01", format="%Y-%m-%d")),
           linetype="dotted", show_guide = T)

}

我使用plyr为所有井创建分面图。
d_ply(dmr_data, .(location_id), .progress = "text", failwith(NA, combo_plot), .print = TRUE)

这是输出的样子。 在此输入图片描述 任何帮助都将不胜感激!
1个回答

6
使用 guides() 中的 override.aes = list(linetype = 0) 可以达到所需的效果,并通过为线型添加新的刻度来排除垂直施工线出现在图例中。请将您的 hline() 部分替换为:
## add horizontal lines for EPA MCL and Upper Prediction Limit
geom_hline(data = limits, aes(yintercept = GWPS, linetype = "GWPS"), colour = "black", size = 0.75, show_guide = T) +
geom_hline(data = limits, aes(yintercept = DMR_limit, linetype = "DMR Limit"), size = 0.75, show_guide = T) +
scale_linetype_manual(name = "Limits", labels = c("GWPS", "DMR Limit"), values = c("GWPS" = 1, "DMR Limit" = 2)) +

将您的 guides() 代码替换为:

guides(colour = guide_legend(override.aes = list(linetype = 0 )), 
       fill = guide_legend(override.aes = list(linetype = 0 )), 
       shape = guide_legend(override.aes = list(linetype = 0 )), 
       linetype = guide_legend()) +

如果您希望在图例中显示虚线垂直线,请向geom_vline(aes())scale_linetype_manual()添加相应的参数。


1
你能解释一下你的 guides() 代码到底在做什么吗? - theforestecologist
2
@theforestecologist,与其让图例使用从变量到线型的映射 - 例如,aes(linetype = variable)默认情况下会为每个variable级别显示不同的线型 - 您可以使用guide_legend(override.aes=list(linetype=0))覆盖图例中的映射。这只是告诉图例忽略来自aes()的线型映射,并使用linetype=0 - Nate Pope
2
我应该提到,linetype=0 意味着没有线条。在 guides() 中,应用于不同比例尺的 colourfill 等,这将从这些比例尺的图例中删除线条。 - Nate Pope
谢谢,@Nate。有道理! - theforestecologist

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接