Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
library(ggplot2) data(mpg) data(diamonds) # geom_bar — counts automatically ggplot(mpg, aes(x = class)) + geom_bar(fill = "steelblue") + labs(title = "Car count by class") # Grouped bar chart ggplot(mpg, aes(x = class, fill = drv)) + geom_bar(position = "dodge") + labs(title = "Class by drive type (dodged)") # Stacked proportions ggplot(mpg, aes(x = class, fill = drv)) + geom_bar(position = "fill") + labs(y = "Proportion") # Histogram ggplot(mpg, aes(x = hwy)) + geom_histogram(binwidth = 2, fill = "coral", colour = "white") + labs(title = "Distribution of highway MPG") # Density plot ggplot(mpg, aes(x = hwy, fill = drv)) + geom_density(alpha = 0.4) + labs(title = "Highway MPG density by drive type") # Boxplot with jittered points ggplot(mpg, aes(x = class, y = hwy)) + geom_boxplot(outlier.shape = NA) + geom_jitter(width = 0.2, alpha = 0.4, colour = "steelblue") + labs(title = "Highway MPG by car class") # facet_wrap ggplot(mpg, aes(x = displ, y = hwy)) + geom_point(alpha = 0.5) + facet_wrap(~ class, nrow = 2) + labs(title = "MPG vs displacement per class") # facet_grid (row by cyl, col by drv) ggplot(mpg, aes(x = displ, y = hwy)) + geom_point(alpha = 0.5) + facet_grid(cyl ~ drv) + labs(title = "Grid facet: cylinders x drive type")
Result
Open