2016-09-28 182 views
1

标题说明了一切:我想在ggplot中的垂直线的标签上使用上标。 这里是一个不太漂亮的例子:R:在geom_vline标签上标

df <- data.frame(x = c(1:10),y = c(2,2,3,4,5,5,6,7,5,4)) 

ggplot(data=df, aes(x,y, group=1)) + 
    geom_line() + 
    scale_x_reverse() + 
    geom_vline(xintercept=3) + 
    geom_text(aes(x=3, label=paste("3400","cm","^-1", sep=""), y=5), angle=90, vjust = 1.2) 

我想-1上标。有很多解决方案都可以在绘图轴标签上做到这一点,但是它们都不在这里工作。有人可以帮忙吗?

回答

3

你太亲近了!您只需在geom_text中设置parse=TRUE并使用?plotmath语法。

df <- data.frame(x = c(1:10),y = c(2,2,3,4,5,5,6,7,5,4)) 

library(ggplot2) 
ggplot(data=df, aes(x,y, group=1)) + 
    geom_line() + 
    scale_x_reverse() + 
    geom_vline(xintercept=3) + 
    #geom_text(data = data.frame(x = 3, y = 5), label = paste("paste(3400, cm)","^-1", sep=""), 
    # angle=90, parse = TRUE, vjust = 1.2) + 
    annotate("text", x = 3, y = 5, angle = 90, label = paste("paste(3400, cm)","^-1", sep=""), 
    vjust = 1.2, parse = TRUE) 

结果:

enter image description here

另外,注意文字较少模糊比你的代码。这是因为你的代码实际上是在相同的坐标下打印标签10次。您需要使geom_text使用不同的数据或更好的使用annotate

+0

也可以使用:https://github.com/hadley/ggplot2/wiki/Plotmath –

+0

@Chrisss非常感谢! – Anca

+0

我会看看情节,谢谢你的建议! – Anca