首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在ggalt软件包中使用"geom_xspline“时的空白绘图输出

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

Stack Overflow用户
提问于 2019-05-31 05:51:32
回答 1查看 374关注 0票数 1

尝试将ggalt中的geom_xsplineggpubr中的ggarrange结合使用时,输出为空,并且在使用dev.off()清除之前无法进行其他绘图。

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

以下是一些代码进行比较,没有什么真正感兴趣的,只是为了给出一个可重复的例子:

不带 geom_xspline初始工作代码

代码语言:javascript
复制
library(ggplot2)
library(ggpubr)
myplot = ggplot(data = mtcars, aes(x = wt, y = mpg)) +
geom_line()
ggarrange(myplot, myplot) # Works and outputs fine

出现 ggalt 时出现失败的包代码

代码语言:javascript
复制
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不能工作。

这是我的会话信息:

代码语言:javascript
复制
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时,它将失败。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-13 00:24:46

geom_xspline所基于的xspline函数通常使用graphics自动打印。这使得ggalt包的作者找到了一些替代方法,以确保它可以很好地与ggplot配合使用。我的粗略解决方案包括在不使用xspline的情况下从ggplot创建或调整geomstat。这使得它更易于使用,而无需在使用ggplot摄取数据之前对数据进行大量预处理。

使用stat splines(1)新的

使用spline而不是xspline插值点。

代码语言:javascript
复制
# 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而不是立方体,则此方法不是理想的;您可能会看到控制点之间的一些大弯曲。

使用geom xsplineGrob的(2)新的

这是来自ggalt的略微调整过的geom_xspline2版本

代码语言:javascript
复制
# 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中工作得很好。

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

  • 一篇来自ggalt

geom_xspline2

  • ggplot vignette的作者在blog上发布的关于扩展ggplot

<代码>F251的文章

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56386084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档