Needed packages
library(ggplot2)
suppressWarnings(suppressMessages(library(ggplot2)))
Dataset from packages
data(package="ggplot2")
Load data
data1=ggplot2::mpg
str(data1)
## tibble [234 × 11] (S3: tbl_df/tbl/data.frame)
## $ manufacturer: chr [1:234] "audi" "audi" "audi" "audi" ...
## $ model : chr [1:234] "a4" "a4" "a4" "a4" ...
## $ displ : num [1:234] 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
## $ year : int [1:234] 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
## $ cyl : int [1:234] 4 4 4 4 6 6 6 4 4 4 ...
## $ trans : chr [1:234] "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
## $ drv : chr [1:234] "f" "f" "f" "f" ...
## $ cty : int [1:234] 18 21 20 21 16 18 18 18 16 20 ...
## $ hwy : int [1:234] 29 29 31 30 26 26 27 26 25 28 ...
## $ fl : chr [1:234] "p" "p" "p" "p" ...
## $ class : chr [1:234] "compact" "compact" "compact" "compact" ...
unique(data1$manufacturer);length(unique(data1$manufacturer))
## [1] "audi" "chevrolet" "dodge" "ford" "honda"
## [6] "hyundai" "jeep" "land rover" "lincoln" "mercury"
## [11] "nissan" "pontiac" "subaru" "toyota" "volkswagen"
## [1] 15
unique(data1$class)
## [1] "compact" "midsize" "suv" "2seater" "minivan"
## [6] "pickup" "subcompact"
unique(data1$year)
## [1] 1999 2008
Change character variables into
factors
data1=as.data.frame(unclass(data1),stringsAsFactors = TRUE)
User defined
n=nrow(data1)
rowind=seq(1,n)
Pointplot
ggplot(data1,aes(x=rowind,y=cty))

ggplot(data1,aes(x=rowind,y=cty))+geom_point()

ggplot(data1,
aes(x=rowind,y=cty))+geom_point(size=5,shape=14,color="blue")

ggplot(data1, aes(x=rowind,y=cty))+ geom_point(size=5)

ggplot(data1, aes(x=rowind,y=cty))+ geom_point(size=2,shape=4)

ggplot(data1, aes(x=rowind,y=cty))+ geom_point(color='red')

ggplot(data1, aes(x=rowind,y=cty))+
geom_point(size=2,shape=4,color="blue")

single_num_plot1=ggplot(data1, aes(x=rowind,y=cty))+
geom_point(size=1.75,shape=25,color="red")
single_num_plot1

single_num_plot2=single_num_plot1+ labs(title="Miles per gallon in
City",subtitle="From mpg dataset",y="Mileage in City",caption = "mpg
dataset")
single_num_plot2

single_num_plot3=single_num_plot2+theme(plot.title=element_text(color="orange",size=14,face="bold.italic"),
plot.subtitle =element_text(color="steelblue",size=15,face="bold"),
plot.caption=element_text(size=15,face="bold",color="blue3"),
axis.title.x=element_text(size=10,face="bold",color="green"),
axis.title.y=element_text(size=10,face="italic",color="red"))
single_num_plot3

single_num_plot3=single_num_plot2+
theme(plot.title=element_text(hjust=0.5,vjust=0.5,color="tomato",size=14,face="bold.italic"),
plot.subtitle =element_text(size=15,face="bold",hjust=0.5),
plot.caption=element_text(size=15,face="bold.italic",color="blue3"),
axis.title.x=element_text(vjust=10,hjust=0.1,size=10),
axis.title.y=element_text(size=10,angle=30),
axis.text.x=element_text(size=20,angle=35,vjust=.5),
axis.text.y=element_text(size=20))
single_num_plot3

single_num_plot4_y=single_num_plot3+ coord_cartesian(ylim=c(15,25))
single_num_plot4_y

single_num_plot4_x=single_num_plot3+ coord_cartesian(xlim=c(100,150))
single_num_plot4_x

single_num_plot4=single_num_plot3+
coord_cartesian(ylim=c(15,25),xlim=c(100,150))
single_num_plot4

single_num_plot5_x=single_num_plot3+scale_x_continuous(breaks=seq(0,120,60))
single_num_plot5_x

single_num_plot5_y=single_num_plot3+scale_y_continuous(breaks=seq(10,35,5))
single_num_plot5_y

single_num_plot5=single_num_plot3+scale_x_continuous(breaks=seq(0,120,60))+scale_y_continuous(breaks=seq(10,35,5))
single_num_plot5_a=single_num_plot3+
scale_x_continuous(breaks=seq(10,35,5), labels=letters[1:6])
single_num_plot5_a

single_num_plot5_c=single_num_plot3+
scale_x_continuous(breaks=seq(10,35,5), labels=LETTERS[1:6])
single_num_plot5_c

single_num_plot5_e=single_num_plot3+ scale_y_continuous(breaks=seq(10,
35, 5), labels = c("O","t","th","f","fi","b1"))+
scale_x_continuous(breaks=seq(1, 235, 30), labels =
c("A1","B2","33","ram", "robert","GO &","see", "rahim"))
single_num_plot5_e

Boxplot
ggplot(data1,aes(y=cty))+geom_boxplot()

ggplot(data1,aes(y=cty))+geom_boxplot(color="red",fill="yellow")

Histogram
ggplot(data1,aes(cty))+geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(data1,aes(cty))+geom_histogram(color="green",fill="yellow")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Density
ggplot(data1, aes(x=cty)) +
geom_density(color="red",size=2)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Dotplot
ggplot(data1,aes(x=cty))+geom_dotplot(dotsize=0.5,color="red",fill="yellow")
## Bin width defaults to 1/30 of the range of the data. Pick better value with
## `binwidth`.

Areaplot
ggplot(data1,aes(cty))+geom_area(stat="bin",color="red",fill="blue")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Barchart
ggplot(data1,aes(cty))+geom_bar(color="steelblue")

Donut Plot
ggplot(data1,
aes(x = 2, fill = class) ) +
geom_bar() +
coord_polar(theta = "y") +
scale_x_discrete("")+
theme(axis.ticks=element_blank(),
axis.title=element_blank(),
axis.text.y=element_blank(),
axis.text.x=element_blank(),
panel.grid = element_blank(),
legend.position = "bottom")+
labs(title = "CLASS DISTRIBUTION",
caption="Milage Per Gallon Data from ggplot2")+
xlim(0,2.5)
## Scale for x is already present.
## Adding another scale for x, which will replace the existing scale.

Pie Chart
ggplot(data1,
aes(x = factor(""), fill = class) ) +
geom_bar() +
coord_polar(theta = "y") +
scale_x_discrete("")+
theme(axis.ticks=element_blank(),
axis.title=element_blank(),
axis.text.y=element_blank(),
axis.text.x=element_blank(),
panel.grid = element_blank(),
legend.position = "bottom")+
labs(title = "CLASS DISTRIBUTION",
caption="Milage Per Gallon Data from ggplot2")

Scatterplot
ggplot(data1,aes(x=cty,y=hwy,col=displ))+geom_point()
