简体   繁体   中英

Plotting multiple independent variables with R

Say I have a dataframe constructed like this:

x1 <- c(1,1,1,1,2,2,2,2)
x2 <- c(1,1,2,2,1,1,2,2)
x3 <- c(1,2,1,2,1,2,1,2)
y <- c(1:8)
d <- data.frame(x1,x2,x3,y)

To help analyze the data, I can use plot(d) to generate 6 graphs to see the effects of x1, x2, and x3 on y. Is there a way to color points differently based on the value of x1 with their symbol based of the value of x2 and their size based off of the value of x3? It doesn't need to be these specific properties. But when I try to add color or change the character with

plot(d, col=c("red", "blue"), pch=c(19, 21))

Only x3 is affected. Is there a way to specify which points receive which properties? Or maybe what I'm trying to accomplish is easier with ggplot2?

Yes, ggplot is pretty straightforward for this.

 library(ggplot2)
 ggplot(d, aes(x = x1, y = y, shape = as.factor(x2), color = as.factor(x3))) + 
  geom_point()

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