How to make a conditional y~x graph for each data.frame factor?

Asked

Viewed 637 times

8

Suppose a data.frame like the following:

set.seed(1)
dados <- data.frame(w=rep(c("A", "B", "C", "D"), 50), y= rnorm(200), x=rnorm(200),
                    stringsAsFactors=FALSE)

How to create a chart y~x separated by each category of w?

2 answers

7

One way is to use the function coplot:

coplot(y~x |w, data=dados)

inserir a descrição da imagem aqui

It is also possible to do with the ggplot2 using facet_wrap:

library(ggplot2)
ggplot(data=dados, aes(y=y, x=x))+ geom_point(aes(color=w))+facet_wrap( ~ w)

inserir a descrição da imagem aqui

  • 1

    Very good! You’ve gathered me together

5


You can also use the Lattice library for this.

library(lattice)
xyplot(y~x|w, data=dados)

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.