настроить шкалы x и y для отдельных фасетов в facet_grid (), чтобы они соответствовали меткам geom_text_repel ()? - PullRequest
2 голосов
/ 02 июня 2019

Мне нужно вручную указать пределы обеих осей x и y для каждого facet (3 строки и 3 столбца) в графе facet_grid.

У меня есть 2 категориальных фактора и непрерывное значение y (поэтому я строю график catch_ema_thousands на оси y, с данными, ограненными redlistCategory (3 уровня) и TaxonGroup (3 уровня), затем раскрасьте точки в соответствии с VARIABLE (также 3 уровня).

facet_grid имеет очень неравномерное количество точек данных и диапазон значений y (0 - 1700, при этом большинство точек сгруппировано около 0, а некоторые грани с 20 точками, а другие - с 0 или 1).

У меня есть scale_y_sqrt, чтобы преобразовать ось Y, что помогает, но все еще слишком трудно разглядеть точки в занятых гранях.

Я использовал аргументы scales = "free" и space="free" в facet_grid(), который регулирует размеры фасетов в соответствии с точками данных, но не учитывает подгонку меток geom_text_repel().

Нужно ли регулировать размеры фасетов в пределах ggrepel?

library(scales)
library(ggrepel)
library(forcats)
library(RColorBrewer)
library(tidyverse)

ylimits <- c(2,NA) #keep the labels above the cluster of points at the bottom of the facets

Список ~ 25 видов, которые я хочу обозначить:

special.points <- special.points <- rownames(subset(plotdat, 
                         SpeciesOrTaxon %in% c("Gadus morhua","Melanogrammus aeglefinus","Thunnus obesus",
                                            "Trachurus trachurus","Sardinella maderensis","Thunnus thynnus",
                                            "Thunnus maccoyii","Hippoglossus hippoglossus","Squalus acanthias",
                                            "Merluccius senegalensis","Epinephelus striatus","Apostichopus japonicus",
                                            "Mobula mobular","Isurus oxyrinchus","Mustelus schmitti",
                                            "Pseudotolithus senegalensis","Sebastolobus alascanus",
                                            "Leucoraja circularis","Argyrosomus hololepidotus","Raja undulata",
                                            "Isurus paucus","Sphyrna lewini","Squatina argentina","Dipturus batis",
                                            "Squatina squatina","Carcharhinus falciformis","Squalus acanthias",
                                            "Makaira nigricans","Thunnus orientalis","Carcharhinus longimanus",
                                            "Lamna nasus","Sphyrna zygaena","Alopias vulpinus","Cetorhinus maximus",
                                            "Alopias superciliosus","Carcharodon carcharias","Palinurus elephas"))) 

Мой действительно раздражающий сюжет:

p <- ggplot(data = plotdat, aes(x= -totRank, y = catch_ema_thousands, label = Name_abbrev)) + 
  geom_point(data=plotdat,aes(color = VARIABLE), size = 1.2, shape = 1, stroke = 0.8) +
  scale_color_manual(values=c("black","orange","darkgrey"), labels = c("CITES","Intl trade","No intl trade")) +
  facet_grid(redlistCategory~TaxonGroup, scales = "free", space = "free") + 
  scale_y_sqrt() +
  geom_text_repel(data = plotdat, 
                  aes(x=-totRank, y = catch_ema_thousands, label = Name_abbrev), 
# used the abbreviated species name to try and fit them better
                  segment.color = "black", segment.size = 0.2, segment.alpha = 0.5, 
                  direction = "both",
                  min.segment.length = 0.5,
                  force = 10, size = 2, color = "black",
                  ylim = ylimits) + 
  ylab("Average catch (thousand tonnes)") +
  theme(axis.text.x = element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.x=element_blank(),
        axis.title.y = element_blank(),
        legend.position = "top", 
        legend.title = element_blank(),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) 
#dev.off() 

Я попробовал новый пакет, , но получил ошибку:

devtools::install_github("zeehio/facetscales")
library(facetscales)
scales_y <- list(
  CR = scale_y_sqrt(limits = c(0,50), breaks = c(0,50,10)),
  EN = scale_y_continuous(limits = c(0,50), breaks = c(0,50,10)),
  VU = scale_y_continuous(labels = scientific_format())
)

scales_x <- list(
  'Invertebrates' = scale_x_discrete(limits = c(-50,-40)), # plotting in reverse rank (-59 to 0)
  'Cartilaginous fish' =  scale_x_discrete(limits = c(-59,0)),
  'Bony fish' =  scale_x_discrete(limits = c(-59,0))
)

Тогда

facet_grid(redlistCategory~TaxonGroup, scales = list(y=scales_y, x = scales_x))

Что-то не так с переопределением "свободных" масштабов в facet_grid (), я получаю эту ошибку:

Error in match.arg(scales, c("fixed", "free_x", "free_y",
 "free")) :    'arg' must be NULL or a character vector

Пример данных графика:

plotdat <- dput(structure(list(SpeciesOrTaxon = c("Squatina argentina", "Dipturus batis", 
"Squatina squatina", "Thunnus maccoyii", "Epinephelus striatus", 
"Sphyrna lewini", "Mobula mobular", "Isurus oxyrinchus", "Mustelus schmitti", 
"Pseudotolithus senegalensis", "Sebastolobus alascanus", "Leucoraja circularis", 
"Argyrosomus hololepidotus", "Raja undulata", "Isurus paucus", 
"Thunnus thynnus", "Hippoglossus hippoglossus", "Merluccius senegalensis", 
"Apostichopus japonicus", "Carcharhinus falciformis", "Carcharhinus longimanus", 
"Lamna nasus", "Sphyrna zygaena", "Alopias vulpinus", "Cetorhinus maximus", 
"Alopias superciliosus", "Carcharodon carcharias", "Sardinella maderensis", 
"Galeorhinus galeus", "Pomatomus saltatrix", "Pentanemus quinquarius", 
"Pseudupeneus prayensis", "Nemipterus virgatus", "Pseudotolithus senegallus", 
"Dalatias licha", "Lutjanus campechanus", "Megalops atlanticus", 
"Mola mola", "Mustelus mustelus", "Centrophorus squamosus", "Balistes capriscus", 
"Centrophorus lusitanicus", "Leucoraja fullonica", "Rhomboplites aurorubens", 
"Dentex dentex", "Epinephelus marginatus", "Palinurus elephas", 
"Alosa immaculata", "Carcharhinus plumbeus", "Oxynotus centrina", 
"Gymnura altavela", "Carcharias taurus", "Gadus morhua", "Melanogrammus aeglefinus", 
"Thunnus obesus", "Trachurus trachurus", "Squalus acanthias", 
"Makaira nigricans", "Thunnus orientalis"), Name_abbrev = c("S. argentina", 
"D. batis", "S. squatina", "T. maccoyii", "E. striatus", "S. lewini", 
"M. mobular", "I. oxyrinchus", "M. schmitti", "P. senegalensis", 
"S. alascanus", "L. circularis", "A. hololepidotus", "R. undulata", 
"I. paucus", "T. thynnus", "H. hippoglossus", "M. senegalensis", 
"A. japonicus", "C. falciformis", "C. longimanus", "L. nasus", 
"S. zygaena", "A. vulpinus", "C. maximus", "A. superciliosus", 
"C.carcharias", "S. maderensis", "G. galeus", "P. saltatrix", 
"P. quinquarius", "P. prayensis", "N. virgatus", "P. senegallus", 
"D. licha", "L. campechanus", "M. atlanticus", "M. mola", "M. mustelus", 
"C. squamosus", "B. capriscus", "C. lusitanicus", "L. fullonica", 
"R. aurorubens", "D. dentex", "E. marginatus", "P. elephas", 
"A. immaculata", "C. plumbeus", "O. centrina", "G. altavela", 
"C. taurus", "G. morhua", "M. aeglefinus", "T. obesus", "T. trachurus", 
"S. acanthias", "M. nigricans", "T. orientalis"), redlistCategory = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("CR", "EN", 
"VU"), class = "factor"), TaxonGroup = structure(c(2L, 2L, 2L, 
3L, 3L, 2L, 2L, 2L, 2L, 3L, 3L, 2L, 3L, 2L, 2L, 3L, 3L, 3L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 
3L, 3L, 3L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 3L, 1L, 3L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 2L, 3L, 3L), .Label = c("Invertebrates", 
"Cartilaginous fish", "Bony fish"), class = "factor"), totRank = c(12, 
39, 55, 7, 38, 56, 57, 8, 11, 17, 33, 36, 37, 50, 58, 6, 9, 16, 
51, 15, 27, 30, 43, 45, 49, 52, 53, 5, 13, 14, 19, 20, 22, 23, 
24, 25, 26, 28, 29, 31, 32, 34, 35, 40, 41, 42, 44, 46, 47, 48, 
54, 59, 1, 2, 3, 4, 10, 18, 21), VARIABLE = structure(c(3L, 3L, 
3L, 2L, 2L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("CITES", "YES", 
"NO"), class = "factor"), catch_ema_thousands = c(3.886654422, 
0.1724791016, 0.00911430205, 10.54412869, 0.174470439, 0.00807692997, 
0.001640665002, 9.424452066, 6.583041893, 2.659608617, 0.2663195953, 
0.2329239555, 0.2219422872, 0.01671489332, 0.0014159872, 13.44830652, 
7.585155675, 2.774650025, 0.01599999, 3.024126379, 0.4539170316, 
0.3113769576, 0.1163730191, 0.1011488649, 0.01681304105, 0.01561369792, 
0.01416268544, 108.778465, 3.822553738, 3.251341729, 2.440669803, 
1.688880545, 1.358903072, 1.100693581, 1.06694699, 0.8025907339, 
0.5465603847, 0.4392502858, 0.3591757093, 0.2919081194, 0.2671983104, 
0.2478545144, 0.2435067011, 0.15794176, 0.1539382418, 0.1226202735, 
0.1079683714, 0.06792588753, 0.03801280875, 0.02357907878, 0.009323075655, 
0.000514006594, 1652.737638, 484.1897672, 397.4311939, 153.0306153, 
7.422144444, 2.459107988, 1.545317165)), row.names = c(NA, -59L
), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame")))

Ответы [ 2 ]

0 голосов
/ 05 июня 2019
blank_data <- read_csv("blankdata.csv")  
# must have all the columns that you call in ggplot. 
# Make a row for the minimum and maximum x and y values you want for each facet
p <- ggplot(data = plotdat, aes(x= xvar, y = yvar, label = labelvar)) + 
  geom_jitter(data=plotdat,
              aes(color = colorvar), size = 1.2, shape = 1, stroke = 0.8, width = 
                0.25, height = 0.25) +
  geom_blank(data=blankdata, aes(x =xvar, y = yvar, label = labelvar)) +
  facet_grid(ycategory ~ xcategory, scales = "free", space = "free") + 
  geom_text_repel(data = labelvar, # can subset and only label certain points
                  aes(x=xvar, y = yvar, label = labelvar),
                  segment.color = "black", segment.size = 0.2, segment.alpha = 
                    0.5, 
                  direction = "both", 
                  min.segment.length = 0.5,
                  force = 10, size = 2, color = "black") +
  scale_y_sqrt(expand = expand_scale(mult=c(0.1,0))) + 
  # added extra space (10%) at the bottom of each axis because all the labels were clustered there 
  scale_x_continuous(expand = expand_scale(mult=c(0.1,0.1))) +
  theme_bw()
0 голосов
/ 02 июня 2019

Редактировать: добавление альтернативы, используя scale_y_sqrt(expand = expand_scale), чтобы создать больше границ в каждом фасете.

Если преобразование sqrt имеет больше смысла, вы можете подумать об автоматическом выделении места для меток с помощью expand_scale. По умолчанию в ggplot добавляется 5% за пределы диапазона ваших данных, но при увеличении это создаст больше места для меток на всех фасетах.

Может также стоить изменить или удалить часть ylim = ylimits, так как это создало несколько перекрывающихся групповых меток, когда я впервые открыл пример.

scale_y_sqrt(expand = expand_scale(0.2)) +
geom_text_repel(data = plotdat, 
                aes(x=-totRank, y = catch_ema_thousands, label = Name_abbrev), 
                # used the abbreviated species name to try and fit them better
                segment.color = "black", segment.size = 0.2, segment.alpha = 0.5, 
                direction = "both", nudge_y = 0,
                min.segment.length = 0.5, max.iter = 5000,
                force = 10, size = 2, color = "black") + # w/o ylim part

enter image description here


Первый ответ:

Я думаю, что log10 может быть подходящим вариантом масштабирования, учитывая широкий диапазон величин:

p <- ggplot(data = plotdat, aes(x= -totRank, y = catch_ema_thousands, label = Name_abbrev)) + 
  geom_point(data=plotdat,aes(color = VARIABLE), size = 1.2, shape = 1, stroke = 0.8) +
  scale_color_manual(values=c("black","orange","darkgrey"), labels = c("CITES","Intl trade","No intl trade")) +
  facet_grid(redlistCategory~TaxonGroup, scales = "free", space = "free") + 
    scale_y_log10(
      breaks = 10^(-10:10),
      labels = scales::trans_format("log10", scales::math_format(10^.x))
    ) +
    # annotation_logticks(side = "l") +

  geom_text_repel(data = plotdat, 
                  aes(x=-totRank, y = catch_ema_thousands, label = Name_abbrev), 
                  # used the abbreviated species name to try and fit them better
                  segment.color = "black", segment.size = 0.2, segment.alpha = 0.5, 
                  direction = "both", nudge_y = 0,
                  min.segment.length = 0.5, max.iter = 5000,
                  force = 10, size = 2, color = "black") + #, ylim = ylimits) + 
  ylab("Average catch (thousand tonnes)") +
  theme(axis.text.x = element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.x=element_blank(),
        axis.title.y = element_blank(),
        legend.position = "top", 
        legend.title = element_blank(),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) 

  p

enter image description here

...