我正在尝试更新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更新标签(而不更改原始数据)?

发布于 2020-05-12 00:36:12
这与labels = c(~g[1],~g[2])一起工作
ggplot(test, aes(y=y, x=x, linetype=grp, colour=grp)) +
geom_line() +
scale_linetype_discrete(breaks=c("g1", "g2"),
labels = c(~g[1],~g[2])) +
scale_colour_discrete(breaks=c("g1", "g2"),
labels = c(~g[1],~g[2]))https://stackoverflow.com/questions/61734040
复制相似问题