问题描述
我正在尝试将我的绘图转换为百分比。我还想包括 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 年 7 月 7 日创建
解决方法
您似乎没有加载包 magrittr
。这就是管道运算符 %>%
的来源。试试library(magrittr)
。如果您尚未安装,请参阅 package website 了解详情。
您没有加载包 dplyr
,因此 R 无法理解管道运算符 %>%
。使用 library(dplyr)
加载它。