library(ggplot2)
library(gridExtra)
mtc <- mtcars
head(mtc)
ggplot(mtc, aes(x = factor(gear))) + geom_bar(stat = "bin")
##PART1 Aggregate data for barplot
#using aggregate
ag.mtc<-aggregate(mtc$wt, by=list(mtc$gear), FUN=mean)
ag.mtc
#using tapply
summary.mtc <- data.frame(
gear=levels(as.factor(mtc$gear)),
meanwt=tapply(mtc$wt, mtc$gear, mean))
summary.mtc
ggplot(summary.mtc, aes(x = factor(gear), y = meanwt)) + geom_bar(stat = "identity")
# 直接使用stat_summary函数
ggplot(mtc,aes(x=factor(gear), y=wt)) + stat_summary(fun.y=mean, geom="bar")
##PART2 Horizontal bars, colors, width of bars
#1. horizontal bars
p1<-ggplot(mtc,aes(x=factor(gear),y=wt)) + stat_summary(fun.y=mean,geom="bar") +
coord_flip()
#2. change colors of bars
p2<-ggplot(mtc,aes(x=factor(gear),y=wt,fill=factor(gear))) + stat_summary(fun.y=mean,geom="bar") +
scale_fill_manual(values=c("purple", "blue", "darkgreen"))
#3. change width of bars
p3<-ggplot(mtc,aes(x=factor(gear),y=wt)) + stat_summary(fun.y=mean,geom="bar", aes(width=0.5))
grid.arrange(p1, p2, p3, nrow=1)
ggplot(summary.mtc, aes(x = factor(gear), y = meanwt)) + geom_bar(stat = "identity", width=0.2)
##PART3 Split and color by another variable
#1. next to each other
p1<-ggplot(mtc,aes(x=factor(gear),y=wt,fill=factor(vs)), color=factor(vs)) +
stat_summary(fun.y=mean,position=position_dodge(),geom="bar")
#2. stacked
p2<-ggplot(mtc,aes(x=factor(gear),y=wt,fill=factor(vs)), color=factor(vs)) +
stat_summary(fun.y=mean,position="stack",geom="bar")
#3. with facets
p3<-ggplot(mtc,aes(x=factor(gear),y=wt,fill=factor(vs)), color=factor(vs)) +
stat_summary(fun.y=mean, geom="bar") +
facet_wrap(~vs)
grid.arrange(p1, p2, p3, nrow=1)
# how to reorder factors
mtc$vs2<-factor(mtc$vs, levels = c(1,0))
ggplot(mtc,aes(x=factor(gear),y=wt,fill=factor(vs2)), color=factor(vs2)) +
stat_summary(fun.y=mean,position="stack",geom="bar")
##PART4 Add text to the bars, label axes, and label legend
ag.mtc<-aggregate(mtc$wt, by=list(mtc$gear,mtc$vs), FUN=mean)
colnames(ag.mtc)<-c("gear","vs","meanwt")
ag.mtc
#1. basic
g1<-ggplot(ag.mtc, aes(x = factor(gear), y = meanwt, fill=factor(vs),color=factor(vs))) +
geom_bar(stat = "identity", position=position_dodge()) +
geom_text(aes(y=meanwt, ymax=meanwt, label=meanwt),position= position_dodge(width=0.9), vjust=-.5)
#2. fixing the yaxis problem, changing the color of text, legend labels, and rounding to 2 decimals
g2<-ggplot(ag.mtc, aes(x = factor(gear), y = meanwt, fill=factor(vs))) +
geom_bar(stat = "identity", position=position_dodge()) +
geom_text(aes(y=meanwt, ymax=meanwt, label=round(meanwt,2)), position= position_dodge(width=0.9), vjust=-.5, color="black") +
scale_y_continuous("Mean Weight",limits=c(0,4.5),breaks=seq(0, 4.5, .5)) +
scale_x_discrete("Number of Gears") +
scale_fill_discrete(name ="Engine", labels=c("V-engine", "Straight engine"))
grid.arrange(g1, g2, nrow=1)
##PART5 Add error bars or best fit line
summary.mtc2 <- data.frame(
gear=levels(as.factor(mtc$gear)),
meanwt=tapply(mtc$wt, mtc$gear, mean),
sd=tapply(mtc$wt, mtc$gear, sd))
summary.mtc2
ggplot(summary.mtc2, aes(x = factor(gear), y = meanwt)) +
geom_bar(stat = "identity", position="dodge", fill="lightblue") +
geom_errorbar(aes(ymin=meanwt-sd, ymax=meanwt+sd), width=.3, color="darkblue")
#summarize data
summary.mtc3 <- data.frame(
hp=levels(as.factor(mtc$hp)),
meanmpg=tapply(mtc$mpg, mtc$hp, mean))
#run a model
l<-summary(lm(meanmpg~as.numeric(hp), data=summary.mtc3))
#manually entering the intercept and slope
f1<-ggplot(summary.mtc3, aes(x = factor(hp), y = meanmpg)) +
geom_bar(stat = "identity", fill="darkblue")+
geom_abline(aes(intercept=l$coef[1,1], slope=l$coef[2,1]), color="red", size=1.5)
#using stat_smooth to fit the line for you
f2<-ggplot(summary.mtc3, aes(x = factor(hp), y = meanmpg)) +
geom_bar(stat = "identity", fill="darkblue")+
stat_smooth(aes(group=1),method="lm", se=FALSE, color="orange", size=1.5)
grid.arrange(f1, f2, nrow=1)
ggplot2常用画图示例
最新推荐文章于 2024-09-08 12:11:39 发布