First remove all prelodaed data

rm(list=ls())

First, you have to tell which data you use Second, is aesthetic(aes) of ggplot where you tell what is y and what is x) In aesthetic you can select color and other beautification) There are geom where you inform what type of graph you wish (point, line, histogram,…) and fine tuning aspects

Load ggplot2 library

& Start first layer of plot — a blank plot

library(ggplot2)
m<-ggplot(data=mtcars, aes(x=wt, y=mpg)) 
m

# You can now use the 2nd layer of ggplot -a scatter plot # Also additional layers, of graph title, & axis labeling

n<-m+geom_point()
n+ggtitle("Scatter plot of Weight by mpg")+ 
  ylab("Mile per gallon") + xlab("Weights in lbs ")

## You can change color add title & axis labes in the graph with ## pre defined R object (here, plot m)

m    + geom_point(aes(wt, mpg), color="red")+
  ggtitle("Spread of weight and mpg")+
  xlab("weight of cars")+ylab("milage per gallon") 

# You can change what type of plot you need, ### such as ‘line of best fit’

m+geom_point()+geom_smooth(method=lm) 

# or add smooth LOWESS curve

m+geom_point()+geom_smooth() # lowess curve fit
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

# You can save graphic in PDF, jpeg or other format

ggsave("myscatter1.pdf")
## Saving 7 x 5 in image
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Create gradient scatter plot

sp3<-ggplot(mtcars, aes(x=wt, y=mpg, color=mpg)) + geom_point()
sp3

# Gradient with ranbow colors

sp3+scale_color_gradientn(colours = rainbow(5))

# Graphing One dimensional continuous variable # First let us generate a categorical data of 100 male & 100 female, named ‘df’

set.seed(1234)
df <- data.frame(
  sex=factor(rep(c("F", "M"), each=100)),
  weight=round(c(rnorm(100, mean=55, sd=5),
                 rnorm(100, mean=65, sd=5)))
)
head(df)
##   sex weight
## 1   F     49
## 2   F     56
## 3   F     60
## 4   F     43
## 5   F     57
## 6   F     58
table(df)
##    weight
## sex 43 44 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
##   F  1  1  1  1  4  9  8  7  9 11  9  6  4  4  7  2  4  1  3  2  2  1  1
##   M  0  0  0  0  0  0  0  2  0  0  0  2  1  2  0  4  2  8  7  9  7 10  9
##    weight
## sex 67 68 69 70 71 72 73 74 75 76 80
##   F  1  1  0  0  0  0  0  0  0  0  0
##   M  7  8  4  4  1  4  3  2  2  1  1

Area curve seems strange

c<-ggplot(df,aes(x=weight))
c+geom_area(stat="bin",fill="red", color="blue")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# Density curve shows two bumps for Male and Female

j<-c+geom_density()
j

# You can Change line color and fill color

j+   geom_density(color="darkblue", fill="lightblue")

# Adding mean line for total weight

j+ geom_vline(aes(xintercept=mean(weight)),
              color="blue", linetype="dashed", size=1)

# Change line type

c+  geom_density(linetype="dashed")

# to calculate mean of each group, use library(plyr)

library(plyr)   
mu <- ddply(df, "sex", summarise, grp.mean=mean(weight))
head(mu)
##   sex grp.mean
## 1   F     54.2
## 2   M     65.2

Now you can make 2 separet density plots for two groups

m<-ggplot(df, aes(x=weight, color=sex)) +
  geom_density()
m

# Can change legend position or NO legend

m + theme(legend.position="top")

m + theme(legend.position="bottom")

m + theme(legend.position="none") 

# Add mean lines

m+  geom_density()+
  geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),
             linetype="dashed")+ theme(legend.position="none")

# Histogram & density plot, overlapped

h<-ggplot(df, aes(x=weight)) + 
  geom_histogram(aes(y=..density..))
h
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

h+  geom_density(alpha=.3, fill="#FF6666") 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(df, aes(x=weight)) + 
  geom_histogram(aes(y=..density..), colour="black", fill="white")+
  geom_density(alpha=.2, fill="#FF6666") 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# Color by groups

ggplot(df, aes(x=weight, color=sex, fill=sex)) + 
  geom_histogram(aes(y=..density..), alpha=0.5,
position="identity")+
  geom_density(alpha=.2) 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

k<-ggplot(df, aes(x=weight))+
  geom_density()+facet_grid(sex ~ .)
k

# Add mean lines
k+geom_vline(data=mu, aes(xintercept=grp.mean, color="red"),
             linetype="dashed")

#bar plots of data diamond
ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut))

ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, color = cut))

ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, fill = cut))

#time series plot

t <- ggplot(data = economics, aes(x = date, y = unemploy))
t <- t + geom_line()
t

t1 <- t + geom_smooth(method = loess, span=1)
t1

# Histogram of height

data(singer, package="lattice")
ggplot(singer, aes(x=height)) + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(singer, aes(x=voice.part, y=height)) + geom_boxplot()

ggplot(mtcars, aes(x=wt, y=mpg, size=disp)) +
  geom_point(shape=21, color="black", fill="cornsilk")

boxplot(mpg~ gear*carb, data=mtcars)