2015-04-29 31 views
14

我是新来的R和之前的任何程序还没有完成......在R,处理错误:GGPLOT2不知道如何处理数字类的数据

当我试图创建带有标准错误栏的框图我收到标题中提到的错误消息。

我使用的脚本我R上菜谱发现了我调整了一下:

ggplot(GVW, aes(x="variable",y="value",fill="Genotype")) + 
    geom_bar(position=position_dodge(),stat="identity",colour="black", size=.3)+ 
    geom_errorbar(data=GVW[1:64,3],aes(ymin=value-seSKO, ymax=value+seSKO), size=.3, width=.2, position=position_dodge(.9))+ 
    geom_errorbar(data=GVW[65:131,3],aes(ymin=value-seSWT, ymax=value+seSWT), size=.3, width=.2, position=position_dodge(.9))+ 
    geom_errorbar(data=GVW[132:195,3],aes(ymin=value-seEKO, ymax=value+seEKO), size=.3, width=.2, position=position_dodge(.9))+ 
    geom_errorbar(data=GVW[196:262,3],aes(ymin=value-seEWT, ymax=value+seEWT), size=.3, width=.2, position=position_dodge(.9))+ 
    xlab("Time")+ 
    ylab("Weight [g]")+ 
    scale_fill_hue(name="Genotype", breaks=c("KO", "WT"), labels=c("Knock-out", "Wild type"))+ 
    ggtitle("Effect of genotype on weight-gain")+ 
    scale_y_continuous(breaks=0:20*4) + 
    theme_bw() 

Data<- data.frame(
    Genotype<- sample(c("KO","WT"), 262, replace=T), 
    variable<- sample(c("Start","End"), 262, replace=T), 
    value<- runif(262,20,40) 
) 
names(Data)[1] <- "Genotype" 
names(Data)[2] <- "variable" 
names(Data)[3] <- "value" 

回答

18

错误发生,因为你试图在geom_errorbar映射数字矢量dataGVW[1:64,3]ggplot仅适用于data.frame

一般情况下,您不应该在ggplot调用子集内。你这样做是因为你的标准错误存储在四个独立的对象中。将它们添加到您的原始data.frame,您将能够在一次通话中绘制所有内容。

此处使用dplyr解决方案来预先汇总数据并计算标准错误。

library(dplyr) 
d <- GVW %>% group_by(Genotype,variable) %>% 
    summarise(mean = mean(value),se = sd(value)/sqrt(n())) 

ggplot(d, aes(x = variable, y = mean, fill = Genotype)) + 
    geom_bar(position = position_dodge(), stat = "identity", 
     colour="black", size=.3) + 
    geom_errorbar(aes(ymin = mean - se, ymax = mean + se), 
     size=.3, width=.2, position=position_dodge(.9)) + 
    xlab("Time") + 
    ylab("Weight [g]") + 
    scale_fill_hue(name = "Genotype", breaks = c("KO", "WT"), 
     labels = c("Knock-out", "Wild type")) + 
    ggtitle("Effect of genotype on weight-gain") + 
    scale_y_continuous(breaks = 0:20*4) + 
    theme_bw() 
+0

非常感谢你Scoa,数据现在按预期打印。虽然,其中一个淘汰列失踪,但我应该能够解决这个问题。 – embacify