Table of Contents
Data Visualization with ggplot2 in R: Comprehensive Guide and Examples
For both histogram and density plot is very much useful for single varivable. Before woring on plot please see related data on which we are going to working.
# Installation
install.packages("ggplot2")
require(ggplot2)
data(diamonds)
class(diamonds)
# understand the diamonds data set
help(diamonds)
# see first 6 rows how its looks like
head(diamonds)
Plot histograms and densities with ggplot2
ggplot(data = diamonds) + geom_histogram( aes(x = carat) )
ggplot(data = diamonds) + geom_histogram( aes(x = carat), binwidth = .1 )
ggplot(data = diamonds) + geom_histogram( aes(x = carat), binwidth = .1 )
densities plot
ggplot(data = diamonds) + geom_density(aes(x = carat))
ggplot(data = diamonds) + geom_density(aes(x = carat), fill= "grey50")
Make scatterplots with ggplot2
ggplot(diamonds, aes(x = carat, y = price)) + geom_point()
You can do that above this in much flexiable manner like below for code reuseability
g
Color code the plot according to the color of the diamonds
g + geom_point(aes(color=color))
g + geom_point(aes(color=color, shape = cut))
You can see clearly the plot using Zoom
Make boxplots and violin plots with ggplot2
Box plot does not x axis
g
Based on labels
g
violin plots
g
Layer Overlaping
g
Change the oreder of layers
g
Using geom_jitter
g