15.2 Linear coordinate systems

  • coord_cartesian(): the default Cartesian coordinate system, where the 2d position of an element is given by the combination of the x and y positions.

  • coord_flip(): Cartesian coordinate system with x and y axes flipped.

  • coord_fixed(): Cartesian coordinate system with a fixed aspect ratio.


  1. coord_cartesian()
p1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 
  geom_point(aes(fill=Species),
             show.legend = F,
             shape=21,color="grey20",alpha=0.5) + 
  geom_smooth(color="pink") +
  theme_light()

p1 | p1 + scale_x_continuous(limits = c(5, 6)) | p1 + coord_cartesian(xlim = c(5, 6))

  1. coord_flip()
p2 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 
  geom_point(aes(fill=Species),
             show.legend = F,
             shape=21,color="grey20",alpha=0.5) + 
  geom_smooth(color="pink") +
  theme_light()

p3 <- ggplot(iris, aes(Sepal.Width,Sepal.Length)) + 
  geom_point(aes(fill=Species),
             show.legend = F,
             shape=21,color="grey20",alpha=0.5) + 
  geom_smooth(color="pink") +
  theme_light()

p2 | p2 + coord_flip() | p3

(the smooth is fit to the rotated data).

  1. coord_fixed()
p3 | p3 + coord_fixed()