Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scatterplot with equal axes

Tags:

r

ggplot2

I have a data set like this one below:

DataFrame <- data.frame(x=runif(25),y=runif(25),
                        z=sample(letters[1:4],25,rep=TRUE))

and using the Lattice package, I can make a scatter plot with equal axes (with a 1:1 line going through the centre) with the following lines:

xyplot(y ~ x | z, data=DataFrame,
       scales=list(relation="free"),
       prepanel=function(x,y,...) {
         rg <- range(na.omit(c(x,y)))
         list(xlim=rg,ylim=rg)
       },panel=function(x,y,...) {
         panel.abline(0,1)
         panel.xyplot(x,y,...)
       })

In ggplot2, I have gotten this far:

ggplot(data=DataFrame) + geom_point(aes(x=x,y=y)) +
  facet_grid(~z,scales="free") + coord_equal(ratio=1) +
  geom_abline(intercept=0,slope=1)

But I'm not sure that coord_equal() is the function I'm looking for. What might be the equivalent function call in ggplot2?

like image 392
hatmatrix Avatar asked Jul 05 '26 20:07

hatmatrix


1 Answers

Your problem lies in setting free facet scales. Once you set the facet scales to be free, you can't then add coord_equal() If you eliminate the free scales, then coord_equal() works properly.

like image 126
JoFrhwld Avatar answered Jul 07 '26 09:07

JoFrhwld



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!