如何可视化三维矩阵?
答案 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))
但是,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"))
(当然,数据看起来像一团糟,因为它们是随机生成的......)
答案 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)
答案 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