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.
ggforce 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()
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()
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With