ggplot2 rotate a graph : reverse and flip the plot


The aim of this R tutorial is to describe how to rotate a plot created using R software and ggplot2 package.

The functions are :

  • coord_flip() to create horizontal plots
  • scale_x_reverse(), scale_y_reverse() to reverse the axes

Horizontal plot : coord_flip()

Box plot :

library(ggplot2)
# Basic box plot
bp <- ggplot(PlantGrowth, aes(x=group, y=weight))+
  geom_boxplot()
bp
# Horizontal box plot
bp + coord_flip()

ggplot2 and R software, reverse and flip the plotggplot2 and R software, reverse and flip the plot

Histogram :

set.seed(1234)
# Basic histogram
hp <-qplot(x=rnorm(200), geom="histogram")
hp
# Horizontal histogram
hp + coord_flip()

ggplot2 and R software, reverse and flip the plotggplot2 and R software, reverse and flip the plot

Reverse y axis

The function scale_y_reverse() can be used as follow :

# Basic histogram
hp
# Y axis reversed
hp + scale_y_reverse()

ggplot2 and R software, reverse and flip the plotggplot2 and R software, reverse and flip the plot

Infos

This analysis has been performed using R software (ver. 3.1.2) and ggplot2 (ver. )


Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!






This page has been seen 34384 times