Skip to content

Instantly share code, notes, and snippets.

@mattsigal
Created October 21, 2013 18:55
Show Gist options
  • Save mattsigal/7089000 to your computer and use it in GitHub Desktop.
Save mattsigal/7089000 to your computer and use it in GitHub Desktop.
Simple example of transforming data/scale/coordinate system in ggplot2.
library(ggplot2)
# Sample diamonds dataframe:
didat <- diamonds[sample(nrow(diamonds), 1000), ]
# Original data:
ggplot(didat, aes(x = depth, y = price)) + geom_point()
# Transform data:
ggplot(didat, aes(x = log10(depth), y = log10(price))) + geom_point()
# Transform scales (same plot visually as transform data, but scales go wonky):
ggplot(didat, aes(x = depth, y = price)) + geom_point() +
scale_x_log10() + scale_y_log10()
# Transform coordinate system:
ggplot(didat, aes(x = depth, y = price)) + geom_point() +
coord_trans(x = "log10", y = "log10")
@mattsigal
Copy link
Author

Visual summary provided at http://rpubs.com/mattsigal/ggtransforms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment