简体   繁体   中英

Plotting labels as points in R

I have the following:

> x<-runif(20)
> y<-rnorm(x)
> g<-rep(factor(LETTERS[1:4]),5)

How can I do

> plot(x,y)

so that the points that are plotted is the corresponding value of g ?

Thanks!

Try

plot(x,y,type="n")
text(x,y,as.character(g))

or (obligatory ggplot)

library(ggplot2)
d <- data.frame(x,y,g)
qplot(x,y,label=g,geom="text",data=d)

or (lattice)

library(lattice)
xyplot(y~x,
    panel=function(...) { 
       panel.xyplot(...,type="n")
       panel.text(x,y,g) })

(I don't know how well that last solution would work if one actually wanted to use some of the features of lattice like dividing the plot into multiple panels ...)

对于单行解决方案,请使用pch (即“绘图字符”)参数:

plot(x,y,pch=as.character(g))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM