在 ggalt 包中使用 "geom_xspline" 时的空白绘图输出

Blank Plot Output when using "geom_xspline" in ggalt package

当尝试将 ggalt 中的 geom_xsplineggpubr 中的 ggarrange 结合使用时,输出为空白,在使用 [ 清除之前无法绘制其他图=18=].

在我的用例中,我希望 geom_xspline 替换我的 ggplot 对象中的一些现有 geom_line。有人知道使用从其他 R 包添加的 geom 的问题吗?

这里有一些代码可以比较,没什么有趣的,只是举一个可重现的例子:

初始工作代码w/ogeom_xspline

library(ggplot2)
library(ggpubr)
myplot = ggplot(data = mtcars, aes(x = wt, y = mpg)) +
geom_line()
ggarrange(myplot, myplot) # Works and outputs fine

使用 ggalt 包失败的代码

library(ggalt)
library(ggplot2)
library(ggpubr)
myplot = ggplot(data = mtcars, aes(x = wt, y = mpg)) +
geom_xspline()
ggarrange(myplot, myplot) # Output becomes blank and freezes the plot panel

替代方法 我没有使用 ggarrange,而是尝试了此 link 中的函数 grid_arrange_shared_legend,它使用 gridgridExtra。但是,我还是很好奇为什么 ggarrange 不起作用。

这是我的会话信息:

R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggpubr_0.1.8  magrittr_1.5  ggplot2_3.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.18       pillar_1.3.0       compiler_3.5.1     RColorBrewer_1.1-2 plyr_1.8.4         bindr_0.1.1       
 [7] tools_3.5.1        extrafont_0.17     tibble_1.4.2       gtable_0.2.0       pkgconfig_2.0.1    rlang_0.2.1       
[13] rstudioapi_0.7     yaml_2.2.0         bindrcpp_0.2.2     Rttf2pt1_1.3.7     withr_2.1.2        dplyr_0.7.6       
[19] maps_3.3.0         grid_3.5.1         ggalt_0.4.0        tidyselect_0.2.4   cowplot_0.9.3      glue_1.3.0        
[25] R6_2.2.2           purrr_0.2.5        extrafontdb_1.0    scales_1.0.0       MASS_7.3-50        assertthat_0.2.0  
[31] proj4_1.0-8        colorspace_1.3-2   labeling_0.3       KernSmooth_2.23-15 ash_1.0-15         lazyeval_0.2.1    
[37] munsell_0.5.0      crayon_1.3.4

快速添加,如果我将对象转换为 ggplotGrob(),它将与 ggarrange 一起使用,但当我尝试使用 common.legend = T.[=32= 时它将失败]

好吧,我不确定为什么 ggpubr::ggarrange 在与 ggalt::geom_xspline 一起使用时会导致绘图面板失败,但我可以告诉你绘图仍在创建中,只是现在才显示在绘图面板上。

所以似乎将它们一起使用会导致绘图设备出现故障,并且它只发生在 ggalt::geom_xspline 而不是 . That is a so you are on the right track posting to GitHub 中的所有 geoms

您可以通过 运行 下面的代码检查:

library(ggalt)
library(ggplot2)
library(ggpubr)


myplot = ggplot(data = mtcars, aes(x = wt, y = mpg)) +
  geom_xspline()
myplot

g <- ggarrange(myplot, myplot) # Output becomes blank and freezes the plot panel
g

jpeg('rplot.jpg')
g
dev.off()
#> pdf 
#>   3

reprex package (v0.3.0)

于 2019-05-30 创建

这是保存的情节:

geom_xspline 所基于的 xspline 函数通常使用 graphics 自动绘图。这导致 ggalt 包作者找到了一些 work-arounds 以确保它能与 ggplot 很好地配合。我的粗略解决方案都涉及在不使用 xspline 的情况下从 ggplot 创建或调整 geomstat。这使得在使用 ggplot.

摄取之前无需大量 pre-processing 数据就可以更轻松地使用

(1) 新 stat 使用 splines

使用 spline 代替 xspline 进行点插值。

# Create a new stat (adjusted from ggalt GitHub page)
stat_spline <- function(mapping = NULL, data = NULL, geom = "line",
                         position = "identity", na.rm = TRUE, show.legend = NA, inherit.aes = TRUE,
                         n=200, method = "fmm", ...) { # Just picking a rough default for n
  layer(
    stat = StatSpline,
    data = data,
    mapping = mapping,
    geom = geom,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(n=n,
                  method=method,
                  na.rm = na.rm,
                  ...
    )
  )
}

StatSpline <- ggproto("StatSpline", Stat,

                       required_aes = c("x", "y"),

                       compute_group = function(self, data, scales, params,
                                                n=200, method = "fmm") {

                         tmp <- spline(data$x, data$y, n = n, method = method, ties = mean)

                         data.frame(x=tmp$x, y=tmp$y)
                       }

)

# Plot with ggarrange
myplot = ggplot(data = mtcars, aes(x = wt, y = mpg)) +
stat_spline(mapping = aes(x = wt, y = mpg)) +
geom_point()

ggpubr::ggarrange(myplot, myplot)

如果您想要类似于 Catmull-Rom 而不是 Cubic 的样条曲线,则此方法并不理想;你可以看到控制点之间有一些大的弯曲。

(2) 使用 xsplineGrob

新建 geom

这是 ggalt

geom_xspline2 的略微调整版本
# Create new geom based upon code from ggalt GitHub page
GeomXSpline3 <- ggproto("GeomXSpline3", Geom,
                        required_aes = c("x", "y"),
                        default_aes = aes(colour = "black", shape=-1, open=T),
                        draw_key = draw_key_point,

                        draw_panel = function(data, panel_params, coord) {
                          coords <- coord$transform(data, panel_params)
                          grid::xsplineGrob(
                            coords$x, coords$y,
                            shape = coords$shape, 
                            open = coords$open[1],
                            gp = grid::gpar(col = coords$colour)
                          )
                        }
)

geom_xspline3 <- function(mapping = NULL, data = NULL, stat = "identity",
                          position = "identity", na.rm = FALSE, show.legend = NA,
                          inherit.aes = TRUE, ...) {
  layer(
    geom = GeomXSpline3, mapping = mapping,  data = data, stat = stat,
    position = position, show.legend = show.legend, inherit.aes = inherit.aes,
    params = list(na.rm = na.rm, ...)
  )
}

# Plot with ggarrange
myplot = ggplot(data = mtcars, aes(x = wt, y = mpg)) +
  geom_xspline3(shape = -.25) + geom_point()
ggpubr::ggarrange(myplot, myplot) 

在确保形状参数仍然接受介于 -1 和 1 之间的输入方面存在一些问题,但是,现在 ggarrange 似乎可以正常工作。

我在编写此解决方案时使用了以下资源: