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.
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)
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