r - reorder facets by multiple variables in ggplot -
i have data frame following column names/header:
date time id datetime site origin species genus sex pp repro
i trying ggplot faceted figure y-axis being of "id"s, in order of "sex" , in order of "repro"... ideally female "ids" @ top , in order of repro status, , under male "ids", grouped repro status (females - pregnant, females - non pregnant, males - reproducing, males -non - in order). ggplot code looks 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 2 variables here didn't work...
in case useful... here's more information 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")
the comment left @aosmith point in right direction. here example based on larger simulated data set.
library(ggplot2) library(dplyr) # create , 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 data set sex , repro status, relevel id factor # such levels in opposite order of arranged data.frame. # allow order expected in 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")
Comments
Post a Comment