1. Learn
  2. /
  3. Courses
  4. /
  5. Intermediate Data Visualization with ggplot2

Connected

Exercise

Log-transforming scales

Using scale_y_log10() and scale_x_log10() is equivalent to transforming our actual dataset before getting to ggplot2.

Using coord_trans(), setting x = "log10" and/or y = "log10" arguments, transforms the data after statistics have been calculated. The plot will look the same as with using scale_*_log10(), but the scales will be different, meaning that we'll see the original values on our log10 transformed axes. This can be useful since log scales can be somewhat unintuitive.

Let's see this in action with positively skewed data - the brain and body weight of 51 mammals from the msleep dataset.

Instructions 1/3

undefined XP
  • 1
    • Using the msleep dataset, plot the raw values of brainwt against bodywt values as a scatter plot.
  • 2
    • Add the scale_x_log10() and scale_y_log10() layers with default values to transform the data before plotting.
  • 3
    • Use coord_trans() to apply a "log10" transformation to both the x and y scales.