I'm using ggplot to do some multiline plots that are constructed with lots of variables and the use of paste. I have not been able to figure out how to get the subscript 3 in O3 to appear in the following simplified version of the code.
我用ggplot来做一些多行图这些多行图是用很多变量和粘贴构造的。我还没有弄明白如何使O3中的下标3出现在下面的简化版本的代码中。
gasSubscript <- "O[3]"
color1 <- paste(gasSubscript,"some additional text")
df <- data.frame(x = c(1,2,3,4,5,6,7,8,9,10), y = c(10,9,8,7,6,5,4,3,2,1))
testPlot <- ggplot(data = df, aes(x = x)) + geom_line(aes(y = y, color = color1))
color1 contains
color1包含
"O[3] some additional text"
The legend displays as "O[3] some additional text" rather than with a subscripted 3.
图例显示为“O[3]一些附加文本”,而不是使用下标3。
2 个解决方案
#1
3
The problem is that you need the label in the scale to be an expression so that, when it is rendered, it is rendered according to the rules of plotmath
. However, ggplot
works with data.frame
s and data.frame
s can not have a column which is a vector of expressions. So the way around this is to store the information as the text (string) version of the plotmath
expression and, as the last step for making the labels, turn these into expressions. This can be done because the labels
argument to the scale functions can itself be a function which can transform/format the labels.
问题是,您需要在scale中标记为一个表达式,以便在呈现时根据plotmath规则呈现它。但是,ggplot使用数据。帧和数据。帧不能有一个列,它是一个表达式的向量。因此,解决这个问题的方法是将信息存储为plotmath表达式的文本(字符串)版本,作为创建标签的最后一步,将它们转换为表达式。这可以实现,因为scale函数的标签参数本身可以是一个函数,它可以转换/格式化标签。
Putting this together with your example:
结合你的例子:
color1 <- paste(gasSubscript,"*\" some additional text\"")
This is now in a format that can be made into an expression.
这是一种可以被做成表达式的格式。
> color1
[1] "O[3] *\" some additional text\""
> cat(color1)
O[3] *" some additional text"
> parse(text=color1)
expression(O[3] *" some additional text")
With that format, you can force the scale to interpret the labels as expressions which will cause them to be rendered as per the rules of plotmath
.
使用这种格式,您可以强制scale将标签解释为表达式,从而使它们按照plotmath规则进行呈现。
testPlot <- ggplot(data = df, aes(x = x)) +
geom_line(aes(y = y, color = color1)) +
scale_colour_discrete(labels = function(x) parse(text=x))
Using the labels
function approach works for data which is stored in the data.frame
as well, so long as the strings are formatted so that they can be parsed.
使用标签函数方法也适用于存储在data.frame中的数据,只要对字符串进行格式化,以便对其进行解析。
DF <- data.frame(x=1:4, y=rep(1:2, times=2),
group = rep(c('O[3]*" some additional text"',
'H[2]*" some different text"'), each = 2))
ggplot(DF, aes(x, y, colour=group)) +
geom_line() +
scale_colour_discrete(labels=function(x) parse(text=x))
#2
0
This should do what I think you want. It took me a little tinkering to get the right order of paste and expression.
这应该是我认为你想要的。我花了一些修修补补的时间才弄清楚粘贴和表达的顺序。
require(ggplot2)
test <- data.frame(x = c(1,2,3,4,5,6,7,8,9,10), y = c(10,9,8,7,6,5,4,3,2,1))
colour1 <- "1"
testPlot <- ggplot(data = test, aes(x = x)) + geom_line(aes(y = y, colour = colour1))
testPlot + scale_colour_discrete(labels = c(expression(paste(O[3], "some other text here"))))
It also returns the warning
它还返回警告
Warning message:
In is.na(scale$labels) :
is.na() applied to non-(list or vector) of type 'expression'
to which I haven't been able to find an explanation.
我还没能找到一个解释。
#1
3
The problem is that you need the label in the scale to be an expression so that, when it is rendered, it is rendered according to the rules of plotmath
. However, ggplot
works with data.frame
s and data.frame
s can not have a column which is a vector of expressions. So the way around this is to store the information as the text (string) version of the plotmath
expression and, as the last step for making the labels, turn these into expressions. This can be done because the labels
argument to the scale functions can itself be a function which can transform/format the labels.
问题是,您需要在scale中标记为一个表达式,以便在呈现时根据plotmath规则呈现它。但是,ggplot使用数据。帧和数据。帧不能有一个列,它是一个表达式的向量。因此,解决这个问题的方法是将信息存储为plotmath表达式的文本(字符串)版本,作为创建标签的最后一步,将它们转换为表达式。这可以实现,因为scale函数的标签参数本身可以是一个函数,它可以转换/格式化标签。
Putting this together with your example:
结合你的例子:
color1 <- paste(gasSubscript,"*\" some additional text\"")
This is now in a format that can be made into an expression.
这是一种可以被做成表达式的格式。
> color1
[1] "O[3] *\" some additional text\""
> cat(color1)
O[3] *" some additional text"
> parse(text=color1)
expression(O[3] *" some additional text")
With that format, you can force the scale to interpret the labels as expressions which will cause them to be rendered as per the rules of plotmath
.
使用这种格式,您可以强制scale将标签解释为表达式,从而使它们按照plotmath规则进行呈现。
testPlot <- ggplot(data = df, aes(x = x)) +
geom_line(aes(y = y, color = color1)) +
scale_colour_discrete(labels = function(x) parse(text=x))
Using the labels
function approach works for data which is stored in the data.frame
as well, so long as the strings are formatted so that they can be parsed.
使用标签函数方法也适用于存储在data.frame中的数据,只要对字符串进行格式化,以便对其进行解析。
DF <- data.frame(x=1:4, y=rep(1:2, times=2),
group = rep(c('O[3]*" some additional text"',
'H[2]*" some different text"'), each = 2))
ggplot(DF, aes(x, y, colour=group)) +
geom_line() +
scale_colour_discrete(labels=function(x) parse(text=x))
#2
0
This should do what I think you want. It took me a little tinkering to get the right order of paste and expression.
这应该是我认为你想要的。我花了一些修修补补的时间才弄清楚粘贴和表达的顺序。
require(ggplot2)
test <- data.frame(x = c(1,2,3,4,5,6,7,8,9,10), y = c(10,9,8,7,6,5,4,3,2,1))
colour1 <- "1"
testPlot <- ggplot(data = test, aes(x = x)) + geom_line(aes(y = y, colour = colour1))
testPlot + scale_colour_discrete(labels = c(expression(paste(O[3], "some other text here"))))
It also returns the warning
它还返回警告
Warning message:
In is.na(scale$labels) :
is.na() applied to non-(list or vector) of type 'expression'
to which I haven't been able to find an explanation.
我还没能找到一个解释。