R编程(气泡图可视化)

R编程(气泡图可视化),r,visualization,data-analysis,bubble-chart,R,Visualization,Data Analysis,Bubble Chart,我有一个乘客出行频率数据集: CountryOrigin-有40多个国家名称 (印度、澳大利亚、中国、日本、巴淡岛、巴厘岛、新加坡) CountryDestination-有40多个国家/地区名称 (印度、澳大利亚、中国、日本、巴淡岛、巴厘岛、新加坡) 我需要x轴上的原点位置名称和y轴上的目的地名称,频率应表示为气泡的大小。对于这些(或任何类型)绘图,我将使用ggplot2。让我们首先创建一些测试数据: countries = c('IND', 'AUS', 'CHI', 'JAP', 'BAT

我有一个乘客出行频率数据集:

CountryOrigin-有40多个国家名称
(印度、澳大利亚、中国、日本、巴淡岛、巴厘岛、新加坡)
CountryDestination-有40多个国家/地区名称 (印度、澳大利亚、中国、日本、巴淡岛、巴厘岛、新加坡)


我需要x轴上的原点位置名称和y轴上的目的地名称,频率应表示为气泡的大小。

对于这些(或任何类型)绘图,我将使用
ggplot2
。让我们首先创建一些测试数据:

countries = c('IND', 'AUS', 'CHI', 'JAP', 'BAT', 'SING')
frequencies = matrix(sample(1:100, 36), 6, 6, dimnames = list(countries, countries))
diag(frequencies) = 0
然后做情节。首先,我们必须将矩阵数据转换为合适的格式:

library(reshape2)
frequencies_df = melt(frequencies)
names(frequencies_df) = c('origin', 'destination', 'frequency')
并使用
ggplot2

library(ggplot2)
ggplot(frequencies_df, aes(x = origin, y = destination, size = frequencies)) + geom_point()

我想留下另一种方式来可视化这些数据。您可以使用R中的
Circleze
migest
软件包来可视化人们是如何移动或旅行的。您需要进行大量编码,但如果您遵循
migest
中的演示,仍然可以创建您想要的东西。你需要一个矩阵和一个数据框来绘制这个图。但是,一旦你把它们弄对了,你就可以使用演示中的代码了。在本例中,您可以看到来自6个国家的人是如何旅行的。例如,澳大利亚人在这些虚假数据中访问了日本、马来西亚和印度;到达这些国家的三条红线。如果队伍更宽,那就意味着有更多的人访问这些国家。同样,中国人访问了澳大利亚、日本和马来西亚。我把密码留在这里了

library(circlize)
library(migest)
library(dplyr)

m <- data.frame(order = 1:6,
            country = c("Ausralia", "India", "China", "Japan", "Thailand", "Malaysia"),
            V3 = c(1, 150000, 90000, 180000, 15000, 10000),
            V4 = c(35000, 1, 10000, 12000, 25000, 8000),
            V5 = c(10000, 7000, 1, 40000, 5000, 4000),
            V6 = c(7000, 8000, 175000, 1, 11000, 18000),
            V7 = c(70000, 30000, 22000, 120000, 1, 40000),
            V8 = c(60000, 90000, 110000, 14000, 30000, 1),
            r = c(255,255,255,153,51,51),
            g = c(51, 153, 255, 255, 255, 255),
            b = c(51, 51, 51, 51, 51, 153),
            stringsAsFactors = FALSE)

### Create a data frame
df1 <- m[, c(1,2, 9:11)]

### Create a matrix
m <- m[,-(1:2)]/1e04
m <- as.matrix(m[,c(1:6)])
dimnames(m) <- list(orig = df1$country, dest = df1$country)


### Sort order of data.frame and matrix for plotting in circos

df1 <- arrange(df1, order)

df1$country <- factor(df1$country, levels = df1$country)

m <- m[levels(df1$country),levels(df1$country)]


### Define ranges of circos sectors and their colors (both of the sectors and the links)

df1$xmin <- 0

df1$xmax <- rowSums(m) + colSums(m)

n <- nrow(df1)

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 (outer part)
##

par(mar=rep(0,4))

circos.clear()

### Basic circos graphic parameters
circos.par(cell.padding=c(0,0,0,0), track.margin=c(0,0.15), start.degree = 90, gap.degree =4)

### Sector details
circos.initialize(factors = df1$country, xlim = cbind(df1$xmin, df1$xmax))

### Plot sectors

circos.trackPlotRegion(ylim = c(0, 1), factors = df1$country, track.height=0.1,
                      #panel.fun for each sector
                      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, "clockwise", "reverse.clockwise")
                      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, facing = dd, cex=0.6,  adj = aa)

                      #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
                      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 (inner part)
##

### 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)
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)

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.6))

### Plot links

for(k in 1:nrow(df2)){
    #i,j reference of flow matrix
    i<-match(df2$orig[k],df1$country)
    j<-match(df2$dest[k],df1$country)

#plot link
circos.link(sector.index1=df1$country[i], point1=c(df1$sum1[i], df1$sum1[i] + abs(m[i, j])),
            sector.index2=df1$country[j], point2=c(df1$sum2[j], df1$sum2[j] + abs(m[i, j])),
            col = df1$lcol[i])

#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])
}

库(圆圈)
图书馆(migest)
图书馆(dplyr)

m我可能会使用
googleVis
软件包。。。可能有助于您开始。是否可以避免在图表中映射频率0值!!这可能可以通过将对角线上的值设置为
NA
来实现。您能解释一下在这个可视化中定义的第二个外圆是什么吗?谢谢@Lathaavishwana外圆表示旅行者的总数。有两条线。外粗线表示旅客总数,内细线表示来自不同国家的旅客。例如,较细的红线表示来自澳大利亚的旅客人数。你看到三行读到目的地,对吗?其余部分用白色细线表示访客总数。你们可以看到绿色、黄色和橙色的线条延伸到那个里。也就是说,澳大利亚有来自日本、印度和中国的游客。更多细节,请看一下migest演示。如果我们有一个非常大的数据帧ie(200行:200列),我认为很难为r、g和b编码不同的颜色组合。有什么简单的方法可以解决吗@Lathaavishwana比200 x 200更大。如果你可以通过对组(例如区域)进行分类来减小矩阵的大小,那么你可能会有很好的可视化效果,我认为。@Lathaavishwana比这更愉快!希望你能找到你的路。感谢您给我机会在这里介绍
migest
想法+1.
library(circlize)
library(migest)
library(dplyr)

m <- data.frame(order = 1:6,
            country = c("Ausralia", "India", "China", "Japan", "Thailand", "Malaysia"),
            V3 = c(1, 150000, 90000, 180000, 15000, 10000),
            V4 = c(35000, 1, 10000, 12000, 25000, 8000),
            V5 = c(10000, 7000, 1, 40000, 5000, 4000),
            V6 = c(7000, 8000, 175000, 1, 11000, 18000),
            V7 = c(70000, 30000, 22000, 120000, 1, 40000),
            V8 = c(60000, 90000, 110000, 14000, 30000, 1),
            r = c(255,255,255,153,51,51),
            g = c(51, 153, 255, 255, 255, 255),
            b = c(51, 51, 51, 51, 51, 153),
            stringsAsFactors = FALSE)

### Create a data frame
df1 <- m[, c(1,2, 9:11)]

### Create a matrix
m <- m[,-(1:2)]/1e04
m <- as.matrix(m[,c(1:6)])
dimnames(m) <- list(orig = df1$country, dest = df1$country)


### Sort order of data.frame and matrix for plotting in circos

df1 <- arrange(df1, order)

df1$country <- factor(df1$country, levels = df1$country)

m <- m[levels(df1$country),levels(df1$country)]


### Define ranges of circos sectors and their colors (both of the sectors and the links)

df1$xmin <- 0

df1$xmax <- rowSums(m) + colSums(m)

n <- nrow(df1)

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 (outer part)
##

par(mar=rep(0,4))

circos.clear()

### Basic circos graphic parameters
circos.par(cell.padding=c(0,0,0,0), track.margin=c(0,0.15), start.degree = 90, gap.degree =4)

### Sector details
circos.initialize(factors = df1$country, xlim = cbind(df1$xmin, df1$xmax))

### Plot sectors

circos.trackPlotRegion(ylim = c(0, 1), factors = df1$country, track.height=0.1,
                      #panel.fun for each sector
                      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, "clockwise", "reverse.clockwise")
                      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, facing = dd, cex=0.6,  adj = aa)

                      #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
                      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 (inner part)
##

### 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)
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)

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.6))

### Plot links

for(k in 1:nrow(df2)){
    #i,j reference of flow matrix
    i<-match(df2$orig[k],df1$country)
    j<-match(df2$dest[k],df1$country)

#plot link
circos.link(sector.index1=df1$country[i], point1=c(df1$sum1[i], df1$sum1[i] + abs(m[i, j])),
            sector.index2=df1$country[j], point2=c(df1$sum2[j], df1$sum2[j] + abs(m[i, j])),
            col = df1$lcol[i])

#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])
}