Home > Mobile >  Show nested circles on ggplot: polygons disappear when limited xy axis
Show nested circles on ggplot: polygons disappear when limited xy axis

Time:10-31

I am trying to show two nested circles in my ggplot object using ggforce::geom_circle that look like this:

enter image description here

It definitely works when I am plotting two circles:

enter image description here

but if I try to limit the x and y axis using scale_x or coord_cartesian either my polygons are weirdly cut, or not shown:

enter image description here

What can be the issue?

Here is a dummy example:

library(ggforce)

set.seed(4242)
dd <- data.frame(x = runif(20, min=0, max=2),
                 y = runif(20, min=0, max=2))


# There's presumably a way to do this within the above mutate function using case_when()
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),   # try to limit the xy axis in two ways
                  ylim = c(0,2.5))  
  theme_bw()  
  theme_update(legend.position = 'bottom')  
  theme_update(aspect.ratio=1) 

Session info:

    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 

CodePudding user response:

You could force the origin to 0 with expand and no limit in scale_*_continuous like this:

library(ggforce)
library(ggplot2)
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))  
  scale_y_continuous(expand = c(0, 0))  
  coord_cartesian(xlim = c(0,2.5),   # try to limit the xy axis in two ways
                  ylim = c(0,2.5))  
  theme_bw()  
  theme_update(legend.position = 'bottom')  
  theme_update(aspect.ratio=1) 

Created on 2022-10-28 with enter image description here

  • Related