代码之家  ›  专栏  ›  技术社区  ›  Zereges Parveen Prajapati

如何调整ggplot中的方面

  •  1
  • Zereges Parveen Prajapati  · 技术社区  · 8 年前

    我正在绘制一些数据并使用 facet_grid .

    data <- read.csv(text='"type1","type2","time"
    "A","T1",1182.17
    "A","T2",1116.89
    "A","T3",1088.66
    "B","T1",1095.51
    "B","T2",1106.09
    "B","T3",1132.95
    "C","T1",1094.11
    "C","T2",1145.15
    "C","T3",1159.14
    ')
    
    library(ggplot2)
    theme_set(theme_bw())
    
    ggplot(data, aes(type2, time)) +
        geom_point() +
        scale_x_discrete(
            expand=c(0,5)
        ) +
        coord_flip() +
        facet_grid(type1 ~ .)
    

    这给了我一个很好的情节

    我想降低每行的高度,所以我试图增加

    scale_x_discrete(
        expand=c(0,5)
    ) +
    

    这给了我

    如何调整面行的大小?我试过了 scale space 的参数 面网格 但那没有效果。我也试过了 heights / widths 属于 面网格 但它们似乎被移除了。

    我正试图得到这样的东西

    1 回复  |  直到 8 年前
        1
  •  0
  •   Jan Boyer VALIZCANOS    8 年前

    r guis(无论您使用的是基本的gui、rstudio等)通常以适合打开的绘图窗口的高宽比显示绘图。无论绘图如何显示在屏幕上,您都可以使用 ggsave() 为了用您喜欢的尺寸保存绘图,ggplot将缩放绘图,使其看起来更符合该纵横比(在大多数情况下,有时您需要编辑文本大小和边距)。

    要将绘图保存到所需的尺寸,请首先将其作为命名对象:

    plot.name <- ggplot(data, aes(type2, time)) +
        geom_point() +
        scale_x_discrete(expand=c(0,5)) +
        coord_flip() +
        facet_grid(type1 ~ .)
    

    然后用长宽短高保存:

    ggsave(plot.name, #your ggplot
           "./example_plot.jpg", #filepath and name of file to save
            dpi = 300, 
            width = 6, #long width
            height = 3) #short height
    
    推荐文章