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
  • 8,073
  • 1
  • 52
  • 58
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
  • Does this answer your question? [How to Add Lines With A Facet R](https://stackoverflow.com/questions/58141541/how-to-add-lines-with-a-facet-r) – November2Juliet Sep 19 '22 at 01:50

1 Answers1

1

This was asked a long time ago, but here's the answer:

You need to add geom_vline to your ggplot. geom_vline takes a single value, not a vector, and it doesn't iterate, so you can't have multiple values in a separate data object from the rest of your data. Merge vline.data as a new column with the rest of dat as is appropriate for your data set. This question doesn't have reproducible data, but put index,gender and however you calculate vline.data into a single dat dataframe with each of those pieces as column names. Then you can summarise the vline.data. The example below assumes the values within vline.data are the mean values for the entire index column.

p <- ggplot(dat, aes(x=index, na.rm= TRUE)) +
  geom_density() + 
  facet_wrap(. ~ gender) +
  geom_vline(data = . %>% group_by(gender) %>% summarise(vl=mean(index)), 
             aes(xintercept=vl))

How to Add Lines With A Facet R