自定义情节的跨越美学

问题描述 投票:0回答:2
grid

软件包的功能,尽管确实是hackish。也许有一种更简单的方法,例如从guides

使用
ggplot2,尽管我无法设法使它起作用?
贝洛(Below)是我只想调整图表中的线宽度的一个示例。当然,我也希望在传奇中滴下来。因此,下面是我使用
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软件包(v0.3.0)在2020-07-22创建

您正在寻找的答案在“您自己绘制r/e曲线”下

https://cran.r-project.org/web/packages/inext/vignettes/introduction.html.

. 不幸的是,包装的作者提供了函数

Fortify()以及一些代码逐字复制,以实现您想要的东西。 您应从该部分复制以下内容,然后更改GEOM_LINE()函数call call to your liking的

lwd
r ggplot2 grid inext
2个回答
2
投票
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")

我认为您使生活过于复杂。 这种方法会给您您需要的东西吗? 构成一个情节 plot <- mtcars %>% ggplot() + geom_line(aes(x=mpg, y=cyl, colour=as.factor(gear))) plot

修改图 plot + aes(size=5) + guides(size=FALSE)


2
投票

guides

呼叫抑制了

size

的传说。 显然,如果您希望传奇出现,则可以将其删除。

updateoriginal plot 在评论中对OP的问题表示反应。我同意:我的建议不会像我预期的那样修改

ggiNEXT

图。 我做了一些挖掘。 图中的多样性曲线是由以下语句在

ggiNEXT.iNEXT
函数中产生的。
g <- g + geom_line(aes_string(linetype = "lty"), lwd = 1.5) + ...

我发现这个奇怪。 据我所知,modified plotlwd不是

ggplot2

的美学。 (而

"lty"
不是
linetype
美的有效值。但是,

lty

lwd分别是ggplot2

linetype

的基础等效物。)

case

size

是一个无证件的功能,我尝试了

lwd
但这没有效果。
然后,我将

custom_plot + aes(lwd=3)

函数的主体复制到我自己的功能中,然后更改了呼叫
ggiNEXT.iNEXT
geom_line
称呼我的新功能产生了一个与原始调用所产生的曲线相同的情节(至少在我眼中)。然后
g <- g + geom_line(aes_string(linetype = "lty"), size = 1.5)
产生了预测的变化。  因此,我最好的建议是(1)创建本地版本的
ggiNEXT.iNEXT
并在需要进行此修改时加载它[当然,然后您需要确保您将本地副本与任何更改的任何更改更新为“官方”版本]或(2)从头开始创建图形。 查看
custom_plot <- myPlot(out)
custom_plot
custom_plot + aes(size=3) + guides(size=FALSE)
的源代码,并不是那么复杂。
这可能值得与
ggiNEXT.iNEXT
的作者一起提出的问题。
    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.