我正在尝试更新ggplot
要使用的图例标签plotmath
但是,当我这样做时,它将之前组合的图例分成两部分。通过一个例子可能更容易理解:
# test data and the default plot gives the correct colours and linetypes
# in the legend but the labels are not parsed (as have not tried to yet!)
test = data.frame(x = 1:10, y = 1:10, grp=factor(rep(c("g1", "g2"),each=5)))
ggplot(test, aes(y=y, x=x, linetype=grp, colour=grp)) +
geom_line()
我想要的是保留上图的图例键,但更改图例标签。我已经尝试过以下内容,现在解析plotmath
但将图例键分开。
ggplot(test, aes(y=y, x=x, linetype=grp, colour=grp)) +
geom_line() +
scale_linetype_discrete(breaks=c("g1", "g2"), labels = parse(text=c("g[1]", "g[2]"))) +
scale_colour_discrete(breaks=c("g1", "g2"), labels = parse(text=c("g[1]", "g[2]")))
# same as before
ggplot(test, aes(y=y, x=x, linetype=grp, colour=grp)) +
geom_line() +
scale_linetype_manual(values=1:2, breaks=c("g1", "g2"), labels = parse(text=c("g[1]", "g[2]"))) +
scale_colour_manual(values=1:2, breaks=c("g1", "g2"), labels = parse(text=c("g[1]", "g[2]")))
如何保留基本图的键但更新标签plotmath
请问(不改变原始数据)?