在R语言中绘制网络图并保存为PDF文件

本示例展示了如何在R语言中使用igraph包绘制网络图,并使用pdf()函数将其保存为PDF文件。

代码示例:

# 假设t_net_its、tits_nodesizes、coords_t_its、t_mods_list_cs已经定义

# 设置绘图参数
par(mfrow=c(1,3), mar=c(0,0,0,0))

t_cols <- c('antiquewhite','antiquewhite1','antiquewhite2','antiquewhite3','antiquewhite4')

# 打开PDF文件
pdf('p1_network.pdf', width=7, height=3.5)

# 绘制网络图
plot(t_net_its, open = TRUE, vertex.label=NA, vertex.size=tits_nodesizes, layout=coords_t_its,
     mark.groups=list(t_mods_list_cs$'1',t_mods_list_cs$'2',t_mods_list_cs$'3',t_mods_list_cs$'4',t_mods_list_cs$'8'),
     mark.col=t_cols, mark.border=t_cols)

# 关闭PDF文件
dev.off()

代码说明:

  1. par(mfrow=c(1,3), mar=c(0,0,0,0)): 设置绘图参数,将图形分为1行3列,并设置边距为0。
  2. t_cols <- c('antiquewhite','antiquewhite1','antiquewhite2','antiquewhite3','antiquewhite4'): 定义颜色向量,用于设置图中不同组节点的颜色。
  3. pdf('p1_network.pdf', width=7, height=3.5): 打开PDF文件,文件名为'p1_network.pdf',宽高分别为7英寸和3.5英寸。
  4. plot(t_net_its, ...): 绘制网络图,其中参数包括:
    • t_net_its: 网络图对象。
    • open = TRUE: 设置节点为打开状态。
    • vertex.label=NA: 不显示节点标签。
    • vertex.size=tits_nodesizes: 设置节点大小。
    • layout=coords_t_its: 设置图的布局。
    • mark.groups=...: 设置不同组节点的标识。
    • mark.col=t_cols: 设置节点的颜色。
    • mark.border=t_cols: 设置节点边框颜色。
  5. dev.off(): 关闭PDF文件。

注意:

  • t_net_its、tits_nodesizes、coords_t_its、t_mods_list_cs 是示例代码中使用的变量,需要根据实际情况替换为自己的变量。
  • 调整代码中的参数可以改变图的外观和大小。
  • 在运行代码前需要确保igraph包已经安装。

原文地址: https://www.cveoy.top/t/topic/ngX2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录