在facet_wrap中处理可变轴比例

时间:2018-06-26 23:18:34

标签: r ggplot2 scale facet-wrap

我正在使用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帖子Setting individual axis limits with facet_wrap and scales = "free" in ggplot2,这可能通过geom_blank()来实现;但是,由于range对于各种因素都没有意义,我不知道该如何实施。

0 个答案:

没有答案