想要在复杂的图例中添加彩色线条

时间:2015-02-10 03:21:39

标签: r plot legend

这是包含图例的情节代码:

png(filename=paste0(filename2, SubNameStr, "_", date, "_", BlankUncertainty, "-percent-blank-unc_", "no_add_unc", "_", " (R#_yyyy-mm-dd_hh-mm-ss).png"),
  type="cairo",
  units="cm",
  width=30,
  height=15,
  pointsize=12,
  res=resolution)

par(mar =c(10, 4, 4, 2)+0.1)
plotCI(1:abscissa, 
     LoopVariable[ ,"F_corrected_normed"],
     xlim=c(0, abscissa + 1),
     ylim=c(LoopVariable[1,"weighted_mean_fraction_modern"] -      (LoopVariable[1,"weighted_mean_fraction_modern"] * y),
            LoopVariable[1,"weighted_mean_fraction_modern"] +  (LoopVariable[1,"weighted_mean_fraction_modern"] * y)),
     xaxs="i",
     yaxs="i",
     uiw=LoopVariable[ ,"F_corrected_normed_error_NOSYSERR"], err="y",
     main=(LoopVariable[1,"Samples..Sample_ID"]),
     xlab="", ylab="F_modern",
     pch=19
     )
if (LoopVariable[1,"Job..R"] == "14047/2")
abline(h=IAEA_C2consensus, lwd=1, col="blue", lty=11)

if (LoopVariable[1,"Job..R"] == "18331/3")
abline(h=TIRI_Iconsensus, lwd=1, col="blue", lty=11)

if (LoopVariable[1,"Job..R"] == "24889/3")
abline(h=FIRI_Cconsensus, lwd=1, col="blue", lty=11)

if (LoopVariable[1,"Job..R"] == "18331/4")
abline(h=TIRI_Jconsensus, lwd=1, col="blue", lty=11)

if (LoopVariable[1,"Job..R"] == "24889/4")
abline(h=FIRI_DFconsensus, lwd=1, col="blue", lty=11)

if (LoopVariable[1,"Job..R"] == "24889/5")
abline(h=FIRI_Econsensus, lwd=1, col="blue", lty=11)

if (LoopVariable[1,"Job..R"] == "24889/9")
abline(h=FIRI_Iconsensus, lwd=1, col="blue", lty=11)

abline(h=LoopVariable[1,"weighted_mean_fraction_modern"], lwd=1, col="red")
mtext(paste0("no additional error added, ", BlankUncertainty, " Blk unc, ",  date))
if (LoopVariable[1,"Job..R"] == "14047/2") {
legend("bottom",
     xpd = TRUE,
     inset=c(0,-0.6),

       legend=c(paste0("degrees of freedom = ",  LoopVariable[1,"degrees_of_freedom"]),
                paste0("Chi square                 = ", LoopVariable[1,"Chi_square"]),
                paste0("Chi square reduced = ", LoopVariable[1,"Chi_square_reduced"]),
                paste0(" F_weighted_mean  = ", LoopVariable[1,"weighted_mean_fraction_modern"]),
                paste0(" F_consensus           = ", IAEA_C2consensus))

                )

     }
else {       

legend("bottom",
   xpd = TRUE,
   inset=c(0,-0.6),

   legend=c(paste0("degrees of freedom ", LoopVariable[1,"degrees_of_freedom"]),
            paste0("Chi square ", LoopVariable[1,"Chi_square"]),
            paste0("Chi square reduced ", LoopVariable[1,"Chi_square_reduced"]),
            paste0(lty=1, col="red", " F_weighted_mean = ", LoopVariable[1,"weighted_mean_fraction_modern"])
            )
   )}


dev.off()

我遇到的问题是,我如何在最后一行为图例添加一条红色和蓝色的短线,以便F_consensus和F_weighted_mean值专用于图中右侧的彩色abline 。 这是从我的示例代码生成的图: enter image description here

1 个答案:

答案 0 :(得分:0)

您要指定参数ltycol,如下所示:

legendText <- c(paste0("degrees of freedom = ",  LoopVariable[1,"degrees_of_freedom"]), 
                paste0("Chi square                 = ", LoopVariable[1,"Chi_square"]), 
                paste0("Chi square reduced = ", LoopVariable[1,"Chi_square_reduced"]),
                paste0(" F_weighted_mean  = ", LoopVariable[1,"weighted_mean_fraction_modern"]),
                paste0(" F_consensus           = ", IAEA_C2consensus))
legend("bottom",
       legendText,
       xpd = TRUE,
       inset=c(0,-0.6),
       col = c(NA,NA,NA,'red','blue'),
       lty = c(NA,NA,NA,1,2))

我通常在位置上提供文本标签(取决于第二或第三个参数),因为这是一个名为legend的函数legend()令人困惑的事情。