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)

4

2 回答 2

0

现在我查看了这个https://stat.ethz.ch/pipermail/r-help/1999-July/004414.html,我明白了,+并且-可以在添加参数时将其放在字符串的末尾。在链接中的话:

"-" 运算符至少需要一个参数,但该参数可以为空

也是如此+,因此输入表可以包含所有格式,如下所示:

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

并将使用上面更新的编辑中的代码。

我认为对于化学家来说,用离子电荷和求和公式在图中标记点特别有趣。

于 2015-06-02T15:24:35.290 回答
0

使用

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

将允许您使用带引号的输入文件(即a[1]^"+")。这stringsAsFactors=F将消除as.character()在您的代码中的使用。

于 2015-06-02T17:09:31.347 回答