ggplot绘制嵌套圆:限制坐标轴后多边形异常截断或消失
问题:ggforce::geom_circle在限制坐标轴范围后显示异常
我尝试使用ggforce::geom_circle在ggplot对象中绘制两个嵌套圆,正常绘制时显示正常,但当使用scale_x/scale_y或coord_cartesian限制x、y坐标轴范围后,多边形要么被异常截断,要么完全不显示。
复现代码
library(ggforce) set.seed(4242) dd <- data.frame(x = runif(20, min=0, max=2), y = runif(20, min=0, max=2)) ggplot(dd) + geom_circle(aes(x0 = 0, y0 = 0, r = 2), inherit.aes = FALSE, fill = 'grey90', lty = 'dotted', color = 'grey70', alpha = 0.5) + geom_circle(aes(x0 = 0, y0 = 0, r = 0.5), inherit.aes = FALSE, fill = 'grey70', lty = 'dotted', color = 'grey50', alpha = 0.5) + geom_point(aes(x = x, y = y), size=1)+ geom_abline(intercept = 0, slope=0.5, col='red') + geom_abline(intercept = 0, slope=1.8, col='blue') + #scale_x_continuous(expand = c(0, 0), limits = c(0, 2)) + #scale_y_continuous(expand = c(0, 0), limits = c(0, 2)) + coord_cartesian(xlim = c(0,2.5), # 尝试两种坐标轴限制方式 ylim = c(0,2.5)) + theme_bw() + theme_update(legend.position = 'bottom') + theme_update(aspect.ratio=1)
会话信息
R version 4.1.1 (2021-08-10) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 17763) Matrix products: default locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] ggforce_0.4.1 ggplot2_3.3.6 loaded via a namespace (and not attached): [1] Rcpp_1.0.7 magrittr_2.0.2 MASS_7.3-54 tidyselect_1.1.2 [5] munsell_0.5.0 colorspace_2.0-2 R6_2.5.1 rlang_1.0.2 [9] fansi_1.0.2 dplyr_1.0.8 tools_4.1.1 grid_4.1.1 [13] gtable_0.3.0 utf8_1.2.2 cli_3.2.0 DBI_1.1.2 [17] withr_2.5.0 ellipsis_0.3.2 digest_0.6.28 assertthat_0.2.1 [21] tibble_3.1.6 lifecycle_1.0.1 crayon_1.5.0 farver_2.1.0 [25] tweenr_1.0.2 purrr_0.3.4 vctrs_0.3.8 glue_1.6.2 [29] labeling_0.4.2 polyclip_1.10-4 compiler_4.1.1 pillar_1.7.0 [33] generics_0.1.2 scales_1.1.1 pkgconfig_2.0.3
解决方案
这个问题的核心是geom_circle通过多边形路径模拟圆形,坐标轴范围限制会触发ggplot的图形裁剪逻辑,导致圆形顶点被截断。以下两种方法可解决:
方法一:用coord_fixed替代aspect.ratio=1
coord_fixed会先保持图形宽高比,再应用坐标范围限制,不会提前裁剪圆形顶点,能完整显示图形:
library(ggforce) set.seed(4242) dd <- data.frame(x = runif(20, min=0, max=2), y = runif(20, min=0, max=2)) ggplot(dd) + geom_circle(aes(x0 = 0, y0 = 0, r = 2), inherit.aes = FALSE, fill = 'grey90', lty = 'dotted', color = 'grey70', alpha = 0.5) + geom_circle(aes(x0 = 0, y0 = 0, r = 0.5), inherit.aes = FALSE, fill = 'grey70', lty = 'dotted', color = 'grey50', alpha = 0.5) + geom_point(aes(x = x, y = y), size=1)+ geom_abline(intercept = 0, slope=0.5, col='red') + geom_abline(intercept = 0, slope=1.8, col='blue') + coord_fixed(xlim = c(0,2.5), ylim = c(0,2.5), ratio = 1) + theme_bw() + theme(legend.position = 'bottom')
方法二:关闭坐标裁剪(可选)
如果必须使用coord_cartesian,可通过clip="off"关闭裁剪,同时调整绘图边距避免图形超出画布:
ggplot(dd) + geom_circle(aes(x0 = 0, y0 = 0, r = 2), inherit.aes = FALSE, fill = 'grey90', lty = 'dotted', color = 'grey70', alpha = 0.5) + geom_circle(aes(x0 = 0, y0 = 0, r = 0.5), inherit.aes = FALSE, fill = 'grey70', lty = 'dotted', color = 'grey50', alpha = 0.5) + geom_point(aes(x = x, y = y), size=1)+ geom_abline(intercept = 0, slope=0.5, col='red') + geom_abline(intercept = 0, slope=1.8, col='blue') + coord_cartesian(xlim = c(0,2.5), ylim = c(0,2.5), clip = "off") + theme_bw() + theme(legend.position = 'bottom', plot.margin = margin(10, 10, 10, 10))
内容的提问来源于stack exchange,提问作者maycca
相关产品推荐
相关产品推荐

