英文:
Display position of a y-maximum using ggplot
问题
I used ggplot
to create a plot:
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)
。
英文:
I used ggplot
to create a plot:
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)
The red line tells me where the maximum of BS
is.
Is there a way to display the x-position and y-position of this value?
It should be something like (for example): P(0.5, 0.8)
.
答案1
得分: 2
使用geom_text()
函数在ggplot
中添加文本注释:
geom_text(
x = df$xval[as.numeric(df$BS) == max(as.numeric(df$BS))],
y = max(as.numeric(df$BS)),
label = paste0("P(", round(df$xval[as.numeric(df$BS) == max(as.numeric(df$BS))], 2), ", ", round(max(as.numeric(df$BS)), 2), ")"),
vjust = -1.5,
color = "black"
)
使用paste0()
函数将位置信息连接成所需的格式(例如,"P(0.5, 0.8)")。
根据需要调整vjust
参数以微调文本的垂直位置。
英文:
Add a text annotation using the geom_text()
function in 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) +
geom_text(
x = df$xval[as.numeric(df$BS) == max(as.numeric(df$BS))],
y = max(as.numeric(df$BS)),
label = paste0("P(", round(df$xval[as.numeric(df$BS) == max(as.numeric(df$BS))], 2), ", ", round(max(as.numeric(df$BS)), 2), ")"),
vjust = -1.5,
color = "black"
)
The paste0()
function is used to concatenate the position information in the desired format (e.g., "P(0.5, 0.8)").
Adjust the vjust
parameter as needed to fine-tune the vertical positioning of the text.
答案2
得分: 2
你可以在筛选数据后使用 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)
英文:
You can use geom_label()
here after filtering your data. Here using the mtcars
dataset as an example:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论