【问题标题】:Why the code did not run with facet wrap function [duplicate]为什么代码没有使用 facet wrap 功能运行 [重复]
【发布时间】:2021-07-01 07:42:10
【问题描述】:

我正在尝试将我的图表转换为百分比。我还想包括 facet wrap 功能。没有那条线,情节似乎正在发挥作用。请建议对代码进行的更改。得到的错误如下: 错误:至少一层必须包含所有分面变量:Building.Age

  • 绘图丢失Building.Age
  • 缺少第 1 层 Building.Age
  • 缺少第 2 层 Building.Age
   data %>% 
     count(Locality.Division = factor(Locality.Division), Number.of.Beetle = factor(Number.of.Beetle)) %>% 
     mutate(pct = prop.table(n)) %>% 
     ggplot(aes(x = Locality.Division, y = pct, fill = Number.of.Beetle, label = scales::percent(pct))) + 
     geom_col(position = 'dodge') + 
     geom_text(position = position_dodge(width = .9),    # move to center of bars
               vjust = -0.5,    # nudge above top of bar
               size = 3) + 
     scale_y_continuous(labels = scales::percent)+
     facet_wrap(~Building.Age)+
     labs(title = "Comparison between Number of beetle, Locality division and Age of the building",subtitle ="Building age") 
#> Error in data %>% count(Locality.Division = factor(Locality.Division), : could not find function "%>%"

reprex package (v2.0.0) 于 2021-07-07 创建

【问题讨论】:

    标签: r ggplot2 facet-wrap


    【解决方案1】:

    您似乎没有加载包magrittr。这就是管道运算符%>% 的来源。试试library(magrittr)。如果您尚未安装,请参阅package website 了解详细信息。

    【讨论】:

    • 谢谢。但情节现在停止出现。请帮忙
    【解决方案2】:

    您没有加载包 dplyr,因此 R 无法理解管道运算符 %>%。使用library(dplyr) 加载它。

    【讨论】:

    • 我尝试上传包。但是剧情现在没有出现。请帮助我。
    • 我已经替换了第三行代码中的 facet wrap。 facet_wrap(~ Building.Age) %>%
    猜你喜欢
    • 2021-02-22
    • 1970-01-01
    • 2018-08-02
    • 2018-09-09
    • 2014-05-18
    • 2018-06-23
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多