image() with a vector

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

image() with a vector

Steven Cordwell
Hi,

I have a vectors x and z, for example,

x <- 0:20
z <- round(runif(20,1,7))
y <- 0.5

and I want to display z as an image. However if I then call image() with a vector

image(x,y,t(z),zlim=c(1,7),col=heat.colors(7),xlab="Year",ylab="Action",yaxt="n",xaxs="r",yaxs="r")

then I get the error

Error in image.default(x, y, t(z), zlim = c(1, 7), col = heat.colors(7),  :
  dimensions of z are not length(x)(-1) times length(y)(-1)

However, transforming z into a matrix with two rows, where both rows are the same as z above does seem to be a workaround.

z <- matrix(c(z,z),2,length(z),byrow=TRUE)
y <- c(0.25,0.75)
image(x,y,t(z),zlim=c(1,7),col=heat.colors(7),xlab="Year",ylab="Action",yaxt="n",xaxs="r",yaxs="r")

The problem is that when I include the figure that is produced in my pdf, there ends up being a white line that runs through the middle horizontally (where the divison of the cells is) which ruins the presentation of the graphic as x
is a time series, and z is an action that is performed at that time. There is only one action that is performed at each time step in x, and having the line running through the middle might cause confusion as to how many actions are
performed at any given time.

I would appreciate any suggestions as to how to get around this, because I haven't been able to find any options for vectors in image(), or if there is a different function that does it.

Cheers

Steven

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: image() with a vector

plangfelder
On Tue, Feb 15, 2011 at 8:43 PM, Steven Cordwell <[hidden email]> wrote:

> Hi,
>
> I have a vectors x and z, for example,
>
> x <- 0:20
> z <- round(runif(20,1,7))
> y <- 0.5
>
> and I want to display z as an image. However if I then call image() with a vector
>
> image(x,y,t(z),zlim=c(1,7),col=heat.colors(7),xlab="Year",ylab="Action",yaxt="n",xaxs="r",yaxs="r")

try

image(x,y,as.matrix(z),zlim=c(1,7),col=heat.colors(7),xlab="Year",ylab="Action",yaxt="n",xaxs="r",yaxs="r")

as.matrix(vector) converts the vector into a 1-column matrix, which
seems to be what you need.

Peter

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: image() with a vector

Dieter Menne
In reply to this post by Steven Cordwell
Steven Cordwell wrote
However, transforming z into a matrix with two rows, where both rows are the same as z above does seem to be a workaround.

z <- matrix(c(z,z),2,length(z),byrow=TRUE)
y <- c(0.25,0.75)
image(x,y,t(z),zlim=c(1,7),col=heat.colors(7),xlab="Year",ylab="Action",yaxt="n",xaxs="r",yaxs="r")

The problem is that when I include the figure that is produced in my pdf, there ends up being a white line that runs through the middle horizontally (where the divison of the cells is) which ruins the presentation of
This looks more like a problem of pdf than one of image. Under Windows and current R, I cannot reproduce the line in the pdf, but we have seen this before. If you are sure that you have the current version of R installed, you could send in a bug report, but make sure sessionInfo() is included.

One workaround that worked for me in the past to use a CairoPDF as show below.
Dieter

library(Cairo)
CairoPDF(file="c:/tmp/Hello.pdf")
x <- 0:20
y <- 0.5
z <- round(runif(21,1,7))
z <- matrix(c(z,z),2,length(z),byrow=TRUE)
y <- c(0.25,0.75)
image(x,y,t(z),zlim=c(1,7),col=heat.colors(7),xlab="Year",ylab="Action",
  yaxt="n",xaxs="r",yaxs="r")
dev.off()


Reply | Threaded
Open this post in threaded view
|

Re: image() with a vector

baptiste auguie-5
In reply to this post by Steven Cordwell
Hi,

rasterImage may alleviate the pdf artefacts in the viewer. This seems
to produce a similar output,

plot(range(x),range(y), t="n")
rasterImage(t(rgb(colorRamp(heat.colors(7))(z/max(z))/255)),
min(x),min(y),max(x),max(y), interpolate=FALSE)

HTH,

baptiste

On 16 February 2011 05:43, Steven Cordwell <[hidden email]> wrote:

> Hi,
>
> I have a vectors x and z, for example,
>
> x <- 0:20
> z <- round(runif(20,1,7))
> y <- 0.5
>
> and I want to display z as an image. However if I then call image() with a vector
>
> image(x,y,t(z),zlim=c(1,7),col=heat.colors(7),xlab="Year",ylab="Action",yaxt="n",xaxs="r",yaxs="r")
>
> then I get the error
>
> Error in image.default(x, y, t(z), zlim = c(1, 7), col = heat.colors(7),  :
>  dimensions of z are not length(x)(-1) times length(y)(-1)
>
> However, transforming z into a matrix with two rows, where both rows are the same as z above does seem to be a workaround.
>
> z <- matrix(c(z,z),2,length(z),byrow=TRUE)
> y <- c(0.25,0.75)
> image(x,y,t(z),zlim=c(1,7),col=heat.colors(7),xlab="Year",ylab="Action",yaxt="n",xaxs="r",yaxs="r")
>
> The problem is that when I include the figure that is produced in my pdf, there ends up being a white line that runs through the middle horizontally (where the divison of the cells is) which ruins the presentation of the graphic as x
> is a time series, and z is an action that is performed at that time. There is only one action that is performed at each time step in x, and having the line running through the middle might cause confusion as to how many actions are
> performed at any given time.
>
> I would appreciate any suggestions as to how to get around this, because I haven't been able to find any options for vectors in image(), or if there is a different function that does it.
>
> Cheers
>
> Steven
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: image() with a vector

Steven Cordwell
In reply to this post by Dieter Menne
On Wed, 16 Feb 2011 05:16:07 pm Dieter Menne wrote:

> Steven Cordwell wrote:
> > However, transforming z into a matrix with two rows, where both rows are
> > the same as z above does seem to be a workaround.
> >
> > z <- matrix(c(z,z),2,length(z),byrow=TRUE)
> > y <- c(0.25,0.75)
> > image(x,y,t(z),zlim=c(1,7),col=heat.colors(7),xlab="Year",ylab="Action",y
> > axt="n",xaxs="r",yaxs="r")
> >
> > The problem is that when I include the figure that is produced in my pdf,
> > there ends up being a white line that runs through the middle
> > horizontally (where the divison of the cells is) which ruins the
> > presentation of
>
> This looks more like a problem of pdf than one of image. Under Windows and
> current R, I cannot reproduce the line in the pdf, but we have seen this
> before. If you are sure that you have the current version of R installed,
> you could send in a bug report, but make sure sessionInfo() is included.
>
> One workaround that worked for me in the past to use a CairoPDF as show
> below.
> Dieter
>
> library(Cairo)
> CairoPDF(file="c:/tmp/Hello.pdf")
> x <- 0:20
> y <- 0.5
> z <- round(runif(21,1,7))
> z <- matrix(c(z,z),2,length(z),byrow=TRUE)
> y <- c(0.25,0.75)
> image(x,y,t(z),zlim=c(1,7),col=heat.colors(7),xlab="Year",ylab="Action",
>   yaxt="n",xaxs="r",yaxs="r")
> dev.off()

I agree, it probably does have something to do with the device. Actually, I'm
using pgfSweave so it might be something there. Though the solution of making
sure that the input to image() is a matrix fixes it.

On Wed, 16 Feb 2011 05:09:15 pm Peter Langfelder wrote:

>
> try
>
> image(x,y,as.matrix(z),zlim=c(1,7),col=heat.colors(7),xlab="Year",ylab="Act
> ion",yaxt="n",xaxs="r",yaxs="r")
>
> as.matrix(vector) converts the vector into a 1-column matrix, which
> seems to be what you need.
>
> Peter


Thanks

Steve

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.