-1

How do I visualize a 3 dimensional matrix?

  • first dimension is x-coordinate = different universities
  • second dimension is y-coordinate = disciplines or specialties (Physics, Mathematics, Art ...)
  • third dimension is color = different years
  • size of the circles = number of papers in corresponding discipline/university
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
perlatex
  • 37
  • 1
  • 8
  • 1
    Does this chart type have a name or is it custom-made? Are you asking how to create a similar type of chart in R? If you know the chart type you can find which package can create it. – Panagiotis Kanavos Oct 16 '15 at 11:21
  • Looks like you also have a 4th dimension in the size of the circles... – Rodrigo Oct 16 '15 at 11:37
  • @Rodrigo, number of papers = the size of the circles; color = years – perlatex Oct 16 '15 at 12:43
  • @ Panagiotis Kanavos , I want to create a similar type of chart in R, But i do not know which type of chart and which package can creat it. – perlatex Oct 16 '15 at 12:47
  • any chance you could give some example data? Something like `ggplot(data)+geom_point(aes(x=university,y=discipline,colour=year, size=num_papers)` should work ... – Ben Bolker Oct 16 '15 at 16:45

4 Answers4

2

Using @alexis_laz's example data:

Reorganize data to be ggplot2-friendly:

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

Load packages (including viridis for prettier colours):

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

Plot as requested (play with size ranges until you like the result):

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 However, ggplot2 gives you lots of freedom, and I would suggest that you pay attention to the Cleveland hierarchy, which says that it's hard to distinguish quantitative features plotted by size. Depending on what comparisons you're most interested in, you might try something like this:

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

(Of course, the data look like a mess because they're randomly generated ...)

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
1

Assuming you have data like:

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)

You could try something along the lines of:

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

Which gives: enter image description here

alexis_laz
  • 12,884
  • 4
  • 27
  • 37
0

You can do something like

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
Rodrigo
  • 4,706
  • 6
  • 51
  • 94
0

====================data============

num univ dis paper cited year

1 Beijing Physics 193 4555 2005

2 Beijing Physics 197 2799 2006

3 Beijing Physics 240 2664 2007

4 Beijing Physics 200 3191 2008

5 Beijing Physics 268 2668 2009

6 Beijing Physics 249 2300 2010

7 Beijing Physics 262 2080 2011

8 Beijing Physics 230 2371 2012

9 Beijing Physics 309 1367 2013

10 Beijing Physics 284 615 2014

11 Beijing Chemistry 143 1650 2005

12 Beijing Chemistry 149 2379 2006

13 Beijing Chemistry 190 2566 2007

14 Beijing Chemistry 147 1888 2008

15 Beijing Chemistry 184 2146 2009

16 Beijing Chemistry 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))

========================================================== but, the year becomes 2005.0 2007.5 2010.0 2012.5 enter image description here

perlatex
  • 37
  • 1
  • 8