首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Geom_line失败:参数隐含不同的行数

Geom_line失败:参数隐含不同的行数
EN

Stack Overflow用户
提问于 2018-02-12 14:12:16
回答 1查看 719关注 0票数 0

我正试图在我的条形图中添加一个“重要性条”,就像这里展示的那样:https://stackoverflow.com/a/29263992/9188785

但是,当我尝试在我的数据上重新生成它时,我得到了以下错误:... arguments imply differing number of rows: 6, 0

请注意,只要我在末尾不包含geom_line,一切都可以正常工作。

数据和代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# 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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-12 14:26:34

您必须将fill从主ggplot()函数迁移到geom_bar,因为fill美学不适用于geom_line

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# 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"))

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48748457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文