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

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 title.

I used a script I found on R Cookbook which I tweaked a bit:

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"

This question is related to r class ggplot2

The answer is


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()

Examples related to r

How to get AIC from Conway–Maxwell-Poisson regression via COM-poisson package in R? R : how to simply repeat a command? session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium How to show code but hide output in RMarkdown? remove kernel on jupyter notebook Function to calculate R2 (R-squared) in R Center Plot title in ggplot2 R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph R multiple conditions in if statement What does "The following object is masked from 'package:xxx'" mean?

Examples related to class

String method cannot be found in a main class method Class constructor type in typescript? ReactJS - Call One Component Method From Another Component How do I declare a model class in my Angular 2 component using TypeScript? When to use Interface and Model in TypeScript / Angular Swift Error: Editor placeholder in source file Declaring static constants in ES6 classes? Creating a static class with no instances In R, dealing with Error: ggplot2 doesn't know how to deal with data of class numeric Static vs class functions/variables in Swift classes?

Examples related to ggplot2

Center Plot title in ggplot2 R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph Saving a high resolution image in R Change bar plot colour in geom_bar with ggplot2 in r Remove legend ggplot 2.2 Remove all of x axis labels in ggplot Changing fonts in ggplot2 Explain ggplot2 warning: "Removed k rows containing missing values" Error: package or namespace load failed for ggplot2 and for data.table In R, dealing with Error: ggplot2 doesn't know how to deal with data of class numeric