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

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

Display position of a y-maximum using ggplot

问题

I used ggplot to create a plot:

  1. pl <- ggplot(df, aes(x=xval, y=as.numeric(BS))) +
  2. geom_point(shape=21) +
  3. xlab("Threshold") +
  4. ylab("BS") +
  5. geom_vline(xintercept=df$xval[as.numeric(df$BS) == max(as.numeric(df$BS))],color='red') +
  6. 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:

  1. pl <- ggplot(df, aes(x=xval, y=as.numeric(BS))) +
  2. geom_point(shape=21) +
  3. xlab("Threshold") +
  4. ylab("BS") +
  5. geom_vline(xintercept=df$xval[as.numeric(df$BS) == max(as.numeric(df$BS))],color='red') +
  6. 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中添加文本注释:

  1. geom_text(
  2. x = df$xval[as.numeric(df$BS) == max(as.numeric(df$BS))],
  3. y = max(as.numeric(df$BS)),
  4. label = paste0("P(", round(df$xval[as.numeric(df$BS) == max(as.numeric(df$BS))], 2), ", ", round(max(as.numeric(df$BS)), 2), ")"),
  5. vjust = -1.5,
  6. color = "black"
  7. )

使用paste0()函数将位置信息连接成所需的格式(例如,"P(0.5, 0.8)")。

根据需要调整vjust参数以微调文本的垂直位置。

英文:

Add a text annotation using the geom_text() function in ggplot:

  1. pl <- ggplot(df, aes(x = xval, y = as.numeric(BS))) +
  2. geom_point(shape = 21) +
  3. xlab("Threshold") +
  4. ylab("BS") +
  5. geom_vline(xintercept = df$xval[as.numeric(df$BS) == max(as.numeric(df$BS))], color = 'red') +
  6. geom_hline(yintercept = max(as.numeric(df$BS)), color = 'darkblue', linetype = 2) +
  7. geom_text(
  8. x = df$xval[as.numeric(df$BS) == max(as.numeric(df$BS))],
  9. y = max(as.numeric(df$BS)),
  10. label = paste0("P(", round(df$xval[as.numeric(df$BS) == max(as.numeric(df$BS))], 2), ", ", round(max(as.numeric(df$BS)), 2), ")"),
  11. vjust = -1.5,
  12. color = "black"
  13. )

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 数据集的示例:

  1. ggplot(mtcars, aes(x = disp, y = qsec)) +
  2. geom_point(shape = 21) +
  3. geom_vline(xintercept = mtcars$disp[as.numeric(mtcars$qsec) == max(as.numeric(mtcars$qsec))], color='red') +
  4. geom_hline(yintercept = max(as.numeric(mtcars$qsec)), color = 'darkblue', linetype = 2) +
  5. geom_label(data = . %>% filter(qsec == max(qsec)),
  6. 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:

  1. ggplot(mtcars, aes(x = disp, y = qsec)) +
  2. geom_point(shape = 21) +
  3. geom_vline(xintercept = mtcars$disp[as.numeric(mtcars$qsec) == max(as.numeric(mtcars$qsec))],color='red') +
  4. geom_hline(yintercept = max(as.numeric(mtcars$qsec)), color = 'darkblue', linetype = 2) +
  5. geom_label(data = . %>% filter(qsec == max(qsec)),
  6. 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:

确定