【问题标题】:Specify Width and Height of Plot指定绘图的宽度和高度
【发布时间】:2010-11-19 17:22:03
【问题描述】:

我有一个包含三个图的面板。如何使用par 指定主面板的宽度和高度,使其始终保持固定大小?

【问题讨论】:

    标签: graphics r par


    【解决方案1】:

    您在设备中执行此操作,例如

    x11(width=4, height=6)
    

    对于基于文件的类似

    pdf("/tmp/foo.pdf", width=4, height=6)
    

    您可以通过par("cin")等读取物理尺寸,但不能设置。

    【讨论】:

    • 为了完整起见,widthheight 以英寸为单位。
    • 正如@fbmd 提到的,但如果您指定unit='in',您还必须指定res='xx',例如xx 为72。
    【解决方案2】:

    我通常在与windows.options 的会话开始时设置它:

    windows.options(width=10, height=10)
    
    # plot away
    plot(...)
    

    如果需要恢复为“出厂设置”:

    dev.off()
    windows.options(reset=TRUE)
    
    # more plotting
    plot(...)
    

    【讨论】:

    • 我认为这是特定于 Windows 的。
    • 不适用于 Mac OSX:Browse[1]> windows.options(width=10, height=10) Error in windows.options(width = 10, height = 10) : could not find function "windows.options"
    【解决方案3】:

    这两种解决方案都不适用于 Jupyter 笔记本。以下是适用于任何环境的通用方法:

    options(repr.plot.width=6, repr.plot.height=4)
    

    只要把以下函数放在手边:

    set_plot_dimensions <- function(width_choice, height_choice) {
            options(repr.plot.width=width_choice, repr.plot.height=height_choice)
            }
    

    示例

    数据

     x <-  c(37.50,46.79,48.30,46.04,43.40,39.25,38.49,49.51,40.38,36.98,40.00,38.49,37.74,47.92,44.53,44.91,44.91,40.00,41.51,47.92,36.98,43.40)
    

    调用带有尺寸的函数,并绘制绘图:

    set_plot_dimensions(6, 4)
    show_distribution(x, 'test vector')
    

    set_plot_dimensions(16, 4)
    show_distribution(x, 'test vector')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多