簡體   English   中英

在ggplot中移動具有相同美學但不同數據的圖例條目的順序

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM