-1

I have written this code:

#Aislar los valores que necesitamos
BP15.5.0op <- subset(ambiental2020, dependencia=="BP15.sala" & tipo.control=="NV")
BP15.5.0op <- BP15.5.0op[!is.na(BP15.5.0op[,"part.5.0"]),]
BP15.5.0op$punto <- paste("Punto", BP15.5.0op$punto, sep=".")

ggplot(BP15.5.0op, 
       aes(x=fecha, y=part.5.0, group = punto, fill = punto)) +
  geom_point(aes(colour = punto), size = 1.5) +
  geom_line(aes(colour = punto), linetype = 1, size = 1) +
  theme(plot.title = element_text(size = 20, face = "bold")) +
  ggtitle("PARTÍCULAS DE 5,0 micras SALA BP15") +
  ylab("Partículas 5,0 micras/m3") +
  geom_hline(yintercept = 29000, colour = "red", size = 1, linetype = 6) +
  scale_x_date(breaks = unique(BP15.5.0op$fecha), labels = date_format("%d/%m/%Y")) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 5.5), legend.position = "none") +
  facet_wrap(~ punto, ncol = 5, strip.position = "left")

In order to create a graph like this one:

enter image description here

I want to know how I can change the order of the graphs. It sould be "Punto.1 Punto.2 Punto.3 ... Punto.16" and not "Punto.1 Punto.10...". So I don't want the graphs to be sorted alphabetically.

Dani
  • 153
  • 8

1 Answers1

1

The facets are sorted by factor levels for a factor. A great option for getting correct sorting of this type is the {naturalsort} package. If you just change your line:

BP15.5.0op$punto <- paste("Punto", BP15.5.0op$punto, sep=".")

to

library(naturalsort)

BP15.5.0op$punto <- naturalsort::naturalfactor(paste("Punto", BP15.5.0op$punto, sep="."))

I think you'll get what you're after.

Also see this related question

Dan Adams
  • 4,971
  • 9
  • 28