user5063841
user5063841

Reputation:

reorder facets by multiple variables in ggplot

I have a data frame with the following column names/header:

 date         time              id            datetime    site  origin           species         genus    sex PP repro

I am trying to ggplot a faceted figure with the y-axis being all of my "id"s, in order of "sex" and then in order of "repro"... so ideally all female "ids" will be at the top and in order of repro status, and then under that will be all male "ids", grouped together with their repro status (females - pregnant, females - non pregnant, males - reproducing, males -non - in that order). This is what my ggplot code looks like currently:

    Presence<-ggplot(data, aes(x=date,y=reorder(sex,repro)))+
  geom_point(aes(x=date,y=Presence,colour=sex))+facet_grid(id~.)+
  theme()+
  ylab("\n")+
  theme(legend.position="none",
        axis.text.y= element_blank(),
        strip.text.y=element_text(angle=0))

reordering by two variables here obviously didn't work...

In case this is useful... here's some more information from dput:

    structure(list(date = structure(c(16439, 16439, 16443, 16444, 
16444, 16445), class = "Date"), time = c("05:11:00.470", "19:41:08.120", 
"20:45:38.570", "22:27:59.370", "22:53:13.370", "18:44:49.630"
), id = c("989001000312244", "989001000312244", "989001000312214", 
"989001000312285", "989001000312285", "989001000312252"), datetime =    structure(list(
    sec = c(0.47, 8.12, 38.57, 59.37, 13.37, 49.63), min = c(11L, 
    41L, 45L, 27L, 53L, 44L), hour = c(5L, 19L, 20L, 22L, 22L, 
    18L), mday = c(4L, 4L, 8L, 9L, 9L, 10L), mon = c(0L, 0L, 
    0L, 0L, 0L, 0L), year = c(115L, 115L, 115L, 115L, 115L, 115L
    ), wday = c(0L, 0L, 4L, 5L, 5L, 6L), yday = c(3L, 3L, 7L, 
    8L, 8L, 9L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L), zone = c("PST", 
    "PST", "PST", "PST", "PST", "PST"), gmtoff = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    )), .Names = c("sec", "min", "hour", "mday", "mon", "year", 
"wday", "yday", "isdst", "zone", "gmtoff"), class = c("POSIXlt", 
"POSIXt")), site = c("chivato", "chivato", "chivato", "chivato", 
"chivato", "chivato"), origin = structure(c(2L, 2L, 2L, 2L, 2L, 
2L), .Label = c("carmen", "chivato", "la capilla", "las cuevas", 
"lto1"), class = "factor"), species = structure(c(2L, 2L, 2L, 
2L, 2L, 2L), .Label = c("californicus", "yerbabuenae"), class = "factor"), 
    genus = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Leptonycteris", 
    "Macrotus"), class = "factor"), sex = structure(c(1L, 1L, 
    2L, 2L, 2L, 2L), .Label = c("female", "male", "unrecorded"
    ), class = "factor"), PP = c(1, 1, 1, 1, 1, 1), repro = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("lactating", "non", "postlactating", 
    "pregnant", "testes"), class = "factor")), .Names = c("date", 
"time", "id", "datetime", "site", "origin", "species", "genus", 
"sex", "PP", "repro"), row.names = c(846L, 878L, 1101L, 1152L, 
1154L, 1185L), class = "data.frame")

Upvotes: 0

Views: 2404

Answers (1)

Peter
Peter

Reputation: 7770

The comment left by @aosmith will point you in the right direction. Here is an example based on a larger simulated data set.

library(ggplot2)
library(dplyr)

# Create and example data set
set.seed(42)
plot_data <- 
  data.frame(date  = lubridate::ymd("2015-10-01") + lubridate::days(0:30), 
             sex   = factor(sample(c("Male", "Female"), 31, replace = TRUE), levels = c("Female", "Male")),
             repro = factor(sample(c("Reproducing", "Non-Reproducing"), 31, replace = TRUE), levels = c("Reproducing", "Non-Reproducing")), 
             PP    = 1,
             id    = factor(1:31))

# arrange the data set by sex and then by repro status,  relevel the id factor
# such that the levels are in the opposite order of the arranged data.frame.
# This will allow for the order to be as expected in the following plot.
plot_data <- 
  plot_data %>% 
  arrange(sex, repro) %>%
  mutate(id = factor(id, levels = rev(id)))

ggplot(plot_data) + 
  theme_bw() + 
  aes(x = date, y = id, color = sex, shape = repro) +
  geom_point(size = 3) + 
  ylab("Subject Id") + 
  xlab("Date") + 
  theme(legend.position = "bottom")

enter image description here

Upvotes: 1

Related Questions