How to get from two plots made with ggcombine to a single plot with two y-axis?

Hey folks,
I am looking for a way to combine two plots. Using ggarrange I have already been able to put them in one figure, but now I would like to combine them with two Y-axis. Unfortunately, I am not getting anywhere at the moment. R is still completely new territory for me. There are some explanations on the internet but unfortunately I have problems applying them to my specific data set. I am honest.
In Case anybody is interested: The upper graph symbolises the moon phase. 1 is full moon and 0 is new moon. The lower graph with geom_col() symbolises animals photographed by a camera trap. The goal is to determine the activity as a function of the moon phase.
The code I used for the two plots is:

ggarrange(
  
  ggplot(d1, aes(x = Datum, y = mondphase)) + 
    geom_smooth(span = .2, se = FALSE) +
    ylab("Mondphase") + xlab("") +
    theme_bw()
  ,
  ggplot(d1, aes(x = Datum, y = Number.of.Animals)) +
    geom_col(show.legend = FALSE) +
    ylab("Anzahl Auslösungen") +
    theme_bw()
  ,
  ncol = 1, align = "v", heights = 1:2
)

Here is the documentation for adding a secondary axis on ggplot2.

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Unless you have a very pressing reason to use a 2-y-axis graph with non-equivalent y-axes, the best advice at the moment is "Don't do it".
For some comments on this see:

My impression is that your currant 2-panel figure is much better.

Hello and thanks for your response!

This is a summary and the structure of my data. It is a table with camera trap data from several locations. I used "d1" for a single camera from that list.

ID Camera.Trap.Name Latitude Longitude Photo.Type Photo.Date Photo.time    Class           Order   Family     Genus   Species
1 2492        Frankfurt    12345     54321     Animal 2020-05-15   22:31:03 MAMMALIA CETARTIODACTYLA CERVIDAE Capreolus capreolus
2 2493        Frankfurt    12345     54321     Animal 2020-05-15   22:34:50 MAMMALIA CETARTIODACTYLA CERVIDAE Capreolus capreolus
3 2494        Frankfurt    12345     54321     Animal 2020-05-15   22:34:51 MAMMALIA CETARTIODACTYLA CERVIDAE Capreolus capreolus
4 2495        Frankfurt    12345     54321     Animal 2020-05-15   22:34:52 MAMMALIA CETARTIODACTYLA CERVIDAE Capreolus capreolus
5 2496        Frankfurt    12345     54321     Animal 2020-05-15   22:38:50 MAMMALIA CETARTIODACTYLA CERVIDAE Capreolus capreolus
6 2497        Frankfurt    12345     54321     Animal 2020-05-15   22:38:51 MAMMALIA CETARTIODACTYLA CERVIDAE Capreolus capreolus
  Number.of.Animals Camera.Start.Date Camera.End.Date Flash      Datum                  up                down day_length
1                 1        2020-05-14      2020-06-24    25 2020-05-15 2020-05-15 05:38:31 2020-05-15 21:05:54   15.45635
2                 1        2020-05-14      2020-06-24    25 2020-05-15 2020-05-15 05:38:31 2020-05-15 21:05:54   15.45635
3                 1        2020-05-14      2020-06-24    25 2020-05-15 2020-05-15 05:38:31 2020-05-15 21:05:54   15.45635
4                 1        2020-05-14      2020-06-24    25 2020-05-15 2020-05-15 05:38:31 2020-05-15 21:05:54   15.45635
5                 1        2020-05-14      2020-06-24    25 2020-05-15 2020-05-15 05:38:31 2020-05-15 21:05:54   15.45635
6                 1        2020-05-14      2020-06-24    25 2020-05-15 2020-05-15 05:38:31 2020-05-15 21:05:54   15.45635
  mondphase  X
1 0.3737363 NA
2 0.3737363 NA
3 0.3737363 NA
4 0.3737363 NA
5 0.3737363 NA
6 0.3737363 NA

Hello!
You might be right but I was told it looks way better if these two plots are combined. Unfortunately nobody could tell me how it's done.

The majority of people from statistical backgrounds will disagree with you on that. Dual y axis plots only make sense when there is a one-to-one transformation between the axes, e.g. to display two currencies or degrees Celsius/Fahrenheit (assuming there is ever a use case for this).

Your options are thus to conceive a way to transform the axes as specified in the link provided by andresrcs or to hack the building blocks of ggplot2 to do so. Both can be done (the former is much easier), but I would again advise you against it.

This is not in a copy/paste friendly format, which makes helping you harder. Can you please read the guide on the link I gave you and try to provide a proper reproducible example?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.