【问题标题】:Display Values in R Plot在 R 图中显示值
【发布时间】:2010-11-29 18:34:24
【问题描述】:

如何在plot 本身中呈现plot 中的点值?

谢谢。

【问题讨论】:

    标签: r plot


    【解决方案1】:

    使用text():

    plot(1:10, 1:10) 
    text(5, 5, "Foo")
    

    有关放置文本的选项,请参阅help(text)。该函数是矢量化的,因此您可以 也做一些类似于

     text(1:10, 1:10, LETTERS[1:10])
    

    如果你有文本和位置的向量。

    【讨论】:

      【解决方案2】:
      b0 = 2.5; b1 = 2
      n = 100
      x = rnorm(n, 20, 15)
      y = b0 + b1*x + rnorm(n, 0, 15)
      plot(x, y)
      plot(x, y, type='n')
      text(x, y, round(y, 2), cex=0.45)
      text(x, y, round(y, 2), cex=0.8)
      text(x, y, paste(round(x, 2), round(y, 2), sep=", "), cex=0.8) # for (x, y), but this gets cluttered. 
      

      使用cex 作为字符大小(请参阅文本帮助)。并使用plot(x, y, type='n') 正确设置您的窗口,而无需实际绘制任何内容。

      【讨论】:

      • 次要问题:您在第 5 行有一个错字(策略而不是情节);而且我很想使用 format 或 formatC 而不是 round,因为你想要一个返回不是数字的字符串。尝试,例如 format(y, trim=TRUE, digits=2)
      • 谢谢里奇。我考虑了格式,但我认为round() 的功能对初学者来说会更明显。并且 paste() 将转换为字符串。
      【解决方案3】:

      类似于 Vince 的回答,除了使用 ggplot2:

      b0 = 2.5; b1 = 2
      n = 20
      x = rnorm(n, 20, 15)
      y = b0 + b1*x + rnorm(n, 0, 15)
      dat<-data.frame(x,y)
      library(ggplot2)
      ggplot(data=dat)+geom_text(aes(x=x,y=y),size=4,label=paste(round(x, 2), round(y, 2), sep=", "))
      

      可以通过改变 size 参数来改变字符大小。

      【讨论】:

      • 我应该更多地研究 ggplot。它似乎越来越受欢迎。我一直在用格子做硬的东西。
      【解决方案4】:
      x <- 1/3
      plot(1,type="none",ann=FALSE)
      ## text and values only
      text(mean(par("usr")[1:2]),mean(par("usr")[3:4])-par("cxy")[2]*2,
           paste("z = ",round(x,2)))
      ## text, values, and mathematical expressions
      text(mean(par("usr")[1:2]),mean(par("usr")[3:4]),
           bquote(x^2==.(round(x,2))))
      text(mean(par("usr")[1:2]),mean(par("usr")[3:4])-par("cxy")[2],
           substitute(gamma==value,list(value=round(x,2))))
      

      【讨论】:

        【解决方案5】:

        使用 ggplot2,您可以添加点和标签。将 aes() 放入 ggplot() 的好处是该 aes() 将成为所有几何图形的默认值。因此,在这种情况下,您只需指定一次 x 和值,但 geom_point() 和 geom_text() 都使用它们

        Ian Fellows 修改后的代码如下所示:

        b0 <- 2.5
        b1 <- 2
        n <- 20
        dat <- data.frame(x = rnorm(n, 20, 15))
        dat$y <- b0 + b1*dat$x + rnorm(n, 0, 15)
        dat$text <- with(dat, paste(round(x, 2), round(y, 2), sep=", "))
        library(ggplot2)
        ggplot(data=dat, aes(x = x, y = y, label = text)) + geom_point() + geom_text(size=4, hjust = 1, vjust = 1)
        

        【讨论】:

          【解决方案6】:

          也许这也有帮助

          # example data
          dat <- data.frame(name = sample(letters[1:4],20, replace=T), x1 = rnorm(20,2), x2 = 42+x1*rnorm(20,0,2))
          # plot the data
          plot(dat$x1,dat$x2)
          # use identify to print name for each 'dot' that you click with left mouse
          identify(dat$x1,dat$x2,labels=name)
          # When done identifying point, click with right mousebutton.
          

          我喜欢这个用于交互目的的功能。虽然不知道如何在 ggplot 中实现这一点

          【讨论】:

            【解决方案7】:

            我发现了这个方法,非常有用:

            for i=1:6
            text(x(i),y(i),num2str(y(i)));
            end
            

            对图中的每个成员重复此行。

            【讨论】:

              猜你喜欢
              • 2021-03-09
              • 1970-01-01
              • 1970-01-01
              • 2013-10-12
              • 1970-01-01
              • 2017-06-14
              • 1970-01-01
              • 2019-06-12
              • 1970-01-01
              相关资源
              最近更新 更多