[英]Move order of legend entries of same aesthetic but different data in ggplot
我有一些不同的數據,但這應該說明這一點:
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"))
這給出了這個 plot:
現在假設我想按順序排列我的圖例:
我怎么能那樣做?
您可以將矢量添加到調色板,並按您想要的順序命名:
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"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.