0

I have some variable and want to use in legend with colors, simply its work like:

## fake plot
plot(0)
## color legend
LCol <- c("PValue > 0.05"="dimgray","PValue <0.05"="red", "PValue <0.05&logFC>|1|"="darkturquoise", "adj.P.Val <0.1&logFC>|1|"="navy")
## legend
legend('topright', legend=rev(names(LCol)), pch = 19, col=rev(LCol),cex=.6)

but i want to replace variable value in legend text using bquote function and i tried:

## Parameters
padj_Cutoff <- 0.1
pval_Cutoff <- 0.05
logFC_Cutoff <- 1
mylegend <- c(bquote(PValue > .(pval_Cutoff)),
          bquote(PValue < .(pval_Cutoff)),
          bquote(c(PValue,lfc) < .(c(pval_Cutoff,logFC_Cutoff))),
          bquote(c(adj.P.Val,lfc) < .(c(padj_Cutoff,logFC_Cutoff))))

plot(0)
leg.col <- c("dimgray", "red","darkturquoise","navy")
legend('topright', legend=rev(mylegend), pch = 19,col = rev(leg.col),cex=.6)

but the legend is not similar what i was expected (please see expected legend in attached figure).

expected legend please find this figure.

enter image description here

Purvik Dhorajiya
  • 4,662
  • 3
  • 34
  • 43
RKK
  • 31
  • 11
  • Why do you need to use `bquote` here? It doesn't look like you are using an special characters that would require the complexity of `bquote`. Have you tried just using paste? – dayne Jul 18 '16 at 13:18

1 Answers1

1

I would not use bquote here -- you're not gaining anything by doing so. paste works for what you need.

padj_Cutoff <- 0.1
pval_Cutoff <- 0.05
logFC_Cutoff <- 1
mylegend <- c(paste0("PValue > ", pval_Cutoff),
              paste0("PValue > ", pval_Cutoff),
              paste0("PValue < ", pval_Cutoff, 
                     " & logFC > |", logFC_Cutoff, "|"),
              paste0("adj.PValue < ", padj_Cutoff, 
                     " & logFC > |", logFC_Cutoff, "|"))
plot(0)
leg.col <- c("dimgray", "red","darkturquoise","navy")
legend('topright', legend=rev(mylegend), pch = 19,col = rev(leg.col),cex=.6)

enter image description here

bquote is helpful when using special characters. For example:

plot(0, type = "n")
text(1, 0, labels = bquote(paste(mu)))

enter image description here

dayne
  • 7,504
  • 6
  • 38
  • 56
  • Thank you so much dayne, Actually i tried single paste in all legend text so it didnt work, then i found something http://stackoverflow.com/q/7210346 similar and used bquote. – RKK Jul 18 '16 at 13:41