Library Pkg
library(ggplot2)
Working dataset
data(mpg)
1 point Chart
(x-axsis = scaler , y-axsis= scaler ,col = catagorigal)
For Relationship
ggplot(data=mpg ,mapping = aes(x = cty, y= hwy , col = manufacturer))+
geom_point()+
labs(title="cars dataset",
x= "city",
y="honda",
col="Manufacturer")
2 Boxplot
(x-axsis = cat, y-axsis = numatic)
For Distribution of Numaric
library(ggplot2)
data(mpg)
ggplot(mpg, aes(x = class, y = hwy)) +
geom_boxplot(fill = "#69b3a2") +
labs(title = "Highway MPG by Car Class",
x = "Car Class",
y = "Highway MPG")
3 Point Plot
(x-axsis = scaler , y-axsis= scaler ,col = catagorigal)
For Relationship
library(ggplot2)
data(mpg)
ggplot(mpg, aes(x = cty, y = hwy)) +
geom_point(alpha=2,color = "#D55E00") +
labs(title = "City vs. Highway MPG",
x = "City MPG",
y = "Highway MPG")
4 Barplot/countplot
(x-axsis=cat, y=count of cat)
For comparison
library(ggplot2)
data(mpg)
ggplot(mpg, aes(x = manufacturer)) +
geom_bar(fill = "#0072B2") +
labs(title = "Count of Cars by Manufacturer",
x = "Manufacturer",
y = "Count")
5 LinePlot
(x-axsis should be time related y-axsis should be any numaric)
used where time involved
library(ggplot2)
data(mpg)
ggplot(mpg, aes(x = year, y = cty, group = manufacturer, color = manufacturer)) +
geom_line() +
labs(title = "City MPG Over Time by Manufacturer",
x = "Year",
y = "City MPG")
6 Heatmap
(both axsis should be scaler)
For correlation
library(ggplot2)
data(mpg)
ggplot(mpg, aes(x = model, y = trans, fill = hwy)) +
geom_tile() +
labs(title = "Average Highway MPG by Car Model and Transmission Type",
x = "Car Model",
y = "Transmission Type",
fill = "Highway MPG")
7 MultiBarChart
For Comparison
library(ggplot2)
data(mpg)
ggplot(mpg, aes(x = class, fill = fl)) +
geom_bar() +
labs(title = "Fuel Type by Car Class",
x = "Car Class",
y = "Count",
fill = "Fuel Type")
scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07","#0072B2","#69b3a2"))
8 Dot plot
(tipically x axsis for cat and y axsis for num)
For distribution of numaric
library(ggplot2)
data(mpg)
ggplot(mpg, aes(x = class, y = hwy, color = trans)) +
geom_dotplot(binaxis = "y", stackdir = "center") +
labs(title = "Highway MPG by Car Type and Transmission Type",
x = "Car Type",
y = "Highway MPG",
color = "Transmission Type") +
scale_color_manual(values = c("#0072B2","#00AFBB", "#E7B800", "#FC4E07","#0072B2","#69b3a2","#D55E00","#FF0000","#A52A2A","#008080"))
9 Histogram
(x-axsis should be scaler)
For distribution of data
library(ggplot2)
data(mpg)
ggplot(mpg, aes(x = cty, fill = class)) +
geom_histogram(aes(y = ..density..), binwidth = 2, color = "black") +
geom_density(alpha = 0.2) +
scale_fill_brewer(palette = "Set1") +
labs(title = "Distribution of City Mileage by Car Class",
x = "City Mileage",
y = "Density",
fill = "Car Class")+
theme_minimal()