0

I am trying to convert my plots into percentages. I also wanted to include the facet wrap function. Without that line, the plot seems to be working. Kindly suggest the changes to be made to the code. The error obtained is as follows: Error: At least one layer must contain all faceting variables: Building.Age.

  • Plot is missing Building.Age
  • Layer 1 is missing Building.Age
  • Layer 2 is missing 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 "%>%"

Created on 2021-07-07 by the reprex package (v2.0.0)

2 Answers2

0

It would seem you don't have the package magrittr loaded. This is where the pipe operator %>% comes from. Try library(magrittr). See package website for details if you don't have it installed yet.

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
0

You didn't load the package dplyr, so the pipe operator %>% isn't understood by R. Use library(dplyr) to load it.

MonJeanJean
  • 2,876
  • 1
  • 4
  • 20