3

I'm trying to plot distribution of species between 2 different habitat types (hab 1 and hab 2). Some of my species secondarily use some habitats, so I have a separate column for secondary hab1 (hab1.sec). To visualise their distribution across the two habitats and different depths, I am using a facet_grid between hab1 and hab2. Example code as below:

# example code
set.seed(101)
ID <- seq(1,20, by=1)  ## ID for plotting
species <- sample(letters, size=20)  ## arbitrary species

## different habitat types in hab.1
hab1 <- c("coastal","shelf","slope","open.ocean","seamount")  
hab1.pri <- sample(hab1, size = 20, replace = T)

## secondarily used habitats, may not be present for some species
hab.sec <- c("coastal","shelf","slope","open.ocean","seamount", NA) 
hab1.sec <- sample(hab.sec, size = 20, replace = T)

## habitat types for hab.2
hab2 <- c("epipelagic","benthopelagic","epibenthic","benthic")  
hab.2 <- sample(hab2, size = 20, replace = T)

## arbitrary depth values
dep.min <- sample(seq(0,1000), size = 20, replace = T)
dep.max <- sample(seq(40, 1500), size = 20, replace = T)

# make data frame
dat <- data.frame(ID, species, hab1.pri, hab1.sec, hab.2,dep.min, dep.max)

# ggplot with facet grid
p <- ggplot(data=dat)+ geom_segment(aes(x=as.factor(ID),xend=as.factor(ID),y=dep.min, yend=dep.max),size=2,data = dat)+ scale_y_reverse(breaks = c(0, 200, 1000,1500))+facet_grid(hab.2~hab1.pri, scales = "free" ,space = "free")+theme_bw()

first plot

I would like to add segments for hab1.sec within the existing facet grid. I have tried this code:

p+ geom_segment(aes(x=as.factor(ID),xend=as.factor(ID),y=dep.min, yend=dep.max),linetype=2,data = dat)+facet_wrap(~hab1.sec)

But doing this produces a new graph.

second plot

Is there a better way to add those extra lines to the existing grid (preferably as dashed lines)? I'd be really grateful for any help with this! Thanks a lot, in advance!

rawr
  • 20,481
  • 4
  • 44
  • 78
diya
  • 33
  • 3
  • The approach of [this answer](http://stackoverflow.com/a/8354731/2461552) might work well here. – aosmith Apr 14 '16 at 19:08
  • thanks for replying @aosmith! did you mean `p + facet_grid(.~g, margins=TRUE)` part? unfortunately it didn't work for me.. – diya Apr 14 '16 at 20:36
  • No, removing the second faceting variable from the dataset in the second `geom_segment` call. It was pretty easy, but I wasn't sure what your final result was supposed to look like so couldn't tell if it worked or not. – aosmith Apr 14 '16 at 20:37

1 Answers1

1

What about combining the primary and secondary habitats into one variable and mapping that variable to an aesthetic?

Note I'm using tidyr and dplyr tools here because they help a lot in cases like this.

library(dplyr)
library(tidyr)

dat %>%
  gather(hab1, value, -ID, -species, -(hab.2:dep.max)) %>%
  ggplot()+ 
  geom_segment(aes(x=as.factor(ID),xend=as.factor(ID),y=dep.min, yend=dep.max, linetype=hab1),size=2) + 
  scale_y_reverse(breaks = c(0, 200, 1000,1500))+
  facet_grid(hab.2~value, scales = "free" ,space = "free")+
  theme_bw()
boshek
  • 4,100
  • 1
  • 31
  • 55
  • hey @boshek , that's really neat! yeah I wanted to combine the two together, but I didn't know which package would be the best tool.. tried using `melt` from `reshape2` but couldn't figure out how to make it work.. Can I just bother you with another tiny detail, please.. would it be possible to change `linetype` according to `hab1` instead of `colour` ? Thanks a ton! – diya Apr 14 '16 at 20:18
  • Sure. Just change `colour` to `linetype`. Spend some time with `dplyr` and `tidyr`. It will change your life. – boshek Apr 14 '16 at 20:23