服务热线
178 0020 3020
summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE, conf.interval=.95, .drop=TRUE) {
library(plyr)
# 计算长度
length2 <- function (x, na.rm=FALSE) {
if (na.rm) sum(!is.na(x))
else length(x)
}
# 以 groupvars 为组,计算每组的长度,均值,以及标准差
# ddply 就是 dplyr 中的 group_by + summarise
datac <- ddply(data, groupvars, .drop=.drop,
.fun = function(xx, col) {
c(N = length2(xx[[col]], na.rm=na.rm),
mean = mean (xx[[col]], na.rm=na.rm),
sd = sd (xx[[col]], na.rm=na.rm)
)
},
measurevar
)
# 重命名
datac <- plyr::rename(datac, c("mean" = measurevar))
# 计算标准偏差
datac$se <- datac$sd / sqrt(datac$N) # Calculate standard error of the mean
# Confidence interval multiplier for standard error
# Calculate t-statistic for confidence interval:
# e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
# 计算置信区间
ciMult <- qt(conf.interval/2 + .5, datac$N-1)
datac$ci <- datac$se * ciMult
return(datac)
}
tg<-ToothGrowth
tgc<-summarySE(tg,measurevar = "len",groupvars = c("supp","dose"))
library("ggplot2")
pd<-position_dodge(0.1)
ggplot(tgc,aes(x=dose,y=len,colour=supp,group=supp))+geom_line(position = pd)+geom_errorbar(aes(ymin=len-sd,ymax=len+sd),colour="black",width=0.1,position = pd)+geom_point(position = pd,size=2,shape=21,fill="white")+xlab("Dose(mg)")+ylab("Tooth length")+scale_colour_hue(name="Supplement type",breaks=c("OJ","VC"),labels=c("Orange juice","Ascorbic acid"),l=40)+ggtitle("R2-30 Mean±Sd")+expand_limits(y=0)+scale_y_continuous(breaks = 0:20*4)+theme_bw()+theme(legend.justification = c(1,0),legend.position = c(1,0))
options(scipen = 100)
library("ggplot2")
theme_set(theme_bw())
data("midwest",package = "ggplot2")
ggplot(midwest,aes(x=area,y=poptotal))+geom_point(aes(col=state,size=popdensity))+geom_smooth(method="loess",se=T)+xlim(c(0,0.1))+ylim(c(0,500000))+labs(subtitle="Area VS population",x="Area",y="Population",title="R2-30",caption="Source:midwest")
library("ggplot2")
datar10<-read.csv(file.choose())
datar101<-as.matrix(datar10[,2:20])
row.names(datar101)<-datar10[,1]
heatmap(datar101,Rowv=NA,Colv=NA,scale="column",margins=c(5,10),col=colorRampPalette(c("white", "blue"))(256),main="R2-30")
附件