Equal axis scatter plot

I have a dataset similar to the one below:

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

and using the lattice packet, I can make a scatter plot with equal axes (with a 1: 1 line through the center) 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 got 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 if the coord_equal () function is the function I'm looking for. What could be the equivalent function call in ggplot2?

+3
source share
2 answers

Your problem is installing free facets. After you check the facet boxes, you cannot add. coord_equal()If you eliminate the free scales, then it coord_equal()works correctly.

+5
source

, facet_wrap() - , , xlim ylim ggplot2.

+1

Source: https://habr.com/ru/post/1761474/


All Articles