0

I have the following 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
  )
)

My current problem is that when I run the following code, the area of "real" is in the background. I want this area to be in the foreground because I want to compare the two areas with each other.

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

Can you show me how I can change it? Many thanks and best regards!

deptrai
  • 147
  • 1
  • 9
  • Possible duplicate of [Reorder levels of a factor without changing order of values](https://stackoverflow.com/questions/2375587/reorder-levels-of-a-factor-without-changing-order-of-values) – ckett Jul 25 '19 at 19:04
  • The default order for factors is alphabetical. You are looking to change the default order, try: https://stackoverflow.com/questions/2375587/reorder-levels-of-a-factor-without-changing-order-of-values or https://www.r-bloggers.com/reorder-factor-levels/ for a basic example – ckett Jul 25 '19 at 19:05

1 Answers1

0

This is a factors order issue, this question is related but not the same, since it deals with dates.

Start by reordering the factor levels, and relabel it accordingly.

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"))

Now the plot works as the question asks.

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

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66