[R] Diagonal population density

Julio Rojas jcredberry at ymail.com
Tue Mar 22 15:35:48 CET 2011


Dear all. I have to plot a the marginal population density for a heat map that represents the population density of a city. I have been able to plot the heat map in the lower left corner, the marginal density in x in the upper left corner and the marginal density in y in the lower left corner. What I need is to change this plot to include the marginal density in the diagonal direction of 135 degress (z=x+y). I have the data, but I need to build the layout to include this plot. That would mean having a 2x3 layout, with the heat map in the lower-center position, the marginal density in the upper-center position, the marginal density (vertically oriented) in the lower-right position and the diagonal density (diagonally oriented) in the upper-left position. How can I do this? I will include the function I'm using to plot the data thus far:


plot.city<-function(city,fcity,n=2,operator="max"){
  def.par <- par(no.readonly = TRUE)
  nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1), c(1,3), TRUE)
  par(mar=c(3,3,1,1))
  x<-as.numeric(colnames(city$data))
  y<-as.numeric(rownames(city$data))
  fx<-colSums(city$data) 
  fx<-(fx-min(fx))/(max(fx)-min(fx)) #Relative Marginal Density in X
  fy<-rowSums(city$data)
  fy<-(fy-min(fy))/(max(fy)-min(fy)) #Relative Marginal Density in Y
  image(x,y,t(city$data),xlab="",ylab="") #plotting city
  ### plotting fuzzy membership function of city
 contour(color(fcity,min_x=min(x),max_x=max(x),min_y=min(y),max_y=max(y),px=length(x),py=length(y),n=n,operator=operator),add=T,xlab="",ylab="",lwd=1.5)  
  ###
  par(mar=c(0,3,1,1)) #upper-left corner
  ### plot fuzzy membership function of X
  plot(fcity$x,c(0,1,1,0),type="l",axes=F,frame.plot=TRUE,xlab="",ylab="",panel.first=grid(),lwd=2)
  ###
  lines(x,fx,col="darkred",lwd=2)  #plot Relative Marginal Density in X
  axis(side=1, labels=FALSE)
  axis(side=2)
  par(mar=c(3,0,1,1)) #lower-right corner
  ### plotting fuzzy membership function of Y
  plot(c(0,1,1,0),fcity$y,type="l",axes=F,frame.plot=TRUE,ylim=c((min(y)-0.25),max(y)+0.25),xlab="",ylab="",panel.first=grid(),lwd=2)   lines(fy,y,col="darkred",lwd=2)
  ###
  axis(side=2, labels=FALSE)
  axis(side=1)
  par(def.par)
  layout(mat=1)
}

Thanks a lot.







More information about the R-help mailing list