Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using plotmath in variable names #20

Closed
sinclairjesse opened this issue Jan 21, 2016 · 17 comments
Closed

Using plotmath in variable names #20

sinclairjesse opened this issue Jan 21, 2016 · 17 comments
Assignees

Comments

@sinclairjesse
Copy link

Is there a way of changing the variables names such that plotmath expressions could be used? I've tried adding a labels argument with a vector of new labels thinking that it may be passed through to text, but that didn't seem to work.

@vsimko
Copy link
Collaborator

vsimko commented Mar 15, 2016

Could you elaborate more on this ?
Or provide some example code-snippets, how it should look like.

@vsimko
Copy link
Collaborator

vsimko commented Mar 23, 2016

In the meantime, I found this stackoverflow question: http://stackoverflow.com/questions/35965433/r-corrplot-change-data-labels/36190501

@sinclairjesse
Copy link
Author

Ideally, I would be able to use plotmath expressions (e.g., use of subscripts or superscripts) as a variable name, similar to using an expression in the axis labels in base graphics. For example:

plot(y ~ x, xlab = expression("Test"["A"]), ylab = expression("Test"^"B")).

Based on your answer in the stackoverflow question, colnames/rownames are used in the plotting. Something like:

mat <- colnames(expression("Test"["A"]), expression("Test"^"B"))

won't work.

@vsimko
Copy link
Collaborator

vsimko commented Apr 27, 2016

Now I understand.
I tried to implement plotmath for row/column labels but then I realized that I need some input from @taiyun - which approach is better ?

The problem is that colnames() and rownames() functions always convert their input to character even if the variable names are encoded using expression.

Approach 1: colnames always parsed as expressions

  • Pros: simple to use
  • Cons: variable names such as "alpha" will always be rendered as greek symbol alpha
M <- cor(mtcars)[1:5,1:5]
colnames(M) <- c("alpha", "beta", "alpha+beta", "a[0]", "a[beta]")
corrplot(M)

image

Approach 2: new parameters xlab & ylab for the corrplot() function

  • Pros: control over plotmath expressions
  • Cons: two ways for variable names specification. (1) dimnames from the original matrix, (2) xlab, ylab parameters which overrides colnames/rownames values.
corrplot(M, xlab = c("alpha", "beta",
                     expression(alpha+beta),
                     expression(a[0]),
                     expression(a[beta])))

image

Approach 3: similar to Approach 1 but using some prefix for expressions

Pros: simple to use
Cons: introducing special notation EXPR:...

M <- cor(mtcars)[1:5,1:5]
colnames(M) <- c("alpha", "beta", "EXPR:alpha+beta", "EXPR:a[0]", "EXPR:a[beta]")
corrplot(M)

image

@vsimko vsimko self-assigned this Apr 27, 2016
@taiyun
Copy link
Owner

taiyun commented Apr 27, 2016

Thanks.
I prefer approach 3.

@vsimko
Copy link
Collaborator

vsimko commented Apr 27, 2016

OK, if I use Approach 3, what would be the best prefix:

  • EXPR:
  • MATH:
  • E:
  • or something else ?

@taiyun
Copy link
Owner

taiyun commented Apr 27, 2016

just:? eg ":beta"

@vsimko
Copy link
Collaborator

vsimko commented Apr 27, 2016

Other options would be:

  • $ used in latex as $math expression$
  • = used in spreadsheets for formulas

@taiyun
Copy link
Owner

taiyun commented Apr 27, 2016

I think anyone in $, : or = is OK.

@vsimko vsimko changed the title Change variable names in corrplot using plotmath in variable names Apr 27, 2016
vsimko added a commit to vsimko/corrplot that referenced this issue Apr 27, 2016
vsimko added a commit that referenced this issue Apr 27, 2016
fixed issue #20 using plotmath in variable names
@vsimko vsimko closed this as completed Apr 27, 2016
@sinclairjesse
Copy link
Author

Good solution! Thanks

@vsimko vsimko changed the title using plotmath in variable names Using plotmath in variable names Aug 28, 2017
@jonnez
Copy link

jonnez commented Oct 5, 2017

How do I now create a column or row name that contains just a $?

I currently get the following error (in version 0.84):

Error in ifelse(grepl("^[:=$]", s), parse(text = substring(s, 2)), s) :
  unimplemented type 'expression' in 'length<-'

@vsimko vsimko reopened this Oct 5, 2017
@vsimko
Copy link
Collaborator

vsimko commented Oct 5, 2017

just use " $", e.g.

M <- cor(mtcars)
rownames(M)[1] <- " $"
corrplot(M)

image

@vsimko vsimko closed this as completed Oct 5, 2017
vsimko added a commit to vsimko/corrplot that referenced this issue Oct 5, 2017
@jonnez
Copy link

jonnez commented Oct 5, 2017 via email

@gezza1232
Copy link

Hey guys! I need to write MIP-1B as a column label, where B is a beta. I can't seem to be able to merge the MIP-1 with the :beta in order to get this to look right. Do you have any ideas on how to do that?

@vsimko
Copy link
Collaborator

vsimko commented Sep 4, 2018

what about this:

library(corrplot)
M <- cor(mtcars)
rownames(M)[1] <- ":MIP-1*beta"
corrplot(M)

image

@gezza1232
Copy link

It worked! Thanks

@Raed-Hamed
Copy link

Hi,

I believe your solution doesn't work whenever you want to have more than one subscript associated with a variable. Is that true and is there a way to fix this?

Thanks

e.g.

M <- cor(mtcars)[1:5,1:5]
colnames(M) <- c("alpha", "beta", ":alpha+beta", ":a[1,2]", ":a[beta]")
corrplot::corrplot(M)

Also the following doesn't work:
colnames(M) <- c("alpha", "beta", ":alpha+beta", ":a"["1,2"], ":a[beta]")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants