Работа с переменными осевыми масштабами в facet_wrap - PullRequest
0 голосов
/ 27 июня 2018

Я использую аргумент scales = "free_x" из facet_wrap(), потому что для каждого фасета имеется разное количество категориальных переменных. Я бы хотел, чтобы расстояние между переменными было фиксированным.

>dput(df)
structure(list(title = structure(c(1L, 76L, 2L, 3L, 4L, 5L, 79L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 75L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 
31L, 32L, 33L, 34L, 36L, 35L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 
44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 78L, 
64L, 56L, 57L, 58L, 59L, 77L, 60L, 61L, 62L, 63L, 65L, 66L, 67L, 
68L, 69L, 70L, 71L, 72L, 73L, 74L), .Label = c(" Advertising & PR managers", 
" Ambulance staff", " Animal care occupations", " Architects, town Planners, surveyors", 
" Artistic & literary occupations", " Authors & writers", " Bakers & flour confectioners", 
" Bar staff", " Biological scientists & biochemists", " Bus drivers", 
" Care assistants & home carers", " Carpenters & joiners", " Chefs & cooks", 
" Chiropodists", " Civil engineers", " Cleaners & domestics", 
" Clergy", " Corporate", " Corporate Managers & Senior Officials", 
" Countryside & park rangers", " Culture, media & sports occupations", 
" Electrical engineers", " Finance & investment analysts/advisers", 
" Financial managers & chartered secretaries", " Fire service officers", 
" Fishmongers", " Graphic designers", " Hairdressers & barbers", 
" Health & social services", " Health professionals", " Hospital porters", 
" Journalists", " Kitchen & catering assistants", " Lab technicians", 
" Leisure & sports", " Leisure occupations", " Librarians", " Marketing professionals", 
" Medical & dental technicians", " Metal makers", " Midwives", 
" Nurses", " Office managers", " Pharmacists/pharmacologists", 
" Photographers", " Physiotherapists", " Police officers (inspectors & above)", 
" Police officers (sergeant & below)", " Printing trades", " Prison-service officers ", 
" Product & clothing designers", " Production managers", " Psychologists", 
" Receptionists", " Retail managers", " School secretaries", 
" Science & engineering Technicians", " Science & technology professionals", 
" Security guards & related occupations", " Shelf fillers", " Social workers", 
" Software professionals", " Solicitors, lawyers, judges & coroners", 
" Taxation experts", " Taxi, cab drivers & chauffeurs", " Teachers", 
" Telephone salespersons", " Textiles & clothing trades", " Therapists", 
" Train drivers", " Travel agents", " Van drivers", " Waiting staff", 
" Youth & community workers", "Accountants", "Farming & fishing trades", 
"Local government", "Retail assistants", "Vehicle Assemblers"
), class = "factor"), category = structure(c(3L, 5L, 6L, 2L, 
7L, 3L, 5L, 3L, 6L, 6L, 7L, 6L, 2L, 5L, 6L, 6L, 2L, 7L, 6L, 2L, 
8L, 8L, 5L, 3L, 5L, 8L, 8L, 2L, 6L, 3L, 6L, 8L, 2L, 5L, 3L, 6L, 
7L, 6L, 8L, 1L, 3L, 2L, 5L, 2L, 2L, 1L, 7L, 3L, 2L, 4L, 4L, 5L, 
4L, 3L, 3L, 2L, 6L, 6L, 6L, 1L, 1L, 7L, 7L, 4L, 8L, 6L, 2L, 7L, 
4L, 6L, 2L, 6L, 5L, 2L, 6L, 6L, 6L, 6L, 2L), .Label = c("ADMIN & ORGANIZATIONAL", 
"CARE & EDUCATION", "CREATIVE & MEDIA", "LAW & JUSTICE", "MANUAL & PRODUCTION WORK", 
"SALES & SERVING OTHERS", "SCIENCE, TECH & ENGINEERING", "SENIOR MANAGERS & EXECS"
), class = "factor"), women = c(34749L, 14826L, 19563L, 14386L, 
31800L, 31663L, 18008L, 31425L, 14296L, 12071L, 33505L, 21660L, 
16364L, 15058L, 33943L, 15627L, 30642L, 28895L, 13290L, 22686L, 
33277L, 59745L, 18951L, 26119L, 29001L, 30510L, 43260L, 28652L, 
12799L, 21972L, 11829L, 35142L, 44788L, 15703L, 27768L, 12412L, 
19164L, 15796L, 22165L, 25281L, 24233L, 24989L, 12916L, 35189L, 
30265L, 28557L, 37321L, 19370L, 30789L, 55732L, 34974L, 16627L, 
22634L, 25100L, 35101L, 35857L, 15962L, 19740L, 13577L, 28883L, 
18167L, 20568L, 32635L, 23754L, 41780L, 13901L, 31738L, 34213L, 
42848L, 13374L, 35253L, 18560L, 16118L, 30258L, 40840L, 17847L, 
15140L, 11894L, 23258L), men = c(46551L, 20041L, 22265L, 14870L, 
36697L, 31803L, 27648L, 27889L, 17355L, 12604L, 39240L, 22232L, 
17555L, 23075L, 40813L, 18000L, 34322L, 35840L, 15162L, 22410L, 
44160L, 98010L, 22687L, 28032L, 45102L, 38987L, 68102L, 30789L, 
15483L, 24820L, 15675L, 42586L, 80159L, 19157L, 30330L, 12428L, 
24713L, 20335L, 27011L, 29613L, 29931L, 28850L, 26557L, 37234L, 
32972L, 40000L, 41370L, 27139L, 30882L, 58921L, 40499L, 23335L, 
28990L, 28044L, 42789L, 43842L, 16788L, 27128L, 15842L, 37078L, 
25721L, 28353L, 38143L, 21160L, 47011L, 14601L, 31785L, 37580L, 
55813L, 19270L, 39194L, 19716L, 21493L, 30290L, 42559L, 19406L, 
18959L, 12619L, 24984L)), .Names = c("title", "category", "women", 
"men"), class = "data.frame", row.names = c(NA, -79L))

Вот что у меня сейчас есть:

# gather the dataset
tidy_gap <- df %>% 
  gather(key = sex, value = salary, -title, -category)

category_means <- tidy_gap %>% 
  group_by(category) %>%
  summarize_at(vars(salary), mean)

tidy_gap %>% ggplot(aes(x = title, y = salary, color = sex)) +
  facet_wrap(~ category, nrow = 1, scales = "free_x") +
  geom_line(color = "white") +
  geom_point() +
  geom_blank() +
  scale_color_manual(values = c("#F49171", "#81C19C")) +
  geom_hline(data = category_means, aes(yintercept = salary), color = "white", alpha = 0.6, size = 1) +
  theme(aspect.ratio = 1, 
        legend.position = "none",
      panel.background = element_rect(color = "#242B47", fill = "#242B47"),
      plot.background = element_rect(color = "#242B47", fill = "#242B47"),
      axis.line = element_line(color = "grey48", size = 0.05, linetype = "dotted"),
      axis.text = element_text(family = "Georgia", color = "white"),
      axis.text.x = element_text(angle = 90),
      # Get rid of the y- and x-axis titles
      axis.title.y=element_blank(),
      axis.title.x=element_blank(),
      panel.grid.major.y = element_line(color = "grey48", size = 0.05),
      panel.grid.minor.y = element_blank(),
      panel.grid.major.x = element_blank(),
      strip.background = element_rect(color = "#242B47", fill = "#242B47"),
      strip.text = element_text(color = "white", family = "Georgia"))

enter image description here

Вы видите, что для первого фасета только с 4 переменными существует большое пространство между ними, тогда как фасеты со многими переменными сжимаются довольно близко друг к другу. В идеале мне нужно решение, которое вы можете установить в facet_wrap(), но я не уверен, возможно ли это.

Согласно другому сообщению SO Установка пределов отдельных осей с помощью facet_wrap и scale = "free" в ggplot2 это может быть возможно с geom_blank(); однако я понятия не имею, как можно это реализовать, поскольку range не имеет значения для факторов.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...