R- plot numbers instead of points

后端 未结 2 1492
长情又很酷
长情又很酷 2020-12-16 12:50

I have successfully made a scatterplot, with different symbols for each data series. But what I want to do is make the same scatterplot with the point to show up as numbers

相关标签:
2条回答
  • 2020-12-16 13:17

    You can use text. Using @HongOoi data:

    dat <- data.frame(x=rnorm(100), y1=rnorm(100)-1, y2=rnorm(100), y3=rnorm(100)+1)
    plot(y1 ~ x, data=dat, type='n', ylim=c(-4, 4))     
    text(dat$x,dat$y1,label=0,col='blue')
    text(dat$x,dat$y2,label=1,col='green')
    text(dat$x,dat$y3,label=2,,col='red')
    

    enter image description here

    0 讨论(0)
  • 2020-12-16 13:20

    Sure, just pass the pch parameter as a character.

    dat <- data.frame(x=rnorm(100), y1=rnorm(100)-1, y2=rnorm(100), y3=rnorm(100)+1)
    plot(y1 ~ x, data=dat, pch="0", ylim=c(-4, 4))
    points(y2 ~ x, data=dat, pch="3")
    points(y3 ~ x, data=dat, pch="6")
    

    ETA: one nice thing is that the pch parameter, like many base graphics parameters, is vectorised. So you can do something like this (which also works for Agstudy's answer).

    dat <- data.frame(x=rnorm(300), y=rnorm(300) + c(0,3,6), depth=rep(c(0,3,6), 100))
    plot(x ~ y, data=dat, pch=as.character(dat$depth))
    
    0 讨论(0)
提交回复
热议问题