R2-35 第10期-综合案例

德先森 2018-01-08 18:12:52 阅读: 1098

1. 学习误差线的添加,看到网上有人写了一个function,大家可以保存用于以后自己的数据处理
## Gives count, mean, standard deviation, standard error of the mean, and confidence interval (default 95%).
##   data: a data frame.
##   measurevar: the name of a column that contains the variable to be summariezed
##   groupvars: a vector containing names of columns that contain grouping variables
##   na.rm: a boolean that indicates whether to ignore NA's
##   conf.interval: the percent range of the confidence interval (default is 95%)
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"))
ggplot(tgc, aes(x=dose, y=len, colour=supp))+geom_point()+geom_line()+ geom_errorbar(aes(ymin=len-se, ymax=len+se), width=.1)+ggtitle("R2-35")

ggplot(midwest,aes(x=area,y=poptotal))+geom_point(aes(col=state,size=popdensity))+geom_smooth(method='loess')+xlim(c(0,0.1))+ylim(c(0,500000))+ggtitle("R2-35")

library(ggplot2)
library(plyr)
require(plyr)
require(reshape2)
require(scales)
data<- read.csv("task3.csv",header = T)
data$Name <- with(data, reorder(Name, PTS))
nba.m <- melt(data)
data.m <- ddply(nba.m, .(variable), transform,rescale = rescale(value))
ggplot(data.m, aes(variable, Name)) + geom_tile(aes(fill = rescale),colour = "white") + scale_fill_gradient(low = "white",high = "blue")+theme(axis.text.x = element_text(angle = 45, hjust = 0.5, vjust = 0.5))

10-1.png10-2.png10-3.png

 
邀请讨论

附件

{{f.title}} 大小 {{f.file_size}} 下载 {{f.count_download}} 金币 {{f.count_gold}}
{{item.nick_name}} 受邀请回答 {{item.create_time}}
{{item.refer_comment.nick_name}} {{item.refer_comment.create_time}}

附件

{{f.title}} 大小 {{f.file_size}} 下载 {{f.count_download}} 金币 {{f.count_gold}}
切换到完整回复 发送回复
赞({{item.count_zan}}) 踩({{item.count_cai}}) 删除 回复 关闭
科研狗©2015-2024 科研好助手,京ICP备20005780号-1 建议意见

服务热线

178 0020 3020

微信服务号