library(datasets)
library(ggplot2)
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 3.4.4
Viz 1
mpg.plot <- ggplot(mpg) ## mpg dataset
mpg.plot + geom_bar(aes(class,fill = trans)) + scale_fill_discrete(name = "Transmission")
## stacked bar chart where X-asix = class and Legend/Title is transmission
Viz 2
mpg.plot + geom_boxplot(aes(manufacturer, hwy)) + theme_classic() + coord_flip() + labs(y = "Highway Fuel Efficiency (mile/gallon)",x = "Vehicle Manufacturer")
## Using box plot to find distribution of Highway Fuel Efficiency by Manufacturer
Viz 3
ggplot(diamonds) +geom_density(aes(price,fill = cut,color = cut),alpha = 0.2,size = 0.4) + labs(title = "Diamond Price Density",x = "Diamond Price (USD)",y = "Density") + theme_economist()
## finding density of diamond price for cuts (alpha for transparency, size for width of strokes)
Viz 4
ggplot(iris, aes(Sepal.Length, Petal.Length)) +geom_point() +
geom_smooth(method = lm) + theme_minimal() + theme(panel.grid.major = element_line(size = 1),panel.grid.minor = element_line(size = 0.7)) +labs(title = "Relationship between Petal and Sepal Length",x = "Iris Sepal Length",y = "Iris Petal Length")
## Scatter plot for relationship between petal length and sepal
## using geom_point for scatter plot
## using geom_smooth for regression
Viz 5
ggplot(iris,aes(Sepal.Length, Petal.Length,color = Species)) + ## Using colors to differentiate species
geom_point() + geom_smooth(method = lm, se = FALSE) +
## Regression line without confidence region
theme_pander() + ## Using the Pander theme
theme(text = element_text(family = "serif"),
## Using Times New Roman for all texts
axis.ticks = element_line(color = "black",
## tick marks black
size = 0.7),
## tick marks size
legend.position = "bottom", ## legend at bottom of plot
legend.title = element_text(face = "plain"),
## Legend title plain italic
plot.title = element_text(size = 14,
## font size of plot title
face = "plain")) +
## font of plot title plain
labs(title = "Relationship between Petal and Sepal Length",
## title
subtitle = "Species level comparison",
## subtitle
x = "Iris Sepal Length", ## Assign x-axis title
y = "Iris Petal Length") ## Assign y-axis title
## using scatter plot with regressionn line without confidence region to find relationship between ptal length and sepal for different species