Lou*_*lou 3 r ggplot2 geom-text
我的数据:
dat <- data_frame(x = c(1,2,3,4,5,6), y = c(2,2,2,6,2,2))
Run Code Online (Sandbox Code Playgroud)
我希望在点 (x=4,y=6) 旁边显示此表达式:
expression <- bquote(paste(frac(a[z], b[z]), " = ", .(dat[which.max(dat$y),"y"] %>% as.numeric())))
Run Code Online (Sandbox Code Playgroud)
但是,当我将此表达式与 ggplot 一起使用时:
ggplot() +
geom_point(data = dat, aes(x = x, y = y)) +
geom_label(data = dat[which.max(dat$y),], aes(x = x, y = y, label = expression))
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息:
Error: Aesthetics must be either length 1 or the same as the data (1): label
Run Code Online (Sandbox Code Playgroud)
您可以使用以下代码(保留数据和表达式的定义):
与您的问题无关,但是:aesthetics在ggplot-call 中定义并在后续函数调用中重用它总是更好。如果需要,您可以覆盖定义,如下所示geom_label
ggplot(data = dat, aes(x = x, y = y)) +
geom_point() +
geom_label(data = dat[4,], label = deparse(expression), parse = TRUE,
hjust = 0, nudge_x = .1)
Run Code Online (Sandbox Code Playgroud)
hjust和nudge_x用于相对于点定位标签。人们可能会争辩说使用nudge_y以及获取图片中的整个标签。
产生这个情节:
请让我知道这是否是您想要的。
| 归档时间: |
|
| 查看次数: |
635 次 |
| 最近记录: |