require(ggplot2)
The data: It's shark incidents grouped by shark species. It's actually a real dataset, already summarized.
D <- structure(list(FL_FATAL = structure(c(2L, 2L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L), .Label = c("FATAL",
"NO FATAL"), class = "factor"), spec = structure(c(26L, 24L,
6L, 26L, 25L, 16L, 2L, 11L, 27L, 5L, 24L, 29L, 12L, 21L, 13L,
15L, 28L, 1L, 17L, 19L, 8L, 3L, 6L, 13L, 22L, 18L, 27L, 14L,
23L, 20L, 7L, 4L, 8L, 9L, 10L), .Label = c("blacknose", "blacktip",
"blue", "bonnethead", "bronze", "bull", "caribbean", "draughtsboard",
"dusky", "galapagos", "ganges", "hammerhead", "involve", "leon",
"mako", "nurse", "porbeagle", "recovered", "reef", "sand", "sandtiger",
"sevengill", "spinner", "tiger", "unconfired", "white", "whitespotted",
"whitetip", "wobbegong"), class = "factor"), N = c(368L, 169L,
120L, 107L, 78L, 77L, 68L, 59L, 56L, 53L, 46L, 42L, 35L, 35L,
33L, 30L, 29L, 29L, 26L, 25L, 25L, 25L, 24L, 24L, 21L, 21L, 20L,
20L, 17L, 16L, 16L, 15L, 11L, 11L, 11L)), .Names = c("FL_FATAL",
"spec", "N"), row.names = c(NA, -35L), class = "data.frame")
.
head(D)
# FL_FATAL spec N Especies
# 1 NO FATAL white 368 white
# 2 NO FATAL tiger 169 tiger
# 3 NO FATAL bull 120 bull
# 4 FATAL white 107 white
# 5 NO FATAL unconfired 78 unconfired
# 6 NO FATAL nurse 77 nurse
Reordering a factor variable by a numeric making a new variable.
# Re-order spec creating Especies variable ordered by D$N
D$Especies <- factor(D$spec, levels = unique(D[order(D$N), "spec"]))
# This two plots work as spected
ggplot(D, aes(x=N, y=Especies)) +
geom_point(aes(size = N, color = FL_FATAL))
ggplot(D, aes(x=N, y=Especies)) +
geom_point(aes(size = N, color = FL_FATAL)) +
facet_grid(. ~ FL_FATAL)
Reordering using reorder()
# Using reorder isn't working or am i missing something?
ggplot(D, aes(x=N, y=reorder(D$spec, D$N))) +
geom_point(aes(size = N, color = FL_FATAL))
# adding facets makes it worse
ggplot(D, aes(x=N, y=reorder(D$spec, D$N))) +
geom_point(aes(size = N, color = FL_FATAL)) +
facet_grid(. ~ FL_FATAL)
Which would be the correct approach for producing the plots with reorder()
?