R geom_行失败:参数表示行数不同

R geom_行失败:参数表示行数不同,r,ggplot2,R,Ggplot2,我正试图将“重要性条”添加到我的条形图中,如下所示: 然而,当我试图在数据上重新生成它时,我得到以下错误:。。。参数表示不同的行数:6,0 请注意,只要最后不包括geom_线,一切都可以工作 数据和代码 # Data means cat 1 296.1610 A 2 235.9618 B 3 220.6905 C 4 127.

我正试图将“重要性条”添加到我的条形图中,如下所示:

然而,当我试图在数据上重新生成它时,我得到以下错误:
。。。参数表示不同的行数:6,0

请注意,只要最后不包括geom_线,一切都可以工作

数据和代码

# Data
          means            cat
1         296.1610            A
2         235.9618            B
3         220.6905            C
4         127.2546            D

# Df for the new line
df1 <- data.frame(a = c(1, 1:4,4), b = c(349, 350, 350, 350, 350, 349))

# Plot
library(ggplot2)    

p1<-ggplot(df,aes(y=means,x=cat,fill=cat))
p1 + geom_bar(stat="Identity",width = 0.75) 
+ labs(y="Mean") 
+ geom_errorbar(aes(ymin=means-sd,ymax=means+sd),width=.2,position = position_dodge(.9)) 
+ theme(legend.position = "none", axis.text.x=element_text(family = "Arial",size=16, face = "bold"),axis.title.x=element_blank(), axis.title.y=element_text(family = "Arial",size=16,face = "bold"),axis.text.y=element_text(family = "Arial",size=14,face = "bold"), legend.title=element_blank()) 
+ scale_fill_grey(start = 0.4,end = 0.8) 
+ geom_line(data = df1, aes(x = a, y = b)) + annotate("text", x = 2, y = 350, label = "*", size = 8)
#数据
意思是猫
1296.1610 A
2235.9618 B
3220.6905摄氏度
4127.2546 D
#新生产线的Df

df1您必须将
fill
从主
ggplot()
函数移动到
geom\u条
,因为
fill
美学不适用于
geom\u线

# Using dummy sd as not specified
sd <- 10

library(ggplot2)
ggplot(df, aes(cat, means)) +
    geom_bar(aes(fill = cat), stat = "Identity", width = 0.75) +
    geom_errorbar(aes(ymin = means - sd, ymax = means + sd),
                  width = 0.2, position = position_dodge(0.9)) +
    geom_line(data = df1, aes(a, b)) + 
    annotate("text", x = 2, y = 350, label = "*", size = 8) +
    scale_fill_grey(start = 0.4, end = 0.8) +
    labs(y = "Mean",
         fill = NULL) +
    theme(legend.position = "none", 
          axis.text.y = element_text(family = "Arial",size=14,face = "bold"), 
          axis.text.x = element_text(family = "Arial", size = 16, face = "bold"), 
          axis.title.x = element_blank(), 
          axis.title.y = element_text(family = "Arial",size = 16, face = "bold"))
#未指定使用虚拟sd

sd您必须将
fill
从主
ggplot()
函数移动到
geom\u条
,因为
fill
美学不适用于
geom\u线

# Using dummy sd as not specified
sd <- 10

library(ggplot2)
ggplot(df, aes(cat, means)) +
    geom_bar(aes(fill = cat), stat = "Identity", width = 0.75) +
    geom_errorbar(aes(ymin = means - sd, ymax = means + sd),
                  width = 0.2, position = position_dodge(0.9)) +
    geom_line(data = df1, aes(a, b)) + 
    annotate("text", x = 2, y = 350, label = "*", size = 8) +
    scale_fill_grey(start = 0.4, end = 0.8) +
    labs(y = "Mean",
         fill = NULL) +
    theme(legend.position = "none", 
          axis.text.y = element_text(family = "Arial",size=14,face = "bold"), 
          axis.text.x = element_text(family = "Arial", size = 16, face = "bold"), 
          axis.title.x = element_blank(), 
          axis.title.y = element_text(family = "Arial",size = 16, face = "bold"))
#未指定使用虚拟sd
sd