This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
# adding line of best fit & LOWESS fit
m+geom_point()+geom_smooth(method=lm)
m+geom_point()+geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(mtcars, aes(x=wt, y=mpg, size=disp)) +
geom_point(shape=21, color="black", fill="cornsilk")
ggsave("myscatter1.pdf")
## Saving 7 x 5 in image
# is saved in my 'document' folder
#gradient scatter plot
sp3<-ggplot(mtcars, aes(x=wt, y=mpg, color=mpg)) + geom_point()
sp3
# Gradient between n colors
sp3+scale_color_gradientn(colours = rainbow(5))
#one dimensional continuous variable
#generating a categorical data of 100male & 100female
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)))
)
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
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`.
j<-c+geom_density()
j
# Change line color and fill color
ggplot(df, aes(x=weight))+
geom_density(color="darkblue", fill="lightblue")
#adding mean line
j+ geom_vline(aes(xintercept=mean(weight)),
color="blue", linetype="dashed", size=1)
# Change line type
ggplot(df, aes(x=weight))+
geom_density(linetype="dashed")
#calculate mean of each group
library(plyr) #'plyr' to calculate mean by groups
mu <- ddply(df, "sex", summarise, grp.mean=mean(weight))
head(mu)
## sex grp.mean
## 1 F 54.2
## 2 M 65.2
# Change density plot line colors by groups
ggplot(df, aes(x=weight, color=sex)) +
geom_density()
# Add mean lines
p<-ggplot(df, aes(x=weight, color=sex)) +
geom_density()+
geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),
linetype="dashed")
p
#change legend position
p + theme(legend.position="top")
p + theme(legend.position="bottom")
p + theme(legend.position="none")#Removelegend
# 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
data(singer, package="lattice")
ggplot(singer, aes(x=height)) + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# bOX PLOTS
ggplot(singer, aes(x=voice.part, y=height)) + geom_boxplot()
boxplot(mpg~ gear*am, data=mtcars)