覆盖自定义情节的美学
Override aesthetics of custom plot
我不确定如何覆盖使用 ggplot 制作的自定义绘图的美学属性。我现在能想到的唯一方法是使用 grid
包的功能,尽管它确实很老套。也许有更简单的方法,比如使用 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-07-22 创建
我认为你让生活变得过于复杂。这种方法是否能满足您的需求?
生成绘图
plot <- mtcars %>% ggplot() + geom_line(aes(x=mpg, y=cyl, colour=as.factor(gear)))
plot
修改剧情
plot + aes(size=5) + guides(size=FALSE)
guides
调用抑制了 size
的图例。显然,如果您确实希望出现图例,可以将其删除。
更新
回复OP在评论中的问题。我同意:我的建议不会像我预测的那样修改 ggiNEXT
图。
我已经做了一些挖掘。图中的多样性曲线由 ggiNEXT.iNEXT
函数
中的以下语句生成
g <- g + geom_line(aes_string(linetype = "lty"), lwd = 1.5) + ...
我觉得这很奇怪。据我所知,lwd
不是ggplot2
的审美。 (并且 "lty"
不是 linetype
美学的有效值。但是,lty
和 lwd
是 ggplot2
的 linetype
和 size
分别。)
万一 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
的作者提出。
您要找的答案在“自己画R/E曲线”下
https://cran.r-project.org/web/packages/iNEXT/vignettes/Introduction.html.
幸运的是,包的作者提供了函数 fortify() 以及一些逐字复制的代码,以实现您想要的。
您应该从该部分复制以下内容并更改 geom_line()[=23= 中的 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")
我不确定如何覆盖使用 ggplot 制作的自定义绘图的美学属性。我现在能想到的唯一方法是使用 grid
包的功能,尽管它确实很老套。也许有更简单的方法,比如使用 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-07-22 创建我认为你让生活变得过于复杂。这种方法是否能满足您的需求?
生成绘图
plot <- mtcars %>% ggplot() + geom_line(aes(x=mpg, y=cyl, colour=as.factor(gear)))
plot
修改剧情
plot + aes(size=5) + guides(size=FALSE)
guides
调用抑制了 size
的图例。显然,如果您确实希望出现图例,可以将其删除。
更新
回复OP在评论中的问题。我同意:我的建议不会像我预测的那样修改 ggiNEXT
图。
我已经做了一些挖掘。图中的多样性曲线由 ggiNEXT.iNEXT
函数
g <- g + geom_line(aes_string(linetype = "lty"), lwd = 1.5) + ...
我觉得这很奇怪。据我所知,lwd
不是ggplot2
的审美。 (并且 "lty"
不是 linetype
美学的有效值。但是,lty
和 lwd
是 ggplot2
的 linetype
和 size
分别。)
万一 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
的作者提出。
您要找的答案在“自己画R/E曲线”下 https://cran.r-project.org/web/packages/iNEXT/vignettes/Introduction.html.
幸运的是,包的作者提供了函数 fortify() 以及一些逐字复制的代码,以实现您想要的。
您应该从该部分复制以下内容并更改 geom_line()[=23= 中的 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")