1
plot(x,y,type="h",...)
text(x,y,labels=columnL)

This seems very simple: I have a (histogram) plot and use the text function to label the points.

In the data table, I have rows containing the x, y and label names (columnL, each point has a different name). The table is imported via read.table from a .csv file.

Everything works fine, BUT: I want to use subscript and superscript in the labels, and all solutions I found were for axis labels - here, I have a range of different labels, all of which need to be formatted correctly, so I was not successful when trying paste() and expression() in the text(labels=) (returned the same label for all points)... So, more generally: is it possible to have formatting options in these labels?

I am completely new to R (and programming), sorry if this is unclear or seems stupid. There is probably a way to "batch handle" the content of ColumnL somehow. I could add the formatting ([] and ^) to the .csv file ahead of time (but because it is not read as expression the output has no formatting).

UPDATED Edit: This almost works:

mytable <- read.table("C:/Data/Example.txt", header= T, sep= "\t")
attach(mytable)

ColL <- as.character(ColumnL)
ColN <- parse(text=ColL)

plot(x1,y1)
text(x1,y1+0.1,labels=ColN)

where Example.txt contains:

x1  y1  ColumnL
1   2   a[1]^"+"
1   3   b[19]
2   5   c[27]
4   6   v[45]

What's good about this is that the subscript works for the numbers (and I don't mind having the formatting defined in the input file). However, the one thing that I can't fix is that the "+", regardless of quotes, triggers a concatenation of rows 1 and 2 when ColL is run through parse(). How can I properly use + here? It is supposed to be superscript...

Edit: Note that the problem only occurs when "+" is the last (non-space) character of the entry (or when followed by [), otherwise it works fine (apparently "+" is only supposed to be in equations and there you wouldn't expect it at the end, but that's where I need it)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
MHelf
  • 11
  • 4
  • 1
    plenty of examples under `?plotmath` – rawr Jun 01 '15 at 20:30
  • It would help to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and show the code you actually tried. Maybe [this question](http://stackoverflow.com/questions/30540028/greek-letters-in-legend-in-r/30540088#comment49155191_30540088) will help. – MrFlick Jun 01 '15 at 20:33
  • Thanks MrFlick, I'll look into the other question when I have a moment, for now I have added a better example for illustration... – MHelf Jun 01 '15 at 21:15

2 Answers2

0

Now that I looked at this https://stat.ethz.ch/pipermail/r-help/1999-July/004414.html, I understood that + and - can be put to the end of a string when there is an argument added. in the words from the link:

the "-" operator needs at least one argument, but that one can be empty

Same goes for +, so the input table can contain all the formatting like this:

x1  y1  ColumnL
1   2   a[11]^{+{}}
1   3   b[19]
2   5   c[27]
4   6   v[45]

and will work with the code from the updated edit from above.

I think this is particularly interesting for chemists to label points in a plot with ion charges and sum formulas.

MHelf
  • 11
  • 4
0

Using

read.table("C:/Data/Example.txt", header= T, sep= "\t", stringsAsFactors=F, quote="")

will allow you use to use the input file with the quote (i.e. a[1]^"+"). The stringsAsFactors=F will eliminate the use of as.character() in your code.

fishtank
  • 3,718
  • 1
  • 14
  • 16