Math expression in shapeCustom legend in EnhancedVolcano
1
0
Entering edit mode
pl23 • 0
@4b83ad99
Last seen 7 months ago
Canada

Hello,

I want to incorporate mathematical expressions in my legend for shapeCustom but am not sure of how to do so. When setting up my keyvals.shape vector, I tried using names with R expression() but this doesn't seem to work. Does anyone have any suggestions? In particular, suppose I have two shapes, one where I want the legend to be log_10(p)>50 and one with log_10(p)<=50 (expressed with subscripts and math symbols). How do I go about doing this?

Thank you!

EnhancedVolcano • 709 views
ADD COMMENT
1
Entering edit mode
@james-w-macdonald-5106
Last seen 5 hours ago
United States

Use bquote. See section 4.6 in the vignette.

0
Entering edit mode

Hi,

Yes, I've tried this, but I am not sure if it is possible to assign bquote values to the names of a vector. In particular, if I use an ifelse statement, I get

> keyvals.shape = c(rep(3,10), rep(2,8))
> names(keyvals.shape) = ifelse(keyvals.shape==3, bquote(-log[10]*p >= 50),bquote(-log[10]*p<50))
Error in rep(yes, length.out = len) : 
  attempt to replicate an object of type 'language'

On the other hand, if I try to set names as a character before assigning the values, I get the following error:

> names(keyvals.shape) = "a"
> names(keyvals.shape)[keyvals.shape==3] = bquote(-log[10]*p >= 50)
Error in names(keyvals.shape)[keyvals.shape == 3] = bquote(-log[10] *  : 
  incompatible types (from language to character) in subassignment type fix

I am not too familiar with these functionalities in R, so I'd appreciate any suggestions. Thank you!

ADD REPLY
0
Entering edit mode

Ah, good point. You could instead try

> library(ggtext)
> keyvals.shape = c(rep(3,10), rep(2,8))
> names(keyvals.shape) = ifelse(keyvals.shape==3, "-log<sub>10</sub>*p≥50", "-log<sub>10</sub>*p>50")
## and then 
> EnhancedVolcano(<args go here>) + theme(legend.text = element_markdown())
ADD REPLY
0
Entering edit mode

Oh LOL. My markdown got edited by the markdown editor! What is between the p and 50 in the first case is the HTML for greater than or equal, which is , and in the next instance it's <

ADD REPLY
0
Entering edit mode

Oh snap. Foiled again. Here it is with spaces & ge ; and & lt ;

ADD REPLY
0
Entering edit mode

So basically you are using HTML markup to set your subscripts and whatnot, and then element_markdown does the rest.

ADD REPLY
0
Entering edit mode

Thank you very much - that worked perfectly! Word of warning to those who try this: it screws up the other legends because of the markdown interpretation so I had to change those to the HTML notation as well.

ADD REPLY

Login before adding your answer.

Traffic: 669 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6