smillig
smillig

Reputation: 5341

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!

Upvotes: 2

Views: 318

Answers (2)

Ben Bolker
Ben Bolker

Reputation: 226247

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 ...)

Upvotes: 8

Josh O&#39;Brien
Josh O&#39;Brien

Reputation: 162341

For a one-line solution, use the pch (i.e. 'plotting character') argument:

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

Upvotes: 6

Related Questions