R中画的一些方块保存下来后连成了一片,怎样能让他放大能显示中间的空白部分
时间: 2024-03-17 14:44:44 浏览: 25
如果您是使用基础绘图函数中的 `rect()` 函数画的方块,可以在保存时增加一些参数来防止方块连成一片。具体来说,可以在保存函数中添加 `bg = "transparent"` 参数,将方块的填充颜色设置为透明,这样就能够在放大后显示出方块之间的空白部分。
例如,假设您要画一个由多个方块组成的图,可以使用以下代码保存为高分辨率的pdf或png文件,并设置 `bg = "transparent"` 参数:
```R
# 创建图形
plot.new()
rect(1, 1, 2, 2, col = "red")
rect(3, 1, 4, 2, col = "green")
rect(1, 3, 2, 4, col = "blue")
rect(3, 3, 4, 4, col = "yellow")
# 保存为pdf
pdf("plot_name.pdf", width = 10, height = 8, units = "in")
plot(0, 0, xlim = c(0, 5), ylim = c(0, 5), xaxs = "i", yaxs = "i", type = "n", xlab = "", ylab = "")
rect(1, 1, 2, 2, col = "red", bg = "transparent")
rect(3, 1, 4, 2, col = "green", bg = "transparent")
rect(1, 3, 2, 4, col = "blue", bg = "transparent")
rect(3, 3, 4, 4, col = "yellow", bg = "transparent")
dev.off()
# 保存为png
png("plot_name.png", width = 2000, height = 1600, res = 300)
plot(0, 0, xlim = c(0, 5), ylim = c(0, 5), xaxs = "i", yaxs = "i", type = "n", xlab = "", ylab = "")
rect(1, 1, 2, 2, col = "red", bg = "transparent")
rect(3, 1, 4, 2, col = "green", bg = "transparent")
rect(1, 3, 2, 4, col = "blue", bg = "transparent")
rect(3, 3, 4, 4, col = "yellow", bg = "transparent")
dev.off()
```
在上述代码中,我们在保存函数中添加了 `bg = "transparent"` 参数,将方块的填充颜色设置为透明。这样,在保存为pdf或png文件时,方块之间的空白部分就不会被填充了,可以在放大后显示出来。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)