在ggplot中通过多个变量重新排序facet

时间:2015-10-27 17:49:34

标签: r ggplot2 facet reorderlist

我有一个包含以下列名称/标题的数据框:

 date         time              id            datetime    site  origin           species         genus    sex PP repro

我正在尝试ggplot一个刻面的人物,y轴是我所有的“id”,依次是“性别”,然后按照“repro”的顺序...所以理想情况下所有的女性“ids”都会在重复状态的顶部和顺序,然后在那将是所有男性“ids”,与他们的repro状态(女性 - 怀孕,女性 - 非怀孕,男性 - 再生,男性 - 非 - 按顺序组合在一起)。这就是我的ggplot代码目前的样子:

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

这里用两个变量重新排序显然不起作用......

如果这很有用......这里有来自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")

1 个答案:

答案 0 :(得分:1)

@aosmith留下的评论将指向正确的方向。以下是基于较大模拟数据集的示例。

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