r - 调整 facet_grid() 中各个方面的 x 和 y 比例以适合 geom_text_repel() 标签?
问题描述
我需要为facet
facet_grid 中的每个(3 行和 3 列)手动指定 x 轴和 y 轴的限制。
我有 2 个分类因子和一个连续的 y 值(所以我catch_ema_thousands
在y
轴上绘制 ,数据由redlistCategory
(3 个级别)和TaxonGroup
(3 个级别)分面,然后根据VARIABLE
(也是 3 个级别)对点进行着色。
具有非常不均匀的facet_grid
数据点数量和 y 值范围(0 - 1,700,大多数点聚集在 0 附近,一些刻面有 20 个点,而另一些刻面有 0 或 1)。
我必须scale_y_sqrt
转换 y 轴,这会有所帮助,但仍然很难看到繁忙面上的点。
我在 中使用了scales = "free"
andspace="free"
参数facet_grid()
,它调整了 facet 的大小以适应数据点,但不考虑拟合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()
我尝试了一个新包facetscales,但出现错误:
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")))
解决方案
编辑:添加替代使用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
第一个答案:
我认为 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
推荐阅读
- apache-spark - Encoder和CatalystSerde是什么关系
- bash - 如何用 bash 下载 Firefox?
- linux - 如何在多行中格式化linux mpstat输出
- javascript - 将 socket.io 配置为 wss 而不是 ws
- javascript - 如何在传单地图上添加叠加层?
- python - 在循环环境下将附加数据写入 xlsx 行而不覆盖的代码
- javascript - 如何停止在富文本编辑器中将“大于”和“小于”符号转换为实体?
- keycloak - 使用 kcadm cli 添加 LDAP 用户联合,但在进行任何领域设置更改之前无法在 UI 中显示
- python-3.x - GPhoto2 - 直接从相机获取焦距和传感器尺寸
- jenkins - 为什么 Jenkins 字符串参数为空?