In R, dealing with Error: ggplot2 doesn't know how to deal with data of class numeric

前端 未结 1 727
悲&欢浪女
悲&欢浪女 2020-12-31 01:49

I\'m new to R and haven\'t done any programming before...

When I attempt to create a box chart with standard error bars I get the error message mentioned in the titl

相关标签:
1条回答
  • 2020-12-31 02:01

    The error happens because of you are trying to map a numeric vector to data in geom_errorbar: GVW[1:64,3]. ggplot only works with data.frame.

    In general, you shouldn't subset inside ggplot calls. You are doing so because your standard errors are stored in four separate objects. Add them to your original data.frame and you will be able to plot everything in one call.

    Here with a dplyr solution to summarise the data and compute the standard error beforehand.

    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 讨论(0)
提交回复
热议问题