1

I wonder if it is possible to specify the point radius of geom_points in the same unit as the axes (meters in my example). I have inventory data where the sampling probability of a tree relies to an individual radius.

# reading some example data
dat <- read.table(header=TRUE, text="x y radius
0 0  7.5
10 0 5.5
20 0 7
30 0 9")
dat
#>    x y radius
#> 1  0 0    7.5
#> 2 10 0    5.5
#> 3 20 0    7.0
#> 4 30 0    9.0

I therefore need to draw the tree specific radius exactly to explain the method visually. I found older questions (like this four year old discussion: geom_point control radius exactly rather than scaling it) where the same problem was not finally solved. I wondered if there are may more recent approaches. I played around with scale_radius and scale_size_area but did not succeed. This is the code I tryed

ggplot(data = dat, aes(x = x, y = y, color = factor(dat$Nutz), size = radius * 2)) + 
geom_point() + scale_radius(name = NULL, range = c(0, 50))

This gives what I am looking for using the graphics package

plot(dat$x, dat$y, asp = 1, xlim = c(0, 50),ylim = c(0, 50))
symbols(dat$x, dat$y, circles = dat$radius, inches = FALSE, add = TRUE)

enter image description here

Does anyone have a clue? All the best, Kai

Eric Fail
  • 8,191
  • 8
  • 72
  • 128
Kai
  • 13
  • 3

2 Answers2

2

like this, adding the package,

library(ggforce) # devtools::install_github("thomasp85/ggforce")
ggplot() + geom_circle(aes(x0=x, y0=y, r= radius), data= dat) + coord_fixed() + 
           scale_x_continuous(minor_breaks = seq(-10, 40, 1), lim = c(-10, 40))  

geom_circle

in case someone needs to do the same thing, but have ellipsis, one can use geom_ellipsis(),

ggplot(dat, aes(x0 = x, y0 = y, a = radius, b = radius, angle = 0)) +
        geom_ellipsis() + geom_point(aes(x, y), size = .5) + coord_fixed() 

geom_ellipsis

here's another kinda of a work-around option,

ggplot(data = dat, aes(x = x, y = y, size = radius))  + 
  geom_point(shape=1, show.legend=F) + 
  scale_size(range = range(dat$radius)*10) + 
  scale_x_continuous(minor_breaks = seq(-10, 40, 1), lim = c(-10, 40))  

enter image description here

Eric Fail
  • 8,191
  • 8
  • 72
  • 128
  • Thanks a lot. Worked for me! For completeness: The geom_circle function does the job as well and is part of the latest CRAN version of ggforce. – Kai Feb 10 '18 at 16:30
  • @Kai If this answered your question, please upvote it and accept the answer. See [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – G5W Feb 10 '18 at 18:43
  • @Kai, it would be great if you could add an option with the `geom_circle`. I did not know about that update. – Eric Fail Feb 10 '18 at 21:18
0

After Erics hint, I found another solution using geom_circle which is part of the recent CRAN version of ggforce and requires less parameters.

ggplot(dat, aes(x0 = x, y0 = y, r = radius)) +
geom_circle(show.legend = FALSE) + 
geom_point(aes(x, y), size = .5, show.legend = FALSE) +
coord_fixed()

Here comes the link to the result

Kai
  • 13
  • 3