我需要为facet_grid中的每个facet
(3行3列)手动指定x和y轴的限制。
我有2个分类因子和一个连续的y值(因此我在catch_ema_thousands
轴上绘制y
,数据以redlistCategory
(3个级别)和{{ 1}}(3个级别),然后根据TaxonGroup
(也是3个级别)为点着色。
VARIABLE
的数据点数量和y值范围非常不均匀(0-1,700,大多数点聚集在0周围,某些构面包含20个点,另一些构面具有0或1)。
我有facet_grid
可以变换y轴,这很有用,但是在忙碌的各个方面都很难看到点。
我在scale_y_sqrt
中使用了scales = "free"
和space="free"
参数,它们调整了构面大小以适合数据点,但没有考虑适合{{1 }} 标签。
我是否必须在facet_grid()
内调整刻面大小?
geom_text_repel()
我要标记的约25种物种的列表:
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
我尝试了一个新软件包facetscales,但遇到错误:
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()
覆盖facet_grid()中的“自由”标度有些不对劲,我得到了这个错误:
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))
答案 0 :(得分:0)
鉴于幅度范围很广,我认为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
答案 1 :(得分:0)
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()