How to control the size of the ggplot plot area instead of fitting them to devices in R?

By default, each graph in ggplotcorresponds to its device.

This is not always desirable. For example, you might want to make tiles in geom_tilesquares. As soon as you change the device or change the number of elements along the x / y axis, the tiles are no longer squares.

Is it possible to set rigid proportions or size for the graph and fit the graph in the window of its device (or make the width and height of the device proportional to the size of the graph)?

+2
source share
2 answers

You can specify the aspect ratio of your charts using the cop_fixed () function.

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

, , .

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

, , coord_fixed(), ( x y ). .

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

, , coord_fixed(), y x. , , , :

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

, ,

library(ggplot2)
p = qplot(1:10, (1:10)^3)
g = ggplotGrob(p)
g$respect = TRUE
library(grid)
grid.draw(g)
+2

Source: https://habr.com/ru/post/1620622/


All Articles