有人建议修改代码会导致覆盖两条趋势线,每组一条(由颜色指定)吗?
library(ggplot2)
x <- seq(0,10,.1)
y <- as.matrix(cbind(rnorm(101,x,1),rep(x,length(x)),rep(1,length(x))))
z <- as.matrix(cbind(rnorm(101,x+3,1),rep(x,length(x)),rep(0,length(x))))
data <- rbind(y,z)
qplot(y=data[,1],x=data[,2],col=data[,3])
答案 0 :(得分:4)
我在df中添加了一些名称以使其更容易,并将所有内容拆分为ggplot中的单独图层,以使操作更容易,imho。
CREATE TABLE players (
id serial PRIMARY KEY,
name varchar(50),
match_count int DEFAULT 0,
wins int DEFAULT 0,
losses int DEFAULT 0,
bye_count int
);
CREATE TABLE matches (
winner_id serial references players(id),
loser_id serial references players(id),
did_battle BOOLEAN DEFAULT FALSE,
match_id serial PRIMARY KEY
);
答案 1 :(得分:3)
使用qplot:
qplot(x = data[, 2], y = data[, 1], col = as.factor(data[, 3]),
geom = c("point", "smooth"), method="lm", se = FALSE)
使用ggplot:
data_df <- data.frame(y = data[, 1], x = data[, 2], grp = as.factor(data[, 3]))
ggplot(data_df, aes(x = x, y = y, col = grp)) + geom_point() +
stat_smooth(method = "lm", se = FALSE)