Question

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()?

Was it helpful?

Solution

The problem is that by using D$ in your reorder call, you're reordering spec independent of the data frame, so the values no longer match up with the corresponding x values. You need to use it directly on the variables:

ggplot(D, aes(x=N, y=reorder(spec, N, sum))) + 
  geom_point(aes(size = N, color = FL_FATAL)) +
  facet_grid(. ~ FL_FATAL)

OTHER TIPS

I'm surprised you like your first way--it's a happy coincidence that worked out. Most of your species have one N value (NO_FATAL only), but you have a few that have both FATAL and NO_FATAL. Whenever there are more than two numeric rows corresponding to a factor, reorder uses a function of those numerics to do the final sort. The default function is mean, but you probably want sum, to sort by the total number of incidents.

D$spec_order <- reorder(D$spec, D$N, sum)
ggplot(D, aes(x=N, y=spec_order)) + 
  geom_point(aes(size = N, color = FL_FATAL))

ggplot(D, aes(x=N, y=spec_order)) + 
  geom_point(aes(size = N, color = FL_FATAL)) +
  facet_grid(. ~ FL_FATAL)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top