0

I am plotting PCA data and by default, ggplot2 uses different grid spacing on the X and Y axis (e.g. each tick on the x axis = 10, but each tick on the y axis = 5). I need to plot a wide range of data, so I do not want to set manual limits for the y and x axes each time.

I tried the coord_fixed() solution in this similar question, but it does not work well, and it still only makes the grid square, and does not force the scale of the grid to be the same on x and y.

Here is a reproducible example:

library(ggplot2)

ggplot(mtcars, aes(mpg, cyl)) +
  geom_point()

enter code here

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
James
  • 67
  • 4
  • Is the problem that the range of the axes do not align, or that the ticks are different? – Melissa Key Apr 01 '18 at 23:05
  • You can pass a function to the `break` and `label` arguments. – Amar Apr 02 '18 at 00:40
  • it's fine if the range of the axes is different, however, the x and y axes are being scaled differently, so the data are being plotted in a distorted/misrepresented way, since the units for PC1 and PC2 should be directly comparable. – James Apr 02 '18 at 02:53
  • @James, please see my edit as an example on how to include a minimal working example for the question. It is good practise as it makes it easier to help. – Michael Harper Apr 02 '18 at 15:34
  • When the legend is present, the coord_fixed() solution does not work well, and it still only makes the grid square, and does not force the scale of the grid to be the same on x and y. – James Apr 02 '18 at 18:55
  • @Jaap this is not a duplicate. This question is distinct (see above). – James Apr 03 '18 at 04:29
  • Expected output is not clear. Do we want x and y range to be same, [like 0 to 1](https://stackoverflow.com/questions/5468280/scale-a-series-between-two-points)? Or do we want plot to be square? (`ggplot(mtcars, aes(mpg, cyl)) + geom_point() + theme(aspect.ratio=1)`) Or do we want output plot as a file, square? (`ggsave(width = 500, height = 500)`) ? – zx8754 Apr 03 '18 at 07:20
  • 2
    Could you explain what is different? – Jaap Apr 03 '18 at 07:22
  • All I want to do is make the SCALE equivalent on the X and Y axes, while having different y and x limits. This means each tick on x and y is equally spaced and represents the same scale. I do not understand why this is so difficult in R. For example, y axis range is 0 to 20, x axis range is -50 to 50, but in both axes each tick is drawn in an interval of 10. I do not want to manually specify the ranges of 0-20, -50-50. – James Apr 05 '18 at 05:48

1 Answers1

3

Have you looked at coord_equal:

+ coord_equal(ratio = 1) 
Michael Harper
  • 14,721
  • 2
  • 60
  • 84
POPPKPD
  • 41
  • 3