How to control ggplot's plotting area proportions instead of fitting them to devices in R?

前端 未结 3 1596
星月不相逢
星月不相逢 2021-01-03 03:26

By default, each plot in ggplot fits its device.

That\'s not always desirable. For instance, one may need to make tiles in geom_tile to be

3条回答
  •  太阳男子
    2021-01-03 03:35

    You can specify the aspect ratio of your plots using coord_fixed().

    > library(ggplot2)
    > df <- data.frame(
    +     x = runif(100, 0, 5),
    +     y = runif(100, 0, 5))
    

    If we just go ahead and plot these data then we get a plot which conforms to the dimensions of the output device.

    > ggplot(df, aes(x=x, y=y)) + geom_point()
    

    If, however, we use coord_fixed() then we get a plot with fixed aspect ratio (which, by default has x- and y-axes of same length). The size of the plot will be determined by the shortest dimension of the output device.

    > ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed()
    

    Finally, we can adjust the fixed aspect ratio by specifying an argument to coord_fixed(), where the argument is the ratio of the length of the y-axis to the length of the x-axis. So, to get a plot that is twice as tall as it is wide, we would use:

    > ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed(2)
    

提交回复
热议问题