gpt4 book ai didi

r - 独立于行构面的数量来控制ggplot2构面的高度

转载 作者:行者123 更新时间:2023-12-04 04:30:50 25 4
gpt4 key购买 nike

问题

ggplot2/knitr强制绘图(在LaTeX中)具有默认的宽高比。面数不同的两个图导致面高度不同的图。不好

我正在寻找一种解决方案,该解决方案可独立于行构面的数量来控制ggplot2构面的高度。

9月9日编辑:
该解决方案应该在一个块中工作,因为两个图都在一个块中(如示例代码中所示)。因此,调整与编织图相关的图块选项是不可行的,因为图块选项同样适用于两个图。

解决方案-但有问题

将每个图放入gridExtra框中,并根据构面的数量缩放其高度,结果构面的高度不再相同:更好但不是完美的。

  • 情节出现在页面上的余量太大,即使下面还有剩余的文本,因此情节也应更近一些。
  • 似乎可以裁剪由此操作的图。

  • 是否有一种更智能且无问题的解决方案,该解决方案可以独立于行构面的数量来控制ggplot2构面的高度?
    \documentclass[a4paper]{article}
    \usepackage[margin=1in]{geometry}

    \begin{document}

    <<setup, results='asis', message=FALSE, echo=FALSE>>=
    require(ggplot2)
    require(gridExtra)

    ### Generate two data frames

    # Data frame with 2 classes
    db.small = data.frame (
    class = as.factor(c(rep("A", 12), rep("B", 12))),
    month = as.factor(rep(1:12, 2)),
    value = runif(2*12, 0, 100)
    )

    # Data frame with 5 classes
    db.large = data.frame (
    class = as.factor(c(rep("A", 12), rep("B", 12), rep("C", 12), rep("C", 12), rep("D", 12))),
    month = as.factor(rep(1:12, 5)),
    value = runif(5*12, 0, 100)
    )

    # Generate plots
    plot1 = ggplot(db.small, aes(month, value)) + geom_bar(stat="identity") + facet_grid(class ~ .) + ylim(c(0, 100))
    plot2 = ggplot(db.large, aes(month, value)) + geom_bar(stat="identity") + facet_grid(class ~ .) + ylim(c(0, 100))
    @

    \section{Before: Native plots without modification}
    Ggplot2/knitr forces both plots to have the default aspect ratio. As both plots have different number of facets the result is that the facet heights are different. This is not nice.

    <<before, results='asis', message=FALSE, echo=FALSE, out.width="0.6\\linewidth">>=
    print(plot1)
    print(plot2)
    @

    \pagebreak
    \section{After: Plots with modification}
    Putting each plot into a gridExtra box and scaling it's height with the number of facets, results in facet heights which are no longer different: nicer but not perfect, see problems below.

    <<After, results='asis', message=FALSE, echo=FALSE, out.width="0.6\\linewidth">>=
    # Calculate number of facets for the two plots
    db.small.numberfacets = length(unique(db.small$class))
    db.large.numberfacets = length(unique(db.large$class))

    # Define heights for modification of plots
    facet.height = 4
    other.height = 2

    plot1 = plot1 + theme( plot.margin = unit(c(0, 0, 0, 0), "cm"))
    plot2 = plot2 + theme( plot.margin = unit(c(0, 0, 0, 0), "cm"))

    # Put plots inside a gridExtra box and change height plots
    grid.arrange(arrangeGrob(plot1, heights=unit(db.small.numberfacets*facet.height+other.height, "cm")), nrow=1)
    grid.arrange(arrangeGrob(plot2, heights=unit(db.large.numberfacets*facet.height+other.height, "cm")), nrow=1)
    @


    2 Problems:
    \begin{itemize}
    \item Plots appear on the page with too much margin, even there's remaining text below so plots shall move more closer.
    \item Seems the second plot is cropped, see the x-axis label.
    \end{itemize}

    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

    \end{document}

    pdf输出:

    最佳答案

    您知道knitr chunk options可以接受任意R表达式吗? example 40显示了如何动态计算fig.widthfig.height

    对于您的情况,您可以根据用于构面的因子变量中的级别数,为fig.height分配一个值。例如,

    \documentclass{article}

    \begin{document}
    <<setup>>=
    library(ggplot2)
    calc_height = function(f) length(levels(f))
    p = ggplot(diamonds, aes(color)) + geom_bar()
    @

    <<test-a, fig.width=7, fig.height=calc_height(diamonds$cut)>>==
    p + facet_grid(cut ~ .)
    @

    <<test-b, fig.width=7, fig.height=calc_height(diamonds$clarity)>>==
    p + facet_grid(clarity ~ .)
    @

    \end{document}

    输出:

    关于r - 独立于行构面的数量来控制ggplot2构面的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18683370/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com