gpt4 book ai didi

r - 带有 bquote 函数的 geom_text/geom_label

转载 作者:行者123 更新时间:2023-12-04 02:53:53 25 4
gpt4 key购买 nike

我的数据:

dat <- data_frame(x = c(1,2,3,4,5,6), y = c(2,2,2,6,2,2))

我想在点 (x=4,y=6) 旁边显示这个表达式:

expression <- bquote(paste(frac(a[z], b[z]), " = ", .(dat[which.max(dat$y),"y"] %>% as.numeric())))

但是,当我将这个表达式与 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))

我收到此错误消息:

Error: Aesthetics must be either length 1 or the same as the data (1): label

最佳答案

您可以使用以下代码(保留您对数据和表达式的定义):

与您的问题无关,但是:在 ggplot 调用中定义 aesthetics 并在后续函数调用中重用它总是更好。如果需要,您可以覆盖定义,就像下面在 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)

hjustnudge_x 用于相对于点定位标签。人们可能会争辩说也可以使用 nudge_y 来获得图片中的整个标签。

产生这个情节:

enter image description here

请告诉我这是否是您想要的。

关于r - 带有 bquote 函数的 geom_text/geom_label,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53866110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com