找不到对象但存在ggplot2

object not found but exists ggplot2

我正在尝试制作一个带有误差条的条形图,我还想添加字母标签以指示各组之间在条形图上的统计显着性。但是,当我添加标签代码时,我收到以下消息:

Error in eval(expr, envir, enclos) : object 'treatment.area' not found

这是我用来生成图表的代码:

# This data frame calculates statistics for each treatment.
data.summary.area <- data.frame(treatment.area = levels(shapes.a$Exposure),
  mean.a=tapply(shapes$Area, shapes$Exposure, mean),
  n.a=tapply(shapes$Area, shapes$Exposure, length),
  sd.a=tapply(shapes$Area, shapes$Exposure, sd))

# Precalculate standard error of the mean (SEM)
data.summary.area$sem.a <- data.summary.area$sd.a/sqrt(data.summary.area$n.a)

# Precalculate margin of error for confidence interval
data.summary.area$me <- qt(1-alpha/2, df=data.summary.area$n.a)*data.summary.area$sem
#Get some stats
 label.area.cor <- data.frame(Group=c("CON CCCP", "CON DMSO", "HF CCCP", "HF DMSO"),
Value = c(0.15, 0.18, 0.16, 0.25))
# Make the plot
require(ggplot2)

png('barplot-sem-mito.area.lab.stat.png')
ggplot(data.summary.area, aes(x = treatment.area, y = mean.a)) +  
geom_bar(position = position_dodge(), stat="identity", 
        fill=c('red', 'red4', 'forestgreen', 'darkgreen')) + 
geom_errorbar(aes(ymin=mean.a-sem.a, ymax=mean.a+sem.a)) +
ggtitle("Mitochondria Area after 24h CCCP exposure, GV oocytes") +   xlab("Exposure")+ 
   ylab(expression(paste("Area, u"^"2"))) +
   theme_bw()+  geom_text(data = label.area.cor, 
                      aes(label=c("a", "b", "c", "d")))+
   theme(panel.grid.major = element_blank())
dev.off()

所以看到是一个下雨的下午,我这样做了:

set.seed(1234)
n <- 50
shapes <- data.frame(Exposure=sample(c("CON CCCP","CON DMSO","HF CCCP","HF DMSO"),n,
                                       replace=T),Area=runif(n,0,10))

data.summary.area <- data.frame(treatment.area = levels(shapes$Exposure),
                                mean.a=tapply(shapes$Area, shapes$Exposure, mean),
                                n.a=tapply(shapes$Area, shapes$Exposure, length),
                                sd.a=tapply(shapes$Area, shapes$Exposure, sd))

alpha <- 0.05

# Precalculate standard error of the mean (SEM)
data.summary.area$sem.a <- data.summary.area$sd.a/sqrt(data.summary.area$n.a)

# Precalculate margin of error for confidence interval
data.summary.area$me <- qt(1-alpha/2, df=data.summary.area$n.a)*data.summary.area$sem.a
#Get some stats
label.area.cor <- data.frame(Group=c("CON CCCP","CON DMSO","HF CCCP","HF DMSO"),
                             Value = c(0.15, 0.18, 0.16, 0.25))
# Make the plot
require(ggplot2)

#png('barplot-sem-mito.area.lab.stat.png')
ggplot(data.summary.area, aes(x = treatment.area, y = mean.a))  +
   geom_bar(position = position_dodge(), stat="identity", 
            fill=c('red', 'red4', 'forestgreen', 'darkgreen')) +
   geom_errorbar(aes(ymin=mean.a-sem.a, ymax=mean.a+sem.a)) +
   ggtitle("Mitochondria Area after 24h CCCP exposure, GV oocytes") +   
   xlab("Exposure") +
   ylab(expression(paste("Area, u^2"))) + 
   theme_bw()  +
   geom_text(data=label.area.cor,aes(x=Group,y=Value,label=c("a","b","c","d")),size=8)+
   theme(panel.grid.major = element_blank()) 

#dev.off()

产生这个: