GGplot2 doesn't work if I reorder the x-axis

Viewed 52

I've the following dataset:

data=data.frame("n"=rep(c(1:35),3),"ni"=as.numeric(rep(ni,3)), "Type"=rep(c("A", "B", "C"), each=35), "Value"=c(M1, M2, M3))

where M1, M2 and M3 are three numerical variables. I want to plot those three variables sorted by ni on the x-axis. My code is:

p11=ggplot(GGCV, 
           aes(x = reorder(n, ni), 
              y = Value, 
              color = Type))+
geom_point(alpha = 0.7, 
             size=3, 
  )+
  geom_smooth(se=FALSE, 
              method = "lm", 
              formula = y~poly(x,4), 
                  size = 1) +
      labs(x = "Areas, sorted by sample size",
           title = "Coefficient of Variation %",
           y = "

CV%",
       color = "Estimator") +
  scale_color_manual(values=c("#999999", "#E69F00", "#00A550"))+
  coord_cartesian(ylim = c(0, 35)) +
  theme(legend.position = "none")+scale_y_continuous(labels=scaleFUN)

And R gives to me:

enter image description here

This plot is correctly sorted by ni but I can't understand why I don't have geom_smooth part of the plot.

If I replace aes(x = reorder(n, ni), y = Value, color = Type) with aes(x = n, y = Value, color = Type)

I obtain: enter image description here

which has the smooth line but is not sorted right. Can someone help me?

Thanks.

0 Answers
Related