如何更改填充区域的顺序?

时间:2019-07-25 18:46:14

标签: r ggplot2 plot

我有以下df

df_pr_curve <- data.frame( 
  Recall = c(0, 0.1,    0.2,    0.3,    0.4,    0.5,    0.6,    0.7,    0.8,    0.9,    1, 
             0, 0.1,    0.2,    0.3,    0.4,    0.5,    0.6,    0.7,    0.8,    0.9,    1),
  Test = c("real",  "real", "real", "real", "real", "real", "real", "real", "real", "real", "real",
           "synthetic", "synthetic",    "synthetic",    "synthetic",    "synthetic",    "synthetic",    "synthetic",    "synthetic",    "synthetic",    "synthetic",    "synthetic"
           ),
  Precision = c(0.9615, 0.4498, 0.4498, 0.4157, 0,  0,  0,  0,  0,  0,  0,
                1,  1,  1,  1,  1,  1, 1,   1,  0.999,  0.997,  0
  )
)

我当前的问题是,当我运行以下代码时,“真实”区域位于背景中。我希望该区域处于前台,因为我想将两个区域相互比较。

plot <- ggplot(df_pr_curve, mapping = aes(x=Recall,y=Precision,fill = Test))+
  geom_area(position="identity", stat="identity",alpha=.5)

您能告诉我如何进行更改吗? 致以真诚的感谢和诚挚的问候!

1 个答案:

答案 0 :(得分:0)

这是一个因素定单问题,this question是相关的但不相同,因为它处理日期。

首先重新排列因子水平,然后相应地重新标记。

df_pr_curve$Test <- as.character(df_pr_curve$Test)
df_pr_curve$Test <- factor(df_pr_curve$Test, 
                           levels = c("synthetic", "real"),
                           labels = c("synthetic", "real"))

现在该情节可以按照问题提出了。

p <- ggplot(df_pr_curve, aes(x = Recall, y = Precision, fill = Test)) +
  geom_area(position = "identity", stat = "identity", alpha = 0.5)

p

enter image description here