How put a second y axis in ggplot2?

Hi community, Im try to put a second y axis with sec.axis() but get an error. How fix this?

FB_2 <- data.frame(
  structure(list(Especie = c("Dasyprocta punctata", "Leopardus pardalis", 
                                              "Tamandua mexicana", "Dasypus novemcinctus", "Didelphis marsupialis", 
                                              "Cerdocyon thous", "Procyon cancrivorus", "Eira barbara", "Sciurus granatensis", 
                                              "Cuniculus paca", "Conepatus semistriatus", "Mazama sanctaemartae", 
                                              "Sylvilagus sp", "Herpailurus yagouaroundi", "Dicotyles tajacu", 
                                              "Cabassous centralis", "Puma concolor", "Leopardus wiedii", "Metachiurus myosuros", 
                                              "Odocoileus cariacou"), Records = c(278L, 237L, 235L, 228L, 226L, 
                                                                                  224L, 204L, 191L, 164L, 160L, 143L, 132L, 117L, 90L, 79L, 56L, 
                                                                                  40L, 36L, 19L, 17L), Biomass = c(743644.44, 2820318.96, 989345.3, 
                                                                                                                   958461.84, 246602.16, 1173755.52, 1417783.68, 746815.73, 52224.16, 
                                                                                                                   1307608, 288860, 2508000, 137216.43, 618750, 1680068.51, 213357.76, 
                                                                                                                   2064001.6, 116998.92, 6697.5, 943645.52), IUCN.status = c("LC", 
                                                                                                                                                                             "LC", "LC", "LC", "LC", "LC", "LC", "LC", "LC", "LC", "LC", "NE", 
                                                                                                                                                                             "NE", "LC", "LC", "DD", "LC", "NT", "NE", "LC")), row.names = c(1L, 
                                                                                                                                                                                                                                             6L, 4L, 3L, 2L, 5L, 7L, 8L, 10L, 9L, 11L, 12L, 13L, 14L, 15L, 
                                                                                                                                                                                                                                             16L, 17L, 18L, 19L, 20L), class = "data.frame"))

FB_2 |> ggplot(aes(x=fct_reorder(Especie,-Records) ,y=Records)) +
  geom_col() +
  coord_flip() +
  scale_y_continuous(breaks = seq(0, max(FB_2$Records), 50),
                     sec.axis = sec_axis( trans=FB_2$Biomass, 
                                          name="Second Axis")) +  # if remove sec.axis the script run.
  annotate(
    "text",
    x = FB_2$Especie,  
    y = FB_2$Records,
    label = FB_2$IUCN.status,
    vjust = 0.5,
    hjust = -0.2,
    size = 3,
    color = "red"
  )  +
  theme(axis.text.x = element_text(vjust = 0.5, hjust = 1, size =10)) +
  labs(title = 'Titulo',
       x='',
       y='No. Records')


#Error in `sec_axis()`:
#! Can't convert `transform`, a double vector, to a function.

Created on 2024-01-29 with reprex v2.0.2

trans has to be set to a formula or a function, not a vector.

Im use this but get error:

sec.axis = sec_axis(trans= ~ . * FB_2$Biomass,  name="Second Axis")

Error in `mono_test()`:
# ! Transformation for secondary axes must be monotonic.

ggplot wants the points for the second axis to be increasing as you go through the vector or decreasing as you go through. Biomass sometimes goes up and sometimes goes down. That's not allowed.

(ggplot ain't very flexible about second axes. )

It's one of my first times using two y axes. :disappointed_relieved:

Unless you absolutely must don't do it.

Avoiding the Dual Axis Chart

Would you settle for two plots side by side?

I have renamed your data to dat1 as is easier to work with for me and this is a very rough sketch of what I mean, but would it serve your purposes?

suppressMessages(library(tidyverse)) 
library(patchwork)


p1 <- ggplot(dat1,aes(x=fct_reorder(Especie,-Records) ,y=Records)) +
  geom_col() +   coord_flip() +
  scale_y_continuous(breaks = seq(0, max(dat1$Records), 50))
                     
                     
p2 <- p1 +   annotate( "text",   x = dat1$Especie,     y = dat1$Records,
    label = dat1$IUCN.status,
    vjust = 0.5,
    hjust = -0.2,
    size = 3,
    color = "red")  +
  theme(axis.text.x = element_text(vjust = 0.5, hjust = 1, size =10)) +
  labs(title = 'Titulo', x='',  y='No. Records')


b1 <- ggplot(dat1,aes(x=fct_reorder(Especie,-Biomass) ,y=Biomass)) +
  geom_col() +   coord_flip() +
  scale_y_continuous(breaks = seq(0, max(dat1$Records), 50))

b2 <- p1 +   annotate( "text",   x = dat1$Especie,     y = dat1$Biomass,
    label = dat1$IUCN.status,
    vjust = 0.5,
    hjust = -0.2,
    size = 3,
    color = "red")  +
  theme(axis.text.x = element_text(vjust = 0.5, hjust = 1, size =10), axis.text.y=element_blank(),  axis.ticks.y=element_blank()) +
  labs(title = 'Titulo', x='',  y='Biomass') 

p2 + b2

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.