Help with graphing ANOVA results in R
1
0
Entering edit mode
9.9 years ago
sammysun • 0

Hi, I'm really new to using statistics and R and I need help with how to visualize my ANOVA results in a bar graph with error bars. I'm trying to follow this tutorial but I keep getting the error message: Error: 'names' attribute [20] must be the same length as the vector [1]

Here is my code:

data$group <- "NA" # make an empty column for the groups
data$group[1:4]<-"volant"
data$group[6:16]<-"terrestrial"
data$group[17:19]<-"semi-aquatic"
data$group[20] <- "aquatic"
cbind(data)

Group<-as.factor(data$group)
Species<-rownames(data)

x <- (data$functional_nonfunctional~data$habitat)
x<-data$functional_nonfunctional
Group<-data$habitat
names(x)<-rownames(data)
names(Group)<-rownames(data)

modelPHYLO<-aov.phylo(x~Group,tree)
summary(modelPHYLO)
var(data$functional_nonfunctional)
length(Group)
graph_summary <- ddply(data,Group, summarize, AVERAGE=mean(data$functional_nonfunctional), SE=sqrt(var(data$functional_nonfunctional)/length(functional_nonfunctional)))

I would SO appreciate any help! I'm feeling really stuck! Thank you

ANOVA • 8.6k views
ADD COMMENT
0
Entering edit mode

It would help if you added a little of what your data looked like, even just the first few lines of your data.frame. Use:

head(data)

Also there are some inconsistencies in your code such as row 5 is not given a group (e.g. is it volant, terrestrial or other?) also you reassign the name Group and x to different objects which is confusing.

ADD REPLY
0
Entering edit mode

Oh wow I didn't even notice the row 5 thing thank you! The way that x is assigned to different things is confusing and doesn't make sense. The first x (x <- (data$functional_nonfunctional~data$habitat)) shouldn't be there now that I'm looking at it again.

Here is part of my table:

                   Olfactory_Bulb OlfactoryEp_Total Olfactory_resp functional_nonfunctional habitat 
Anoura_geofroyi                2.41              <NA>           <NA>                 9.985155       1
ADD REPLY
1
Entering edit mode
9.9 years ago
gtho123 ▴ 260

I am unsure where the error occurred however if I understand you correctly you want to perform an ANOVA on your functional_nonfunctional column using the different habitats as factors. While I am unable to use the aov.phylo function as I don't have the phylogenetic information I think I can help you with graphing it.

I created some fake data as a demo to show you how to draw the graph I think you want.

data <- data.frame(Olfactory_Bulb = NA, OlfactoryEp_Total = NA, Olfactory_resp = NA,
                   functional_nonfunctional = c(rnorm(5, 10, 5), rnorm(11, 20, 5), rnorm(3, 30, 5),rnorm(1, 40, 5)),habitat = c(rep(1,5), rep(2,11), rep(3,3), rep(4,1)))

rownames(data) <- c("Anoura_aequatoris", "Anoura_cadenai", "Anoura_canishina", "Anoura_caudifera", "Anoura_cultrata", "Anoura_fistulata", "Anoura_geoffroyi", "Anoura_latidens", "Anoura_luismanueli", "Anoura_peruana", "Choeroniscus_godmani", "Choeroniscus_periosus", "Choeroniscus_minor", "Choeronycteris_mexicana", "Glossophaga_commissarisi", "Glossophaga_leachii", "Glossophaga_longirostris", "Glossophaga_morenoi", "Glossophaga_soricina", "Hylonycteris underwoodi")

This code then creates the graph:

library("plyr")
library("ggplot2")


graph_summary <- ddply(data, c("habitat"), summarize,
                       AVERAGE=mean(functional_nonfunctional),
                       SE=sqrt(var(functional_nonfunctional)/length(functional_nonfunctional)))

ggplot(data = graph_summary, aes(x = habitat, y = AVERAGE, colour = habitat))+
  geom_point()+
  geom_errorbar(aes(ymax=AVERAGE+SE, ymin=AVERAGE-SE))+
  theme(axis.text.x = element_text(angle = 90, hjust = 0, size=11),
       axis.title.x = element_text(size=14),
       axis.title.y = element_text(angle = 90, size=14))+
  scale_x_discrete("Habitat")+
  scale_y_continuous("Functional/Nonfunctional")

This gives this graph:

Note: habitat 4 has only one individual so no error bars.

ADD COMMENT
0
Entering edit mode

thank you so much!!!

ADD REPLY

Login before adding your answer.

Traffic: 1960 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6