Add secondary axis on ggplot

Hello
I would like to add a secondary axis on my geom_bar to represent the depth for each of my stations.
I would like to have something similar to this :

with the depth represented by points at each station and a line connecting them all.
And also have the new axis for the depth on the left (in front of the y axis)

Here an exemple of a df :

df <- tribble(
  ~zone, ~type, ~station, ~species, ~number, ~depth
  'A1', 'Adult', 1, 'Atlanticus', 2, 200,
  'A1', 'Adult', 1, 'Olrikii', 1, 450,
  'A1', 'Larvae', 2, 'Medius', 5, 345,
  'A2', 'Larvae', 1, 'Glacialis', 7, 269,
  'A2', 'Larvae', 2, 'Unidentified', 3, 405, 
  'A2', 'Adult', 2, 'Glacialis', 2, 380,
  'A2', 'Larvae', 2, 'Medius', 4, 310,
  'A3', 'Zoo', 1, 'Capilatta', 17, 290,
  'A3', 'Adult', 3, 'Olrikii', 1, 410
)

Thanks you very much

I am not sure what you want to plot from that data set. The code below shows how to define a secondary y axis using that data set. Notice that the values on second axis have to be calculated from the values on the primary axis. I adjust the Depth values so they will be conveniently displayed on the primary scale and then I do the inverse adjustment to display the desired scale on the secondary axis.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
df <- tribble(
  ~zone, ~type, ~station, ~species, ~number, ~depth,
  'A1', 'Adult', 1, 'Atlanticus', 2, 200,
  'A1', 'Adult', 1, 'Olrikii', 1, 450,
  'A1', 'Larvae', 2, 'Medius', 5, 345,
  'A2', 'Larvae', 1, 'Glacialis', 7, 269,
  'A2', 'Larvae', 2, 'Unidentified', 3, 405, 
  'A2', 'Adult', 2, 'Glacialis', 2, 380,
  'A2', 'Larvae', 2, 'Medius', 4, 310,
  'A3', 'Zoo', 1, 'Capilatta', 17, 290,
  'A3', 'Adult', 3, 'Olrikii', 1, 410
)
ggplot(df,aes(species)) + geom_col(aes(y = number, fill = zone), 
                                   color = "white") +
  geom_line(aes(y = depth / 20, group = 1)) +
  scale_y_continuous(sec.axis = sec_axis(~ . * 20, name = "Depth"))

Created on 2022-05-31 by the reprex package (v2.0.1)

Thanks for your response !
Yes sorry I didn't see there were not the axis names..

So I want to my in X my Station, Y (right side) my % and on the other Y axis (left side) my depth

df %>% 
  filter(Individual_type == "Adult") %>%
  group_by(Station) %>% 
  mutate(Percent = 100 * Number/Total) %>%
  ggplot(aes(Station, fill = Species)) + 
  geom_bar(aes(y = Percent), position = "fill", stat = "identity") +
  geom_line(aes(y = Depth / 20, group = 1)) +
  scale_y_continuous(sec.axis = sec_axis(~ . * 20, name = "Depth")) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  scale_fill_manual(name = "Species", values=PZ) + 
  facet_grid(~factor(Area,levels = c("A1","A2","A3")),scale = "free_x", space = "free_x", switch = "x")

Created on 2022-05-31 by the reprex package (v2.0.1)

I tried this code but I don't know how to have my %, so have the right side Y axis between 0 and 100 or 0 and 1 to see the proportion of each species for each station for each area and on the other side have the depth of each station with geom_line..

Please post a data set that can be used with your ggplot code. The data you posted earlier only has one station for each zone, so a line drawn with station on the x axis cannot be drawn.
Do you want a line drawn that goes across facet boundaries?

The zone doesn't matter, it was just for an exemple to adapt to my real dataset after.
Yes the idea it's to have a line that goes across facet boundaries
I can modificate the dataset if you want

Thanks

Thanks for your time ! *

This is the plot I can make with your most recent data. I do not know how to get a line to go from one facet to the next.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
df <- tribble(
  ~zone, ~type, ~station, ~species, ~number, ~depth,
  'A1', 'Adult', 1, 'Atlanticus', 2, 200,
  'A1', 'Adult', 1, 'Olrikii', 1, 450,
  'A1', 'Larvae', 2, 'Medius', 5, 345,
  'A1', 'Adult', 3, 'Calo', 8, 402,
  'A1', 'Larvae', 3, 'Pryi', 15, 298,
  'A1', 'Zoo', 4, 'Galk', 2, 336,
  'A1', 'Adult', 4, 'Spu', 9, 315,
  'A2', 'Adult', 5, 'Dryu', 17, 269,
  'A2', 'Adult', 5, 'Spu', 3, 256,
  'A2', 'Larvae', 5, 'Olrikii', 13, 311,
  'A2', 'Zoo', 6, 'Calo', 5, 289,
  'A2', 'Zoo', 6, 'Unidentified', 3, 405, 
  'A2', 'Adult', 6, 'Glacialis', 2, 380,
  'A2', 'Larvae', 7, 'Medius', 4, 310,
  'A2', 'Larvae', 7, 'Glou', 7, 269,
  'A3', 'Zoo', 8, 'Capilatta', 17, 265,
  'A3', 'Zoo', 8, 'Calo', 17, 295,
  'A3', 'Adult', 9, 'Dryu', 17, 378,
  'A3', 'Zoo', 9, 'Glou', 17, 368,
  'A3', 'Zoo', 9, 'Azu', 17, 490,
  'A3', 'Adult', 10, 'Olrikii', 1, 410
)

df %>% 
  filter(type == "Adult") %>%
  group_by(station) %>% 
  mutate(Percent = 100 * number/sum(number),
         station=factor(station)) %>%
  ggplot(aes(station, fill = species)) + 
  geom_bar(aes(y = Percent), position = "stack", stat = "identity") +
  geom_line(aes(y = depth /4, group = 1)) +
  scale_y_continuous(sec.axis = sec_axis(~ . * 4, name = "Depth")) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  #scale_fill_manual(name = "Species", values=PZ) + 
  facet_grid(~factor(zone,levels = c("A1","A2","A3")),scale = "free_x", space = "free_x", switch = "x")

Created on 2022-05-31 by the reprex package (v2.0.1)

Thank you very much for your time !
Sorry for that dataset, it's a terrible dataset but it was just to illustrate.
I succeed to have what I wanted thanks to you !

This is Awesome! This is my first time seeing responses from the R Community, now I know where to look for help

This topic was automatically closed 7 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.