6

I want to do a plot with large points inside a rectangle from a simple dataset. There are potentially multiple results that I want to display in different facets. The problem is that the size of the rectangle (using geom_rect) is defined in axis units while the size argument of geom_point is in some other units. Thus, the relative size of the points wrt the rectangle changes depending on the number of facets:

data<-data.frame(y=1:3,
                 facet=factor(1:3),
                 x=rep(1,3))

testplot<-function(data){
  p<-ggplot(data,aes(x=x,y=y,color=y))
  p<-p+facet_grid(.~facet)
  p<-p+scale_x_continuous(limits=c(0.5,1.5))
  p<-p+scale_y_continuous(limits=c(0.5,3.5))
  p<-p+geom_rect(xmin=0.85,xmax=1.15,ymin=0.74,ymax=3.25)
  p<-p+geom_point(size=50)
  return(p)
}

p1<-testplot(subset(data,facet=="1"))
p2<-testplot(data)

My question is, if I can scale the absolute point size in axis units, so that the relative size of points and the rectangle is identical for p1 and p2, independent of the number of facets in the graph.

agenis
  • 8,069
  • 5
  • 53
  • 102
  • [**This Q&A**](http://stackoverflow.com/questions/17311917/ggplot2-the-unit-of-size) doesn't answer your question, but may still be relevant reading for your thoughts on "`geom_point` in some other units". – Henrik Jun 30 '14 at 12:40
  • 2
    [**Possibly relevant as well?**](https://groups.google.com/forum/#!topic/ggplot2/gl97TrK31tE). E.g. the answer of @baptise: "If you want to add a circle in cartesian coordinates indicating a distance from a centre, then `geom_point()` is not the appropriate `geom` for this task. You should probably use `geom_polygon` instead and construct a circle manually with many vertices. That way, it will respect the coordinates, even after munching." – Henrik Jun 30 '14 at 12:49

1 Answers1

2

makes this fairly straight forward, the radius r is scaled relative to the coordinate scales (therefore important to use coord_fixed() if you want circles).

Examples:

library(ggplot2)
library(ggforce)

##sample data frame
grid_df = data.frame(x = 1:5, y = rep(1,5), r = seq(0.1, 0.5, 0.1), fill = letters[1:5])

with empty circles

ggplot() + 
geom_circle(data = grid_df, mapping = aes(x0 = x,  y0 = y, r = r)) + 
coord_fixed()

enter image description here

with filled circles and "fixed" fill (outside of aes)

ggplot() + 
geom_circle(data = grid_df, mapping = aes(x0 = x,  y0 = y, r = r), fill = 'black') + 
coord_fixed()

enter image description here

with filled circles and fill based on variable (inside of aes)

ggplot() + 
geom_circle(data = grid_df, mapping = aes(x0 = x,  y0 = y, r = r, fill = fill)) + 
coord_fixed()

enter image description here

tjebo
  • 21,977
  • 7
  • 58
  • 94