Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: aspect.ratio overpowers coord_equal or coord.fixed

I want to receive a square plot with equal coordinates whatever is the shape of the cloud of my data points.

Here is a primitive illustration of my question.

xy <- data.frame(x = rnorm(100),
                 y = rnorm(100, mean = 1, sd = 2))
gg <- ggplot(xy,aes(x = x, y = y))+
        geom_point()
gg + coord_equal() #now we have square coordinate grid
gg + theme(aspect.ratio = 1) #now the plot is square

# if we use both, aspect.ratio overpowers coord_equal
gg + coord_equal() + theme(aspect.ratio = 1)  

Is there any way to have both plot and the coordinate grid squared? Of course, there would be some blank areas in the plot. I don't mind this.

Also, I'd like to know the simplest way to ensure equal labeling on both x and y axes.

like image 986
ikashnitsky Avatar asked Aug 11 '15 10:08

ikashnitsky


1 Answers

You can force the plot to be square by manually setting the scale of the axes:

gg +
scale_x_continuous(limits=c(min(xy$x,xy$y), max(xy$x,xy$y))) +
scale_y_continuous(limits=c(min(xy$x,xy$y), max(xy$x,xy$y))) +
theme(aspect.ratio=1)
like image 195
heathobrien Avatar answered Oct 18 '22 07:10

heathobrien