我正在尝试将绘图转换为百分比。我还想包括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软件包(v2.0.0(于2021-07-07创建
您似乎没有加载包magrittr
。这就是管道操作符%>%
的来源。尝试library(magrittr)
。如果您还没有安装软件包,请参阅软件包网站了解详细信息。
您没有加载包dplyr
,因此R无法理解管道运算符%>%
。使用library(dplyr)
加载。