Nanna
Nanna

Reputation: 93

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)?

Upvotes: 1

Views: 325

Answers (2)

Jon Spring
Jon Spring

Reputation: 66510

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

Upvotes: 2

Nanna
Nanna

Reputation: 93

In addition to Jon Springs solution above for sending one/a few values to geom_line there is a solution if one have a lot of lines to add (like if one displays several elemental analysis's and wants to add different detection levels to some elements):

ggplot(gather(df, key=KeyVector, value="value", ),
                  aes(y=value, x=Xvalues, color=ColValues, shape=ShapeValues))  + 
                  geom_point() + 
                  #geom_histogram() +
                  facet_wrap(~ KeyVector, scales = 'free') +
                  geom_hline(data = tibble(KeyVector, y = unlist(HlineValues)), aes(yintercept=y) )

> dput(KeyVector)
c("Al", "Ca", "Fe", "K", "Mg", "Mn", "Na", "P", "Si", "Ti")

> dput(HlineValues)
structure(list(Al = NA_real_, Ca = NA_real_, Fe = 0.00699430761427042, 
    K = NA_real_, Mg = NA_real_, Mn = 0.00774461846427111, Na = NA_real_, 
    P = 0.00436426817378561, Si = NA_real_, Ti = 0.00599348901270895), class = "data.frame", row.names = "y")

To be clear for others with similar knowledge as me the variable HlineValues is a data frame with one row (that contains the data to become the hline) that must have the same length as KeyVector, colnames(HlineValues) are the same as what is in KeyVector.

Upvotes: 1

Related Questions