Skip to content

Instantly share code, notes, and snippets.

@blahah
Created June 8, 2017 15:04
Show Gist options
  • Save blahah/63fb0221cf31f72bc1fed0a41ee3ef22 to your computer and use it in GitHub Desktop.
Save blahah/63fb0221cf31f72bc1fed0a41ee3ef22 to your computer and use it in GitHub Desktop.
ggplot2 data exploration
library(ggplot2)
install.packages('hexbin')
cars <- mtcars
cars$car <- rownames(mtcars)
cars <- cars[with(cars, order(hp)), ]
cars$car <- factor(cars$car, levels = cars$car)
cars$cyl[cars$cyl == 4] <- '4 cylinders'
cars$cyl[cars$cyl == 6] <- '6 cylinders'
cars$cyl[cars$cyl == 8] <- '8 cylinders'
cars$cyl
cars$cyl <- factor(cars$cyl)
cars$cyl <- factor(cars$cyl, levels = rev(levels(cars$cyl)))
ggplot(cars, aes(y = car, x = hp, colour = cyl)) +
geom_point() +
facet_grid(cyl~., scales = "free_y", space = "free_y") +
theme_bw() +
guides(colour = FALSE) +
ggtitle('Effect of no. cylinders on horsepower')
ggplot(cars, aes(x = mpg, y = hp)) + geom_point() + stat_smooth()
ggplot(cars, aes(x = mpg, fill = cyl)) +
geom_density(alpha = 0.5)
ggplot(cars, aes(y = mpg, x = cyl, fill = cyl)) +
geom_violin() +
geom_point() +
geom_jitter() +
guides(fill = FALSE)
ggplot(cars, aes(y = mpg, x = cyl, fill = cyl)) +
geom_boxplot() +
guides(fill = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment