0

I am quite a newbie to RStudio and I am having problems adding different vertical lines on each of my two facets using facet_wrap

Here is what I thought:

library(ggplot2)

g <- ggplot(dat, aes(x=index, na.rm= TRUE))

d <- g+ geom_density() + facet_wrap(~gender)

vline.data <- data.frame(z = c(2.36,2.48),gender = c("Female","Male")) 

d1 <- d + geom_vline(aes(xintercept = z),vline.data)

But it adds the same two lines to each facet - what would you reckon is the problem? I have thought of somehow splitting the facets into two separate data frames, but I have no idea how to go on about it.

P.S The x-axis (the index) goes from 1 to 4.

Thank you in advance.

        index  <-  c(NA, NA, 4, 4, 4, NA, NA, NA, NA, 2, 2, 2, 2, 2, 3, 3, 3, 3, 
        3, NA, 3, NA, NA, 3, 4, 4, 4, 4, 4, 3, 4, 3, 4, 3, NA, 4, 2, 
        4, 4, 2, 2, NA, 2, 3, 3, 2, 2, NA, NA, 2)

       gender <-  c("Female", "Female", "Male", "Male", "Male", "Female", "Female", 
        "Male", "Female", "Male", "Female", "Male", "Female", "Male", 
        "Male", "Female", "Female", "Female", "Male", "Female", "Female", 
        "Male", "Male", "Female", "Female", "Female", "Male", "Male", 
        "Female", "Female", "Male", "Female", "Female", "Female", "Male", 
        "Male", "Female", "Male", "Male", "Female", "Female", "Male", 
        "Male", "Female", "Female", "Male", "Male", "Male", "Male", "Male")

Using the code and data above I get this plot

Joe
  • 6,965
  • 1
  • 43
  • 53
amyk
  • 9
  • 3
  • Welcome to stack overflow. Please read about [how to make a great reproducible question](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – shayaa Dec 04 '16 at 10:16
  • Hi, this doesn't seem to be the problem. Maybe I didn't ask my question very well - I want to add the one line only to one facet and the other one to the other. 2.36 only to the facet corresponding to female and 2.48 to the male facet – amyk Dec 04 '16 at 10:38

0 Answers0