From Moab To Midian
The abrupt change from concerns about the Moabites to the Midianites, and mention of the end of a plague which is never described as starting, is explained by the documentary hypothesis as being down to a change from the JE source to the Priestly source. Despite the Torah redactor usually, according to the hypothesis, interlacing together the sources, the stories here were evidently difficult to combine, as the redactor appears to have cut the end of the JE version, and beginning of the Priestly source version, joining the remaining ends of each source together.
As the Priestly source is considered to be based on the JE source, albeit spun to the Aaronid priesthood's political viewpoint, the original JE ending, and priestly source beginning, can be expected to be fairly similar to the text that is currently present. Nevertheless, the use of plagues, by God, to punish the people in very large numbers, is unique to the Priestly source, once the Israelites have left Egypt, and consequently this detail is unlikely to have been present in the original.
While it is considered uncertain as to why the Priestly source would change Moab to Midian, it is generally agreed amongst critical scholars that the account of the war against Midian, and its spoils, originates from a writer who added these elements to the original version of the Priestly source. The command to vex the Midianites is also considered a part of these additions, and thus the revenge on Midian serves as a vehicle for this writer to include the list of spoils, and its distribution, which has a larger amount of text than the narrative of the war. Thus, this later writer could easily have changed Moab to Midian to facilitate this addition.
Other Baal Peor, of Jordanian-Moabite origin, appeared as both the sun god and moon goddess, as did his priests. Healing was discerned from feces, often salt bathing in the dead sea, and his cult was present in the south dead sea (i.e. Sodom and Gomorrah legends).
Read more about this topic: Heresy Of Peor