R循环-绘制边距和绘制区域

标签 r plot circos circlize

我正在尝试使用r库“circlize”复制此网站https://gjabel.wordpress.com/2014/03/28/circular-migration-flow-plots-in-r/的图形。
不幸的是,我有两个问题:

首先,我得到了所有绘图区域的警告(即Note: 1 point is out of plotting region in sector 'Mexico', track '1')。我以为问题是在circos.textcircos.axis中,因为我将它们与direction一起使用,这是一个已弃用的函数。但是也使用facing而不是direction,问题仍然存在。因此,我想我不明白该警告的含义。您有什么提示可以帮助我吗?

而且,在我的情节中,链接与分段之间相距甚远。因此,我尝试减少track.margin来解决问题,但现在这些段的名称不在页边距范围内,因此无法对其进行可视化显示。有没有更好的方法来解决此问题?

这是我到目前为止所写的内容(几乎完全取自该网站https://github.com/null2/globalmigration)

library("circlize")
library("plyr")
library("migest")
#load data
m<-read.table(system.file("science", "country_custom.txt", package = "migest"), skip=2, stringsAsFactors=FALSE)
#1)a data.frame to store information on each segment of the circle to be plotted
df1<-m[,1:3]
names(df1)<-c("order","rgb","region")
df1$region<-gsub("\\.", "\n", df1$region)
#2) a matrix containing the flow data (in this example only 28 countries)
m<-m[,-(1:3)]/1e05
m<-as.matrix(m)
dimnames(m)<-list(orig=df1$region,dest=df1$region)
#sort order of data.frame and matrix for plotting in circos    
df1<-arrange(df1, order) #reordering a data frame by its columns
df1$region <- factor(df1$region, levels=df1$region)
m<-m[levels(df1$region),levels(df1$region)]
#define ranges of circos sectors and their colors (both of the sectors and the links)
#determine the length of segments on the outside of the plot.
df1$xmin <- 0
df1$xmax <- rowSums(m)+colSums(m) #inflows+outflows
#set the colour names for segments and flows
n<-nrow(df1)
df1 <- cbind(df1, matrix(as.numeric(unlist(strsplit(df1$rgb,","))),nrow=n, byrow=TRUE) )
names(df1)[ncol(df1)-2:0]<-c("r","g","b")
df1$rcol<-rgb(df1$r, df1$g, df1$b, max = 255)
df1$lcol<-rgb(df1$r, df1$g, df1$b, alpha=200, max = 255)
##plot sectors
windows()
par(mar=rep(0,4))
circos.clear()
#1)basic circos graphic parameters
circos.par(cell.padding=c(0,0,0,0), track.margin=c(0,0.01), start.degree = 90, gap.degree =4)
#2)sector details
circos.initialize(factors = df1$region, xlim = cbind(df1$xmin, df1$xmax))
#3)plot sectors
circos.trackPlotRegion(ylim = c(0, 1), factors = df1$region, track.height=0.1,
       panel.fun = function(x, y) {
         #select details of current sector
         name = get.cell.meta.data("sector.index")
         i = get.cell.meta.data("sector.numeric.index")
         xlim = get.cell.meta.data("xlim")
         ylim = get.cell.meta.data("ylim")

         #text direction (dd) and adjusmtents (aa)
         theta = circlize(mean(xlim), 1.3)[1, 1] %% 360
         dd <- ifelse(theta < 90 || theta > 270, "vertical_right", "vertical_left")
         aa = c(1, 0.5)
         if(theta < 90 || theta > 270)  aa =c(0, 0.5)

         #plot country labels
         circos.text(x=mean(xlim), y=1.7, labels=name, direction = dd,
         cex=0.6,adj = aa)
         #circos.text(x=mean(xlim), y=2, labels=name, facing = "bending",cex=0.6)             

         #plot main sector
         circos.rect(xleft=xlim[1], ybottom=ylim[1], xright=xlim[2], ytop=ylim[2], 
         col = df1$rcol[i], border=df1$rcol[i])

         #blank in part of main sector
         circos.rect(xleft=xlim[1], ybottom=ylim[1], xright=xlim[2]-rowSums(m)[i], ytop=ylim[1]+0.3, 
         col = "white", border = "white")

         #white line all the way around
         circos.rect(xleft=xlim[1], ybottom=0.3, xright=xlim[2], ytop=0.32, col = "white", border = "white")

         #plot axis
         #NOTE: Ticks indicate the number of migrants in 100s.
         circos.axis(labels.cex=0.6, direction = "outside", major.at=seq(from=0,to=floor(df1$xmax)[i],by=5), 
         minor.ticks=1, labels.away.percentage = 0.15)
       })

非常感谢您的帮助。

编辑:
我添加了脚本的第二部分,因为似乎有必要解决第二个问题。
#plot links
#create a new dataframe containing the long form of the matrix m
#add sum values to df1, marking the x-position of the first links out (sum1) and in (sum2). Updated for further links in loop below.
df1$sum1 <- colSums(m) #outflows
df1$sum2 <- numeric(n)
#create a data.frame of the flow matrix sorted by flow size, to allow largest flow plotted first
df2<-cbind(as.data.frame(m),orig=rownames(m),  stringsAsFactors=FALSE)
#long matrix
df2<-reshape(df2, idvar="orig", varying=list(1:n), direction="long", timevar="dest", time=rownames(m),  v.names = "m") 
df2<-arrange(df2,desc(m))
#keep only the largest flows to avoid clutter
df2<-subset(df2, m>quantile(m,0.925))
#plot links
for(k in 1:nrow(df2)){
require("circlize")
#i,j reference of flow matrix
#note: you are selecting the states in region1 according to the edgelist in region2
i<-match(df2$orig[k],df1$region)
j<-match(df2$dest[k],df1$region)
#plot link
#sector.index1=sender
#point1=size of the base of the link at the origin
#We set the origin segment to start at the current sum of outflows from the sender country (df1$sum1[i])    
#We set the end of the segment outflow equal to the total outflows from the sender country, plus the flow from the edge we considered
circos.link(sector.index1=df1$region[i], point1=c(df1$sum1[i], df1$sum1[i] + abs(m[i, j])),
sector.index2=df1$region[j], point2=c(df1$sum2[j], df1$sum2[j] + abs(m[i, j])),
col = df1$lcol[i]) #, top.ratio==0.66, top.ratio.low==0.67)
#note: The height and thickness of the link at its mid-point is determined by the top.ratio and the top.ratio.low argument
#update sum1 and sum2 for use when plotting the next link
df1$sum1[i] = df1$sum1[i] + abs(m[i, j])
df1$sum2[j] = df1$sum2[j] + abs(m[i, j])
}

最佳答案

关于注释消息的第一个问题并不是真正的问题。无论警告如何,您仍然可以获取所需的绘图。您可以使用以下方式征服消息

circos.par(points.overflow.warning=FALSE)

第二个问题是对circlize软件包的最新更新的结果。您需要通过添加以下内容来从其初始设置更改轨道边距
circos.par(track.margin=c(0,0)) 

circos.trackPlotRegion命令之后以及在circos.link循环中通过for函数绘制链接之前。

对不起,有问题。我一直想在一段时间内更新migest包中的演示文件,但在假期中却忘记了它。

关于R循环-绘制边距和绘制区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27871768/

相关文章:

javascript - D3和弦图,带确切位置

Python 负二项式回归 - 结果与 R 中的结果不匹配

将绘图限制为 r 中的 shapefile 边界

r - 从图中删除线

r - 如何绘制 field 指数曲线族?

javascript - 错误 : <path> attribute d: Expected number, "….43564672524005LNaN,NaNZ"

r - 如何在 R 中创建 for 循环来进行这种特殊的计算

r - 带有 dplyr::do 的匿名函数 - 使用 rle 的结果来过滤数据

r - Shiny - 在 observeEvent 中调用 API 后更新 UI

macos - 在 mac 10.9 中安装 libgd 时出错