【问题标题】:Override aesthetics of custom plot覆盖自定义情节的美学
【发布时间】:2020-07-22 09:58:19
【问题描述】:

我不确定如何覆盖使用 ggplot 制作的自定义绘图的美学属性。我现在能想到的唯一方法是使用 grid 包的功能,虽然这真的很hackish。也许有更简单的方法,比如使用ggplot2 中的guides 左右,虽然我无法使其工作?

以下是我只想调整图表中线宽的示例。当然,我也希望这也能在传奇中流传下来。因此,以下是我使用grid 的步骤,但非常感谢任何更简单的解决方案(理想情况下,不需要grid 而只需要ggplot2,如果可能的话)。

library(iNEXT)
library(ggplot2)
library(grid)

# Some custom plot from the iNEXT package
data(spider)
out <- iNEXT(spider, q=0, datatype="abundance")

custom_plot <- ggiNEXT(out)
custom_plot

# Get the grobs
g <- grid.force(ggplotGrob(custom_plot))

# Check the list of names of grobs:
# grid.ls(g) 
# View(g$grobs)

# Get an idea about the grob paths
gpaths <- paste(gsub(pattern = "layout::", 
                     replacement = "", 
                     x = grid.ls(g, print = FALSE)$gPath), 
                grid.ls(g, print = FALSE)$name, 
                sep = "::")
gpaths[grepl("polyline", gpaths)]
#> [1] "panel.7-5-7-5::grill.gTree.114::panel.grid.minor.y..polyline.107"            
#> [2] "panel.7-5-7-5::grill.gTree.114::panel.grid.minor.x..polyline.109"            
#> [3] "panel.7-5-7-5::grill.gTree.114::panel.grid.major.y..polyline.111"            
#> [4] "panel.7-5-7-5::grill.gTree.114::panel.grid.major.x..polyline.113"            
#> [5] "panel.7-5-7-5::GRID.polyline.91"                                             
#> [6] "panel.7-5-7-5::geom_ribbon.gTree.101::geom_ribbon.gTree.95::GRID.polyline.93"
#> [7] "panel.7-5-7-5::geom_ribbon.gTree.101::geom_ribbon.gTree.99::GRID.polyline.97"

# Edit the width of the lines
g <- editGrob(grob = g, 
              gPath = gpaths[grepl("panel.7-5-7-5::GRID.polyline", gpaths)],
              gp = gpar(lwd = c(1,1,1,1)))
plot(g)

reprex package (v0.3.0) 于 2020 年 7 月 22 日创建

【问题讨论】:

    标签: r ggplot2 grid inext


    【解决方案1】:

    您正在寻找的答案在“自己绘制 R/E 曲线”下 https://cran.r-project.org/web/packages/iNEXT/vignettes/Introduction.html.

    幸运的是,该软件包的作者提供了函数 fortify() 以及一些逐字复制的代码,以实现您想要的。

    您应该从该部分复制以下内容,并根据自己的喜好更改 geom_line() 函数调用中的 lwd(线宽)参数。

    df <- fortify(out, type=1) # Note the type parameter!
    
    df.point <- df[which(df$method=="observed"),]
    df.line <- df[which(df$method!="observed"),]
    df.line$method <- factor(df.line$method, 
                             c("interpolated", "extrapolated"),
                             c("interpolation", "extrapolation"))
     
    ggplot(df, aes(x=x, y=y, colour=site)) + 
      geom_point(aes(shape=site), size=5, data=df.point) +
      geom_line(aes(linetype=method), lwd=1.5, data=df.line) +
      geom_ribbon(aes(ymin=y.lwr, ymax=y.upr,
                      fill=site, colour=NULL), alpha=0.2) +
      labs(x="Number of individuals", y="Species diversity") +
      theme(legend.position = "bottom", 
            legend.title=element_blank(),
            text=element_text(size=18),
            legend.box = "vertical") 
    

    【讨论】:

      【解决方案2】:

      我认为你让生活变得过于复杂。这种方法能满足您的需要吗?

      生成图

      plot <- mtcars %>% ggplot() + geom_line(aes(x=mpg, y=cyl, colour=as.factor(gear)))
      plot
      

      修改剧情

      plot + aes(size=5) + guides(size=FALSE)
      

      guides 调用取消了 size 的图例。显然,如果您确实希望显示图例,可以将其删除。

      更新

      在 cmets 中回答 OP 的问题。我同意:我的建议不会像我预测的那样修改ggiNEXT 的情节。

      我已经进行了一些挖掘。图中的多样性曲线由ggiNEXT.iNEXT函数中的以下语句生成

      g <- g + geom_line(aes_string(linetype = "lty"), lwd = 1.5) + ...
      

      我觉得这很奇怪。据我所知,lwd 不是ggplot2 的审美。 (并且"lty" 不是linetype 美学的有效值。但是,ltylwd 分别是ggplot2linetypesize 的基本R 等效项。)

      如果 lwd 是一个未记录的功能,我尝试过

      custom_plot + aes(lwd=3)
      

      但这没有效果。

      然后我将ggiNEXT.iNEXT 函数的主体复制到我自己的函数中,并将调用更改为geom_line 以读取

      g <- g + geom_line(aes_string(linetype = "lty"), size = 1.5)
      

      调用我的新函数产生的图与原始ggiNEXT.iNEXT 调用产生的图相同(至少在我看来)。那么

      custom_plot <- myPlot(out)
      custom_plot
      custom_plot + aes(size=3) + guides(size=FALSE)
      

      产生了预测的变化。所以我最好的建议是(1)创建一个本地版本的ggiNEXT.iNEXT 并在需要进行此修改时加载它[当然,您需要确保根据对“官方”版本] 或 (2) 从头开始​​创建图表。看ggiNEXT.iNEXT的源码,没那么复杂。

      这可能值得作为iNEXT 的作者提出的问题。

      【讨论】:

      • 嗨@Limey,您是否在提供的 iNEXT 自定义绘图上尝试过您提出的解决方案?我特意创建了那个例子。我似乎没有得到预期的结果。
      • 瓦伦丁,不,我没有。仅仅是因为我没有发现您的输入数据是包数据集。道歉。我没有安装 iNEXT 软件包,但我会看看我能做什么。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多