如何可视化三维矩阵?

时间:2015-10-16 11:16:11

标签: r matrix visualize

如何可视化三维矩阵?

  • 第一维是x坐标=不同的大学
  • 第二个维度是y坐标=学科或专业(物理,数学,艺术......)
  • 第三维是color = different years
  • 圈子的大小=相应学科/大学的论文数量

4 个答案:

答案 0 :(得分:2)

使用@ alexis_laz的示例数据:

将数据重组为ggplot2 - 友好:

library("reshape2")
mm <- melt(mat)

加载包(包括viridis以获得更漂亮的颜色):

library("ggplot2"); theme_set(theme_bw())
library("viridis")

根据要求绘制(使用大小范围,直到您喜欢结果):

ggplot(mm)+
    geom_point(aes(x=univ,y=dis,colour=yr, size=value))+
        scale_color_viridis()+
            scale_size(range=c(2,18))

enter image description here 但是,ggplot2为您提供了很多自由,我建议您注意Cleveland hierarchy,它表示很难区分按大小绘制的定量特征。根据您最感兴趣的比较,您可以尝试这样的事情:

library(grid)  ## for unit(), to squash panels
ggplot(mm,aes(x=yr,y=value,colour=univ))+
    geom_point()+geom_line()+
        facet_wrap(~dis)+
            scale_color_brewer(palette="Set1")+
                theme(panel.margin=unit(0,"lines"))

enter image description here

(当然,数据看起来像一团糟,因为它们是随机生成的......)

答案 1 :(得分:1)

假设您有以下数据:

set.seed(911)
mat = tapply(sample(0:200, 5*10*16, TRUE, prob = rev(prop.table(0:200))), 
             expand.grid(univ = paste("univ", 1:5, sep = ""), 
                         dis = paste("dis", 1:10, sep = ""), 
                         yr = 2000:2015), 
             I)

您可以尝试以下方式:

#convert to easy to manipulate format            
DF = as.data.frame(as.table(mat))

#x
xlvs = unique(DF$univ)
xx = match(DF$univ, xlvs)

#y
ylvs = unique(DF$dis)
yy = match(DF$dis, ylvs)

#colors
collvs = unique(DF$yr)
cols = terrain.colors(length(collvs), alpha = 0.75)[match(DF$yr, collvs)]

#sizes
maxcex = 5
cexs = (DF$Freq * maxcex) / max(DF$Freq)


layout(matrix(c(1, 1, 1, 1, 1, 2), nrow = 1))
#plot 1
plot(xx, yy, col = cols, cex = cexs, pch = 19, axes = FALSE, frame.plot = TRUE)
axis(1, at = seq_along(xlvs), labels = xlvs)
axis(2, at = seq_along(ylvs), labels = ylvs, las = 1)   

#plot 2 
par(mar = c(1, 1, 1, 4))
fill = terrain.colors(length(collvs) * 9, alpha = 0.75)  #higher 'resolution' of plot
barplot(matrix(rep_len(1L, length(fill))), col = fill, border = NA, axes = FALSE)
axis(4, at = seq(1, length(fill), 9) + 4, labels = collvs, las = 1) 

给出了: enter image description here

答案 2 :(得分:0)

您可以执行类似

的操作
fulldata <- read.csv(...) # your data.frame
colors=c(...) # create your color array here
plot(NULL,xlim=1:9,ylim=1:20) # just to define the area of the graph
abline(v=1:9,h=1:20) # the axis inside the graph
for (y in 2013:2002) {
    data <- fulldata[which(fulldata$year == y),]
    circle(data$university,data$discipline,size=data$numberofpapers,col=year[y-2001])
}
axis(...) # or mtext or whatever to put the labels on the axis

答案 3 :(得分:0)

====================数据============

num univ dis paper引用年份

1北京物理193 4555 2005

2北京物理学197 2799 2006

3北京物理学240 2664 2007

4北京物理学200 3191 2008

5北京物理学268 2668 2009

6北京物理249 2300 2010

7北京物理262 2080 2011

8北京物理学230 2371 2012

9北京物理309 1367 2013

10北京物理284 615 2014

11北京化学143 1650 2005

12北京化学149 2379 2006

13北京化学190 2566 2007

14北京化学147 1888 2008

15北京化学184 2146 2009

16北京化学214 2568 2010

---

  mm <- read.table("data.txt", header = TRUE, sep = "", encoding='UTF-8')

  library("ggplot2")
  theme_set(theme_bw())
  library("viridis")

  ggplot(mm)+
  geom_point(aes(x=univ,y=dis,colour=year, size=paper))+
  scale_color_viridis()+
  scale_size(range=c(2,18))

=============================================== =========== 但是,这一年变成了2005.0 2007.5 2010.0 2012.5 enter image description here