【问题标题】:Bars/Columns not showing up in bar plat in ggplot2条形图/柱形图未显示在 ggplot2 的条形图上
【发布时间】:2022-08-12 23:52:58
【问题描述】:

我正在尝试在 R 中制作条形图,并且正在遵循过去成功的代码。但是,由于某种原因,该图只显示了图表的误差线,而不是误差线本身。

数据从以下数据框中提取

plr_sum2 <- data_summary(plr2, varname=\"Ranking\", 
                        groupnames=\"Choice\")

其中包括以下数据

Choice Ranking sd se Friend
0 2.98 1.27 0.08 Incorrect
1 3.67 1.26 0.08 Correct

我尝试使用条形的“Choice”变量和“Friend”变量(它们是同义词)制作图表并得到相同的错误

graph5<-ggplot(plr_sum2, aes(x=Friend, y=Ranking) ) +
  geom_col(fill=\"lightblue\") +
  theme_bw()+
  geom_errorbar( aes(x=Friend, ymin=Ranking-se, ymax=Ranking+se, width=0.2), colour=\"black\", alpha=0.9, size=0.5)+
  labs(y = \"Social Preference\", x = \"Register Choice\")+
  scale_x_discrete(breaks=c(\"Correct\",\"Incorrect\"))+
  scale_y_continuous(limits=c(1,5))
graph5 

当我使用此代码时,我收到警告消息“已删除 2 行包含缺失值 (geom_col)”,并且图表仅显示错误栏。

  • 摆脱limits 或调整它们。当您将下限设置为 1 时,条形图从零开始,它们被丢弃。

标签: r ggplot2 bar-chart


【解决方案1】:

您的问题是您的 y 限制指定不正确(必须包括 0)。此外,不需要scale_x_discrete 的倒数第二行:

ggplot(plr_sum2, aes(x = Friend, y = Ranking)) +
  geom_col(fill = "lightblue") +
  geom_errorbar(aes(x=Friend, ymin=Ranking-se, ymax=Ranking+se, width=0.2), colour="black", alpha=0.9, size=0.5)+
  labs(y = "Social Preference", x = "Register Choice") +
  theme_bw() +
  scale_y_continuous(limits = c(0,5))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    相关资源
    最近更新 更多