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