1 Goal


The goal of this tutorial is to rotate the title of the secondary y axis.


2 How to add a secondary axis


library(ggplot2)

# We are going to use the iris database

ggplot( data = iris, aes( x = seq_len(150))) + geom_line( aes( y = Petal.Width), size = 0.5) + 
                                               xlab( "Entry number") +
                                               ylab( "Petal Width (cm)") +
                                               geom_line( aes( y = Petal.Length), 
                                                          size = 0.5, colour = "steelblue3") +
                                               scale_y_continuous( sec.axis = sec_axis( ~. , 
                                                          name = "Petal Length (cm)"))


3 How to rotate the secondary axis


library(ggplot2)

# We are going to use the iris database

ggplot( data = iris, aes( x = seq_len(150))) + geom_line( aes( y = Petal.Width), size = 0.5) + 
                                               xlab( "Entry number") +
                                               ylab( "Petal Width (cm)") +
                                               geom_line( aes( y = Petal.Length), 
                                                          size = 0.5, colour = "steelblue3") +
                                               scale_y_continuous( sec.axis = sec_axis( ~. , 
                                                          name = "Petal Length (cm)")) +
                                              theme( axis.title.y.right = element_text( angle = 90))


4 Conclusion


In this tutorial we have learnt how to create a secondary y axis and rotate its title.