Visualization 1.
library(ggplot2)
library("ggthemes")
theme_set(theme_classic())
# Histogram on a Categorical variable
g <- ggplot(mpg, aes(class))
g + geom_bar(aes(fill=trans), width = 0.5) +
theme(axis.text.x = element_text(angle=65, vjust=0.6)) +
labs(title="Histogram on Categorical Variable",
subtitle="Manufacturer across Vehicle Classes")
Visualization 2.
mpg.plot <- ggplot(mpg)
mpg.plot + ## Plot for mpg dataset
geom_boxplot(aes(manufacturer, hwy)) +
## Use box plot to find distribution of Highway Fuel Efficiency by Manufacturer
theme_classic() + ## Use the classic theme
coord_flip() + ## Flip coordinate
labs(y = "Highway Fuel Efficiency (mile/gallon)",
## Assign y-axis (horizontal) title
x = "Vehicle Manufacturer") ## Assign x-axis (vertical) title
Visualization 3.
ggplot(diamonds) + ## Create plot for diamonds dataset
geom_density(aes(price, ## Find density of diamond price
fill = cut, ## Legend is cut (quality of the cut). Use fill colors to differentiate
color = cut), ## Legend is cut (quality of the cut). Use stroke colors to differentiate too
alpha = 0.3, ## Set transparency level of fill color
size = 0.6) + ## Set width of strokes
labs(title = "Diamond Price Density",
## Assign plot title
x = "Diamond Price (USD)", ## Assign x-axis title
y = "Density") + ## Assign y-axis title
theme_economist() ## Use the theme used by Economist magazine
Visualization 4.
ggplot(iris, ## Create plot for iris dataset
aes(Sepal.Length, Petal.Length)) +
## X-axis is sepal length; y-axis is patel length
geom_point() + ## Use scatter plot
geom_smooth(method = lm) + ## Draw regression line
theme_minimal() + ## Use the "minimal" theme
theme(panel.grid.major = element_line(size = 1),
## Set width of major grid line
panel.grid.minor = element_line(size = 0.7)) +
## Set width of minor grid line
labs(title = "Relationship between Petal and Sepal Length",
## Assign plot title
x = "Iris Sepal Length", ## Assign x-axis title
y = "Iris Petal Length") ## Assign y-axis title
Visualization 5.
ggplot(iris, ## Create plot for iris dataset
aes(Sepal.Length, ## Set x-axis: sepal length
Petal.Length, ## Set y-axis: petal length
color = Species)) + ## Set legend: species. Use colors to differentiate.
geom_point() + ## Use scatter plot
geom_smooth(method = lm, se = FALSE) +
## Draw regression line without confidence region
theme_pander() + ## Use the Pander theme
theme(text = element_text(family = "serif"),
## Use Times New Roman for all texts
axis.ticks = element_line(color = "black",
## Set color of tick marks to be black
size = 0.7),
## Set size of tick marks
legend.position = "bottom", ## Move legend to the bottom of plot
legend.title = element_text(face = "plain"),
## Legend title was italic. Set to plain.
plot.title = element_text(size = 14,
## Set font size of plot title
face = "plain")) +
## Set font of plot title to be plain
labs(title = "Relationship between Petal and Sepal Length",
## Assign plot title
subtitle = "Species level comparison",
## Assign plot subtitle
x = "Iris Sepal Length", ## Assign x-axis title
y = "Iris Petal Length") ## Assign y-axis title