问题描述
我试图在一些x轴值上添加上标,以便连接到位于页面底部的脚注.简单的解决方法只是将星号而不是^a
,但这不适用于我的目的.
I'm trying to add a superscript to some x-axis values in order to connect to a footnote that'll be at the bottom of the page. The easy workaround would just be an asterisk instead of ^a
but that won't work for my purposes.
我做了很多搜索,尽管有很多关于轴标签上标的文章,但我找不到关于轴值上标的任何文章.他们中的大多数人似乎都在中间加了gg + labs(x = expression("blah^a"))
.
I did a lot of searching and while there's plenty of posts about superscripts in axis labels, I couldn't find any about superscripts in axis values. Most of them appeared to centera round adding a gg + labs(x = expression("blah^a"))
.
我确实找到了这篇有关解析内部上标的帖子geom_text()
,但对于geom_bar()
来说似乎不起作用.
I did find this post about parsing superscripts inside a geom_text()
but it appears the same doesn't work for a geom_bar()
.
以下是一些测试数据:
library(ggplot2)
dat <- data.frame(x = c("alpha", "bravo^a"),
y = c(10, 8))
ggplot(data = dat) +
geom_bar(aes(x = x,
y = y),
stat = "identity")
推荐答案
您只需要parse
scale_x_discrete
添加geom_text
示例
library(ggplot2)
dat <- data.frame(x = c("alpha", "bravo^a"),
y = c(10, 8))
ggplot(data = dat) +
geom_bar(aes(x = x,
y = y),
stat = "identity") +
scale_x_discrete(labels = parse(text = levels(dat$x))) +
geom_text(aes(x = x, y = y,
label = x),
parse = TRUE,
nudge_y = 1,
size = 5) +
theme_minimal(base_size = 14)
由 reprex软件包(v0.2.0.9000)创建.
Created on 2018-08-27 by the reprex package (v0.2.0.9000).
这篇关于在geom_bar上解析离散轴值中的上标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!