【问题标题】:R line chart with numbers on the graph [closed]图表上带有数字的 R 折线图 [关闭]
【发布时间】:2014-10-11 17:28:41
【问题描述】:

我需要在 R 中绘制一些数字向量,但我想查看图表上的数字。例如,假设用折线图绘制函数。我想直接在图表上可视化与函数的最大值/最小值对应的数字。它是否可行以及如何?谢谢。

【问题讨论】:

  • 一般来说,是的,你可以这样做,但你需要给出一个更具体的例子来说明你的输入是什么以及完成的图表应该是什么样子。
  • 一般来说,对 SO 的期望是您向我们展示您的尝试。请参阅:how-to-make-a-great-r-reproducible-example 以了解对恰当问题的描述。
  • 希望问题在搜索 Internet 和 SO 时会做出某种努力。

标签: r plot ggplot2 lattice linechart


【解决方案1】:

这里是一个使用 R 基础图的示例:

可重现的例子

## you should set the seed with random data
set.seed(1)
x <- rnorm(100)

绘制您的数据

## note the use of extrandrange to be sure that we have enough spaces 
## to plot and show your extrema
plot(x,type='l',ylim=extendrange(x))

提取极值

## wich.max extreact the index of the extrema 
xm = c(which.max(x),which.min(x))
ym <- c(max(x),min(x))

绘制极值

## you should round the data for pretty formatting
text(xm,ym,label=round(ym,2),col='red',adj=c(-0.5))
points(xm,ym,col='green',pch=20,cex=2)

【讨论】:

  • 支持您的回答是结构良好的答案的好例子,但由于标签过多,投票结束时过于广泛。如果提问者只会做一些搜索,那么已经有很多 R 图形教程。
  • @BondedDust 我完全同意你的看法。我正在等待 OP 改进他的问题,否则我也会投票关闭它。