gpt4 book ai didi

r - 在 Rscript 中使用 grid.edit() 的问题

转载 作者:行者123 更新时间:2023-12-04 00:10:47 25 4
gpt4 key购买 nike

我在使用 Rscript 中的 grid.edit() 时遇到问题。我正在使用 grid.edit() 来增加图例和图表中空心点的厚度。我从这篇文章 ( Change thickness of a marker in ggplot2 ) 中获取了这个。 IMO 看起来好多了。我知道从源文件和 Rscripts 中你可以获得 ggplot 对象来使用 print(p) 进行绘图,但我需要使用 grid.edit() 所以我不确定如何解决这个问题。下面的工作示例。

我的 R 脚本名为 test.r

library(ggplot2)
library(grid)
library(gtable)

p <- ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(shape = factor(cyl))) + scale_shape(solid = FALSE)
lwd = 2 # Set line width
g = ggplotGrob(p); dev.off() # Get the plot grob
# Get the indices for the legend: t = top, r = right, ...
indices <- c(subset(g$layout, name == "guide-box", select = t:r))
# Get the row number of the legend in the layout
rn <- which(g$layout$name == "guide-box")
# Extract the legend
legend <- g$grobs[[rn]]
# Get the legend keys
pointGrobs = which(grepl("points", legend$grobs[[1]]$grobs))
# Check them out - no line width set
# for (i in pointGrobs) str(legend$grobs[[1]]$grobs[[i]])
# Set line width
for (n in pointGrobs) legend$grobs[[1]]$grobs[[n]]$gp$lwd = lwd
# Check them out - line width set
# for (i in pointGrobs) str(legend$grobs[[1]]$grobs[[i]])
# Put the modified legend back into the plot grob
g$layout$clip[g$layout$name == "panel"] <- "off"
g = gtable_add_grob(g, legend, t=indices$t, l=indices$l)
###g$grobs[[4]]$children[[2]]$gp$lwd = gpar(lwd = lwd) # Alternative for setting lwd for points in the plot
grid.newpage()
grid.draw(g)
grid.edit("geom_point.points", grep = TRUE, gp = gpar(lwd = lwd))

dev.print(cairo_pdf,filename="Aplot.pdf",
width=11,
height=8.5)

我的批处理文件。

...\R-3.2.3\bin\x64\Rscript.exe test.r
PAUSE

脚本运行,我得到以下错误。

Error in editDLfromGPath(gPath,specs,strict,grep,global,redraw):
'gPath' (geom_point.points) not found
Calls: grid.edit -> editDLfromGPath
Execution halted

此外,一个名为 Rplots 的 PDF 被打印到我的工作目录。该图是默认大小,有趣的是,图例中的点很粗,但图中的点不是。似乎脚本在 grid.edit() 处失败,但 grid.draw() 成功。

最佳答案

您需要 grid.force() 绘图,以便网格编辑功能可以看到所有的 grob。我增加了点大小和线宽乘数,以便很明显编辑已生效。

library(ggplot2)
library(grid)
library(gtable)

p <- ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(shape = factor(cyl)), size = 5) + scale_shape(solid = FALSE)
lwd = 3 # Set line width
g = ggplotGrob(p); dev.off() # Get the plot grob
# Get the indices for the legend: t = top, r = right, ...
indices <- c(subset(g$layout, name == "guide-box", select = t:r))
# Get the row number of the legend in the layout
rn <- which(g$layout$name == "guide-box")
# Extract the legend
legend <- g$grobs[[rn]]
# Get the legend keys
pointGrobs = which(grepl("points", legend$grobs[[1]]$grobs))
# Check them out - no line width set
# for (i in pointGrobs) str(legend$grobs[[1]]$grobs[[i]])
# Set line width
for (n in pointGrobs) legend$grobs[[1]]$grobs[[n]]$gp$lwd = lwd
# Check them out - line width set
# for (i in pointGrobs) str(legend$grobs[[1]]$grobs[[i]])
# Put the modified legend back into the plot grob
g$layout$clip[g$layout$name == "panel"] <- "off"
g = gtable_add_grob(g, legend, t=indices$t, l=indices$l)
###g$grobs[[4]]$children[[2]]$gp$lwd = gpar(lwd = lwd) # Alternative for setting lwd for points in the plot
grid.newpage()
grid.draw(g)

grid.ls()
grid.ls(grid.force()) # Note the difference here

grid.force()
grid.edit("geom_point.points", grep = TRUE, gp = gpar(lwd = lwd))


# Or to edit the grob (rather than edit on screen)
g = editGrob(grid.force(g), "geom_point.points", grep = TRUE, gp = gpar(lwd = lwd))
grid.newpage()
grid.draw(g)

# To give it a print method
print.ggplotgrob <- function(x) {
grid.newpage()
grid.draw(x)
}
class(g) = c("ggplotgrob", class(g))

g

enter image description here

我的 session 信息()

R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
[5] LC_TIME=English_Australia.1252

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

other attached packages:
[1] Cairo_1.5-9 gtable_0.2.0 ggplot2_2.1.0

loaded via a namespace (and not attached):
[1] labeling_0.3 colorspace_1.2-6 scales_0.4.0 plyr_1.8.3
[5] tools_3.2.3 Rcpp_0.12.3 digest_0.6.9 munsell_0.4.3

关于r - 在 Rscript 中使用 grid.edit() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35950905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com