简体   繁体   中英

How to get from ggcombine to a single plot with two y-axes?

I am looking for a way to combine these two plots. With ggarrange I have already been able to put them in one figure, but now I would like to combine them with two Y-axes. 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
) 

This is an exmple of my data. I used "d1" in my code because "d1" is a specific camera.

    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

I'd be extremely happy about any advice.! Thank you very much.

This is the graph I am talking about

I think you might be looking for secondary y-axis. In ggplot, they are a bit of pain to use, because it discourages people from using them. The essential two steps are:

  • Transform your input data for the secondary axis to fit the primary axis.
  • Specify the inverse-transform in sec_axis .

The make the data ranges overlap between two variables we can use the rescale function from the scales package.

library(ggplot2)
library(scales)

# Make secondary axis
secondary <- with(economics, sec_axis(
  ~ rescale(., from = range(psavert), to = range(unemploy)), # <- inverse transform
  name = "Unemployment"
))

# Make plot
ggplot(economics, aes(date)) +
  geom_col(aes(y = psavert)) +
  # Apply transformation
  geom_line(aes(y = rescale(unemploy, to = range(psavert))), # <- transform
            colour = "blue") +
  # Supply secondary axis
  scale_y_continuous(
    name = "Personal Savings Rate",
    sec.axis = secondary
  )

Created on 2021-03-04 by the reprex package (v1.0.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM