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

huangapple go评论73阅读模式
英文:

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)

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

英文:

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)

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

huangapple
  • 本文由 发表于 2023年6月29日 20:15:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定