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