Data Visualization With Ggplot2: Install Packages
Data Visualization With Ggplot2: Install Packages
Install packages
# remove comment for the first time
# install.packages( "ggplot2" )
# install.packages( "titanic" )
View(titanic.df)
#for( n in names(titanic.df) )
# print( paste( n, class( titanic.df[,n] ) ) )
library(ggplot2)
Bar plot
1. Survival rates
ggplot( data = titanic.df, aes( x = Survived ) ) +
geom_bar()
8. Survival rate by gender, but split barplot by Passenger class, bars next to each
other
ggplot( data = titanic.df, aes( x = Sex, fill = Survived ) ) +
geom_bar( position = "dodge" ) +
facet_wrap( ~ Pclass )
Histograms, boxplots, density plots
1. Age distribution
ggplot( data = titanic.df, aes( x = Age ) ) +
geom_histogram( binwidth = 5 )
Pie chart
ggplot( data = titanic.df, aes( x = factor(1), fill = Survived ) ) +
geom_bar( position="fill" ) +
facet_grid( Pclass ~ Sex ) +
coord_polar( theta = "y" )
Themes
ggplot( data = titanic.df, aes( x = Age, y = Fare, color = Survived, size = N
umRelatives ) ) +
geom_point( alpha = 0.5 ) +
theme_minimal() #