【问题标题】:Boxplot and xyplot overlapped箱线图和 xyplot 重叠
【发布时间】:2014-08-04 08:43:57
【问题描述】:

我使用bwplot 库的bwplot 函数对我的数据进行了条件箱线图。

    A1 <- bwplot(measure ~ month | plot , data = prueba,
              strip = strip.custom(bg = 'white'),   
              cex = .8, layout = c(2, 2),
              xlab = "Month", ylab = "Total",
              par.settings = list(
                box.rectangle = list(col = 1),
                box.umbrella  = list(col = 1),
                plot.symbol   = list(cex = .8, col = 1)),
              scales = list(x = list(relation = "same"),
                            y = list(relation = "same")))

然后,我做了一个 xyplot,因为我想将降水数据添加到上一个图表中,同时使用来自 lattice 库的 xyplot

    B1 <- xyplot(precip ~ month | plot, data=prueba,
   type="b",
   ylab = '% precip',
   xlab = 'month',
   strip = function(bg = 'white', ...)
     strip.default(bg = 'white', ...),
   scales = list(alternating = F,
                 x=list(relation = 'same'),
                 y=list(relation = 'same')))

我尝试使用来自gridExtra 库的grid.arrange 将它们绘制在同一张图上:

    grid.arrange(A1,B1)

但是有了这个,我没有重叠数据,但是结果是这样的

如何在由 plot 条件的箱线图“内部”绘制降水数据?

谢谢

【问题讨论】:

    标签: r lattice boxplot bwplot


    【解决方案1】:

    像 Andrie 一样使用 barley 数据,使用 latticeExtra 的另一种方法:

    library(lattice)
    library(latticeExtra)
    
    bwplot(yield ~ year | variety , data = barley, fill = "grey") +
    xyplot(yield ~ year | variety , data = barley, col = "red")
    

    【讨论】:

    • 谢谢@Pascal。它看起来非常简单易行,但它不适用于我的数据。我使用了 bwplot(...) + as.layer(xyplot(...)),但它只绘制 bwplot,没有给出任何错误。
    • 为什么要使用as.layer()?示例中没有as.layer()。当您尝试同时绘制总数和百分比时,我并不感到惊讶它不起作用。
    • 对不起,我使用了as.layer(),因为没有它,错误说Error in as.layer(lay) : argument "lay" is missing, with no default
    • 无论如何,现在我已经在没有as.layer() 的情况下再次尝试并且错误没有出现,一切似乎都可以正常工作,但它只绘制了第一个情节(如果我先使用bwplot 它绘制它,如果我先使用 xyplot 它只会绘制它
    • 当然,% PrecipTotal 的 33 倍。
    【解决方案2】:

    您需要创建自定义面板功能。我用内置的barley数据演示:

    假设您想使用barley 数据创建一个简单的bwplotxyplot。您的代码可能如下所示:

    library(lattice)
    
    bwplot(yield ~ year | variety , data = barley)
    xyplot(yield ~ year | variety , data = barley)
    

    要合并绘图,您需要创建一个面板函数,该函数首先绘制默认的panel.bwplot,然后是panel.xyplot。试试这个:

    bwplot(yield ~ year | variety , data = barley,
           panel = function(x, y, ...){
             panel.bwplot(x, y, fill="grey", ...)
             panel.xyplot(x, y, col="red", ...)
           }
    )
    


    ?xyplot 的帮助中有一些关于执行此操作的信息 - 向下滚动到 panel 参数的详细信息。

    【讨论】:

    • 我需要了解面板功能如何正常工作,我会尝试。谢谢你的回答。
    猜你喜欢
    • 1970-01-01
    • 2019-03-17
    • 2021-05-19
    • 2012-11-02
    • 2014-03-31
    • 2021-12-12
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    相关资源
    最近更新 更多