rggplot2

Adding different lines to some facets in a facet_wrap on gather:ed data


I have two data set that looks like:

> str(Maj.plot)
'data.frame':   70 obs. of  13 variables:
 $ Sample  : chr  "Kx V17" "Mu V17" "Ob V17" "Vä V17" ...
 $ Mill    : chr  "Karlsborg" "Munksund" "Obbola" "Väja" ...
 $ Halfyear: chr  "S17" "S17" "S17" "S17" ...
 $ Al      : num  0.355 0.593 0.804 0.318 0.847 ...
 $ Ca      : num  17.6 14.1 15.1 24 14.1 ...
 $ Fe      : num  0.315 0.455 0.413 0.224 0.776 ...
 $ K       : num  0.639 0.473 0.324 0.955 0.216 ...
 $ Mg      : num  5.36 9.51 10.36 2.84 12.25 ...
 $ Mn      : num  1.46 3.11 3.2 1.49 4.25 ...
 $ Na      : num  7.08 5.31 2.23 8.45 2.79 ...
 $ P       : num  0.096 0.144 0.144 0.6023 0.0829 ...
 $ Si      : num  0.767 0.467 1 1.271 3.613 ...
 $ Ti      : num  NA NA NA NA 0.018 ...

> str(DL.major)
'data.frame':   1 obs. of  10 variables:
 $ Al: num NA
 $ Ca: num NA
 $ Fe: num 0.00699
 $ K : num NA
 $ Mg: num NA
 $ Mn: num 0.00774
 $ Na: num NA
 $ P : num 0.00436
 $ Si: num NA
 $ Ti: num 0.00599

I have plotted this data with both facet_wrap and gather:

ggplot(gather(Maj.plot, key=Element.major, value="value",  -"Sample", -"Mill", -"Halfyear"),
                  aes(y=value, x=Mill, color=Mill, shape=Halfyear))  + 
  geom_point() + 
  facet_wrap(~ Element.major, scales = 'free', ncol=3) 

Added to this plot is also stuff like theme, ggtitel() and guides etc not displayed here (not sure if it impacts the answer or not).

What I would like is add different lines to some of the facets, like a geom_hline() from the data set DL.

So as an example, for the facet with the name Fe I would like an yintercept-line at y= 0.00699 (they cant be seen for this data set but I have other bigger sets where the the scale for y is small enough that the line will be visible). But no line for the Al facet.

I tried:

ggplot(gather(Maj.plot, key=Element.major, value="value",  -"Sample", -"Mill", -"Halfyear"),
                  aes(y=value, x=Mill, color=Mill, shape=Halfyear))  + 
  geom_point() + 
  facet_wrap(~ Element.major, scales = 'free', ncol=3) +
  geom_hline(data= DL.major)

But geom_hline requires one to set yintercept and I do not know what to specify there so that did not work. I also tried:

DL.major.1 <-t(DL.major)
colnames(DL.major.1) <- "DL"

ggplot(gather(Maj.plot, key=Element.major, value="value",  -"Sample", -"Mill", -"Halfyear"),
                  aes(y=value, x=Mill, color=Mill, shape=Halfyear))  + 
  geom_point() + 
  facet_wrap(~ Element.major, scales = 'free', ncol=3) +
  geom_hline(data= DL.major.1, aes(yintercept = DL))

Using this example: How to add different lines for facets But that got me all lines in all facets and that is not what I wanted.

How can it be done (and can it be done)?


Solution

  • Perhaps something like this?

    (BTW it is good practice to make your question reproducible, either by using a built-in data set or by including a sample of data structured like yours by either using dput(YOUR_DATA) or providing code that generates it.)

    mtcars %>%
      ggplot(aes(wt, mpg)) +
      geom_point() +
      #important, this should have the faceted variable
      geom_hline(data = tibble(gear = 3), aes(yintercept = 30)) +
      facet_wrap(~gear)
    

    enter image description here