How to fix the aspect ratio in ggplot?

前端 未结 3 1132
忘了有多久
忘了有多久 2020-11-30 20:18

I\'m trying to resize a plot to fit into my document, but I\'m having difficulties getting the plotted diagram do be a square.

Example:

pdf(file = \"         


        
3条回答
  •  庸人自扰
    2020-11-30 20:47

    In ggplot the mechanism to preserve the aspect ratio of your plot is to add a coord_fixed() layer to the plot. This will preserve the aspect ratio of the plot itself, regardless of the shape of the actual bounding box.

    (I also suggest you use ggsave to save your resulting plot to pdf/png/etc, rather than the pdf(); print(p); dev.off() sequence.)

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

    enter image description here

提交回复
热议问题