#reading the file with the R values (step 1 in Fig. 1)
      
         d <-    read.table("journalsstat.txt",
    header=TRUE, sep="\t", na.strings="", dec=".", strip.white=TRUE)

s=dim(d)
         
dm=as.matrix(d,nrow=s[1],ncol=s[2])

        
kk=as.double(dm)

m=matrix(kk,nrow=s[1],ncol=s[2])

la=names(d) #abreviated names of the journals




#Step 2 in Fig.1

rr=rank(m,  ties.method = "max")


rrm=matrix(rr,nrow=s[1],ncol=s[2])


  
#changing from similarity to dissimilarity (ne=25*25=625)
#Step 3 in Fig. 1
ne=s[1]*s[2]

urrm=ne-rrm

#Step 4 in Fig. 1
dda=cbind(urrm,t(urrm)) #dimension: (25,25*2)

s2=s[1]*2

#H-plot

p=prcomp(dda)
v=p$rotation
pr=p$sdev
pv=cbind(pr[1]*v[1:s2,1],pr[2]*v[1:s2,2])

#Fig. 3 with names
plot(pv,type='n',asp=1,xlab='Dim 1',ylab='Dim 2')
text(pv[1:s[1],1],pv[1:s[1],2],la,cex=0.7)
text(pv[(s[1]+1):s2,1],pv[(s[1]+1):s2,2],la,col=2,cex=0.7)
  text(100,105,"Cited")
  text(100,90,"Citing",col=2)
 points(100,90,pch=1,col=2,cex=6.5)


#Fig. 3 with numbers
 windows()
 plot(pv,type='n',asp=1,xlab='Dim 1',ylab='Dim 2')
 text(pv[1:s[1],1],pv[1:s[1],2],1:s[1])
text(pv[(s[1]+1):s2,1],pv[(s[1]+1):s2,2],1:s[1],col=2)
 points(pv[(s[1]+1):s2,1],pv[(s[1]+1):s2,2],pch=1,col=2,cex=3)
  text(100,105,"Cited")
  text(100,90,"Citing",col=2)
 points(100,90,pch=1,col=2,cex=6.5)

#asymmetry in journals

as=c()
for (i in 1:s[1]){
as[i]=sqrt(sum((pv[i,1:2]-pv[i+s[1],1:2])^2))
}

#goodness-of-fit
cumsum(pr^4)/sum(pr^4)


#Unfolding
library(smacof) #for computing unfolding


res <- smacofRect(urrm)

fm=res$stress

resf=res

set.seed(2013)
for (i in 1:100){

init= list(x=matrix(runif(s[1]*2,-300,400),nrow=s[1],ncol=2), y=matrix(runif(s[1]*2,-300,400),nrow=25,ncol=2))
res <- smacofRect(urrm,ndim=2,weightmat = NULL, init = init)

if (res$stress <fm){
resf=res
}

}

pv=cbind(resf$conf.col,resf$conf.row)

#unfolding with names
plot(pv,type='n',asp=1,xlab='Dim 1',ylab='Dim 2')
text(pv[1:s[1],1],pv[1:s[1],2],la,cex=0.7)
text(pv[1:s[1],3],pv[1:s[1],4],la,col=2,cex=0.7)
 text(300,160,"Cited")
  text(300,125,"Citing",col=2)
 points(300,125,pch=1,col=2,cex=6.5)


#unfolding with numbers
plot(pv,type='n',asp=1,xlab='Dim 1',ylab='Dim 2')
text(pv[1:s[1],1],pv[1:s[1],2],1:s[1])
text(pv[1:s[1],3],pv[1:s[1],4],1:s[1],col=2)
points(pv[1:s[1],3],pv[1:s[1],4],pch=1,col=2,cex=3)
  text(300,160,"Cited")
  text(300,125,"Citing",col=2)
 points(300,125,pch=1,col=2,cex=6.5)



#Fig. 2

 split.screen(c(1,2)) 
screen(1)
#unfolding with numbers
plot(pv,type='n',asp=1,xlab='Dim 1',ylab='Dim 2')
text(pv[1:s[1],1],pv[1:s[1],2],1:s[1])
text(pv[1:s[1],3],pv[1:s[1],4],1:s[1],col=2)
points(pv[1:s[1],3],pv[1:s[1],4],pch=1,col=2,cex=3)
  text(300,160,"Cited")
  text(300,125,"Citing",col=2)
 points(300,125,pch=1,col=2,cex=6.5)



#trasnposing: changing roles of individuals and objects
 urrm2=t(urrm)

res <- smacofRect(urrm2)


fm=res$stress

resf=res

set.seed(2013)
for (i in 1:100){

init= list(x=matrix(runif(s[1]*2,-300,300),nrow=s[1],ncol=2), y=matrix(runif(s[1]*2,-300,300),nrow=25,ncol=2))
res <- smacofRect(urrm,ndim=2,weightmat = NULL, init = init,itmax=2000)

if (res$stress <fm){
resf=res
}

}

pvt=cbind(resf$conf.col,resf$conf.row)




#unfolding with names
plot(pvt,type='n',asp=1,xlab='Dim 1',ylab='Dim 2')
text(pvt[1:s[1],1],pvt[1:s[1],2],la,col=2,cex=0.7)
text(pvt[1:s[1],3],pvt[1:s[1],4],la,col=1,cex=0.7)
 text(150,200,"Cited")
  text(150,160,"Citing",col=2)
 points(150,160,pch=1,col=2,cex=6.5)


#unfolding with numbers
plot(pvt,type='n',asp=1,xlab='Dim 1',ylab='Dim 2')
text(pvt[1:s[1],1],pvt[1:s[1],2],1:s[1],col=2)
text(pvt[1:s[1],3],pvt[1:s[1],4],1:s[1],col=1)
points(pvt[1:s[1],1],pvt[1:s[1],2],pch=1,col=2,cex=3)
  text(150,200,"Cited")
  text(150,160,"Citing",col=2)
 points(150,160,pch=1,col=2,cex=6.5)


 

#Fig. 2

 split.screen(c(1,2)) 
screen(1)
#unfolding with numbers
plot(pv,type='n',asp=1,xlab='Dim 1',ylab='Dim 2')
text(pv[1:s[1],1],pv[1:s[1],2],1:s[1])
text(pv[1:s[1],3],pv[1:s[1],4],1:s[1],col=2)
points(pv[1:s[1],3],pv[1:s[1],4],pch=1,col=2,cex=3)
  text(300,160,"Cited")
  text(300,125,"Citing",col=2)
 points(300,125,pch=1,col=2,cex=6.5)

screen(2)
plot(pvt,type='n',asp=1,xlab='Dim 1',ylab='Dim 2')
text(pvt[1:s[1],1],pvt[1:s[1],2],1:s[1],col=2)
text(pvt[1:s[1],3],pvt[1:s[1],4],1:s[1],col=1)
points(pvt[1:s[1],1],pvt[1:s[1],2],pch=1,col=2,cex=3)
  text(150,200,"Cited")
  text(150,160,"Citing",col=2)
 points(150,160,pch=1,col=2,cex=6.5)


#Fig. 4

#25*25*.75=625*.75= 468.75
library(sna)

 set.seed(1)
  gplot(rrm,label=1:25,thresh=469,mode="spring",edge.lwd=0.00000000000000000001) 
