【问题标题】:Graphic Exporting in A Loop with Grid带网格的循环图形导出
【发布时间】:2017-12-22 12:51:24
【问题描述】:

我正在尝试开发将生成随机 5x5 矩阵(参见代码)、通过 ggplot2 显示并通过png() 导出对象位置的脚本。我已经打下了基础,但是(我认为)我遇到问题的地方是导出图像

library(ggplot2) 
library(grid)
### 5x5 Array ###
for(d1l in c(12, 9, 7)) {
  for(i in 1:20) {
a5 <- matrix(nrow = 5, ncol = 5)  # Empty 5x5 matrix
a5l <- length(a5)  # Number of cells in array
ind <- sample(x = length(a5), replace = F, size = a5l)  # Rand. indexes
a5[ind[1:d1l]] <- "X"  # Write objects to rand. indexes
a5[ind[d1l+1:24]] <- "O"  # Write remaining obj to rand. indexes
a5[ind[25]] <- "T"  # Place target

# Turn into data frame for ggplot2
testdf <- data.frame("row"=substr(levels(interaction(1:5, 1:5, sep="")),1,1),
           "col"=substr(levels(interaction(1:5, 1:5, sep="")),2,2),
           "val"=a5[1:25],
           "colour"=ifelse(a5[1:25]=="X", "green", "red"))

# Initiate graphics device
png(paste0("figs/", d1l,"items-interation",i,".png") , height=1080, width=1920)

# Generate plot
ggplot(testdf, aes(x=row, y=col, shape=val, colour=colour))+
  geom_point(position=position_jitter(width=.2, height=.2), size=15)+
  scale_shape_manual(values=c("O"=1, "X"=4, "T"=4))+
  scale_colour_manual(values=c("red"="red", "green"="green4"))+
  theme_minimal()+
  theme(panel.grid.major = element_blank(),
        axis.text = element_blank(),
        axis.title = element_blank(),
        legend.position = "none")

# Force editing
grid.force()

# Changing thickness of shapes
grid.edit("geom_point.points", grep = TRUE, gp = gpar(lwd = 5))

# Turn off gr device
dev.off()

  }
}

根据我运行脚本的位置,我会遇到不同的错误:

Error in (function (filename = "Rplot%03d.png", width = 480, height = 480,  : 
  unable to start png() device

Error in editDLfromGPath(gPath, specs, strict, grep, global, redraw) : 
  'gPath' (geom_point.points) not found

我很难解决我的格式有什么问题,因此非常感谢任何有助于理解错误的帮助。

【问题讨论】:

  • 你没有在这里问一个问题。您要解决的问题是什么?您看到的行为与您想要的行为有何不同?
  • @ChiPak 尝试导出每个单独的用作实验刺激
  • 你想用grid.edit做什么?如果您将其排除在外,其余代码将起作用。
  • @ChiPak 我正在增加图中形状的厚度,这是我第一次使用它,所以我对它的功能非常满意

标签: r loops ggplot2 graphics png


【解决方案1】:

---线条粗细---

您可以使用stroke 来控制形状的线条粗细

geom_point(position=position_jitter(width=.2, height=.2), size=15, stroke=5)

那么你就不需要使用gridgrid.editgrid.force(删除这些命令)。

----保存地块-----

您可以使用ggsave 保存您的ggplots。将ggsave 放在ggplot 之后

ggsave(paste0("figs/", d1l,"items-interation",i,".png"), width = 9, height = 9, dpi = 100)

----简化的建议----

您还可以通过以下方式简化您的 make-random-matrix 命令:

a5 <- sample(c(rep("X",d1l),rep("O",25-d1l)),replace=F)
a5[sample(1:25,1)] <- "T"
a5 <- matrix(a5,nrow=5)

可以替换:

a5 <- matrix(nrow = 5, ncol = 5)  # Empty 5x5 matrix
a5l <- length(a5)  # Number of cells in array
ind <- sample(x = length(a5), replace = F, size = a5l)  # Rand. indexes
a5[ind[1:d1l]] <- "X"  # Write objects to rand. indexes
a5[ind[d1l+1:24]] <- "O"  # Write remaining obj to rand. indexes
a5[ind[25]] <- "T"  # Place target

【讨论】:

    猜你喜欢
    • 2020-07-03
    • 2020-08-01
    • 2018-09-06
    • 2018-05-23
    • 2018-08-26
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多