r - 使用 ggplot 显示 y 最大值的位置

标签 r ggplot2 position vline

我使用ggplot来创建一个绘图:

pl <- ggplot(df, aes(x=xval, y=as.numeric(BS))) + 
    geom_point(shape=21) + 
    xlab("Threshold") + 
    ylab("BS") + 
    geom_vline(xintercept=df$xval[as.numeric(df$BS) == max(as.numeric(df$BS))],color='red') +
    geom_hline(yintercept=max(as.numeric(df$BS)),color='darkblue',linetype=2)

红线告诉我 BS 的最大值在哪里。

有没有办法显示这个值的x位置和y位置?

它应该类似于(例如):P(0.5, 0.8)

最佳答案

过滤数据后,您可以在此处使用geom_label()。这里以mtcars数据集为例:

ggplot(mtcars, aes(x = disp, y = qsec)) +
  geom_point(shape = 21) + 
  geom_vline(xintercept = mtcars$disp[as.numeric(mtcars$qsec) == max(as.numeric(mtcars$qsec))],color='red') +
  geom_hline(yintercept = max(as.numeric(mtcars$qsec)), color = 'darkblue', linetype = 2) +
  geom_label(data = . %>% filter(qsec == max(qsec)), 
             aes(label = paste0("P(", disp, ", ", qsec, ")")), hjust = -0.5)

enter image description here

关于r - 使用 ggplot 显示 y 最大值的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76580959/

相关文章:

r - 误差线在 R 中看起来很大,但在 Excel 中则不然

r - 抑制R输出

html - 窗口调整大小时的动画定位

html - 页脚在上方,当我将浮点值设置为正确时

r - 单个数据帧中的重叠范围

r - 如何使用 Rayshader R 导出高分辨率图像

r - 使用带有 scale_y_log10 的 geom_histogram 时如何抑制零

r - 在 ggplot/R 中使用位置参数排斥文本

r - 使用scale = "free_y"时如何设置相等的面板水平空间?

html - 使用 CSS/HTML 正确定位 2 个并排图像的方法?