R ggplot中相同但不同数据的图例条目的移动顺序
我有一些不同的数据,但这里应该说明一点:R ggplot中相同但不同数据的图例条目的移动顺序,r,ggplot2,R,Ggplot2,我有一些不同的数据,但这里应该说明一点: x = seq(1, 10, 0.1) y = x + rnorm(10) df = data.frame(x = x, y = y) sample = sample(c(TRUE, FALSE), nrow(df), replace = T, prob = c(.7, .3)) train = df[sample,] test = df[!sample,]
x = seq(1, 10, 0.1)
y = x + rnorm(10)
df = data.frame(x = x, y = y)
sample = sample(c(TRUE, FALSE),
nrow(df),
replace = T,
prob = c(.7, .3))
train = df[sample,]
test = df[!sample,]
绘制数据,加上50%分位数的分位数回归,我这样做:
ggplot(train, aes(x, y)) +
geom_point(aes(color = "train")) +
geom_point(data = test,
aes(color = "test")) +
geom_quantile(quantiles = 0.05,
aes(color = "median regression"))
它给出了这个图:
现在让我们说,我想让我的传奇按顺序排列:
- 试验
- 中值回归
- 训练
我该怎么做呢?您可以向调色板添加一个矢量,并根据需要按顺序命名:
ggplot(train, aes(x, y)) +
geom_point(aes(color = "train")) +
geom_point(data = test,
aes(color = "test")) +
geom_quantile(quantiles = 0.05,
aes(color = "median regression"))+
scale_color_discrete(limits = c("test", "median regression", "train"))