My goal is to fix the coordinate aspect ratio of a plot from ggplot2 via coord_fixed()
. I thought coord_fixed(ratio=1)
did the job independently of the scales of the x- or y-axis. My intuition: the argument ratio
refers to the ratio of the total range of coordinate x to the total range of coordinate y. Implying that a ratio of 1 always means that the x-axis will be as long as the y-axis in the plot.
Yet with x-coordinates in the 1000s and y-coordinates e.g. percent, coord_fixed does not behave not as I expect it.
2 Questions:
- Can you explain why coord_fixed takes the actual scale of the data into account but not the coordinate length as a whole?
- Can I change the coord_fixed programatically to always refer to the whole range of the x- and y-coordinate values?
Here's an illustration
library("ggplot2")
set.seed(123)
df = data.frame(x=runif(11)*1000,y=seq(0,.5,.05))
ggplot(df, aes(x,y)) +geom_point() +coord_fixed(1)
produces
Rescaling the data by the ratio of x- and y-values in coord-fixed solves the issue
ggplot(df, aes(x,y)) +geom_point() +coord_fixed(1*max(df$x)/max(df$y))
However, this is not progammatically. I would have to specify the df$x
manually to achieve the desired effect. See question 2: Is there a sensible way to automatize the re-scaling of the coordinates within coord_fixed
depending on which data is on the x-/y-axis in my ggplot plot?