Search code examples
rimageaxis-labels

R image axis labels and ticks


I am trying to add x-axis labels to an image but failing to understand why I can't add the labels. I wrote this code below and I do not see any axis labels after running it.

    data = matrix(c(0,1), nrow = 500, ncol = 383)
    w<-dim(data)[1]*2;
    h<-dim(data)[2]*2;
    my_palette <- gray.colors(256,start=0,end=1,rev=TRUE);
    tiff(file="data_1.tiff",width=w,height=h,units="px");
    par(mar=c(10,10,10,10));
    image(data, xaxt= "n", yaxt= "n", bty="n", col=my_palette,useRaster=TRUE);
    axis(side=1,at=seq(0,500,by=10)[2:51],labels=TRUE)
    dev.off()

Additionally, when I remove at=seq(0,500,by=10)[2:51] an x-axis appears but only stays between 0 and 1.

    data = matrix(c(0,1), nrow = 500, ncol = 383)
    w<-dim(data)[1]*2;
    h<-dim(data)[2]*2;
    my_palette <- gray.colors(256,start=0,end=1,rev=TRUE);
    tiff(file="data_2.tiff",width=w,height=h,units="px");
    par(mar=c(10,10,10,10));
    image(data, xaxt= "n", yaxt= "n", bty="n", col=my_palette,useRaster=TRUE);
    axis(side=1,labels=TRUE)
    dev.off()

Please help me understand why I cannot add my own labels and ticks to this data like other examples in R I have seen so far. Any help is appreciated and please guide me if there is way to add my own ticks and labels to an image like this or any other image.


Solution

  • For some reason, the x axis is limited to (0,1); which is evident when I try,

    axis(side=1,at=seq(0,1,by=.02)[2:51],labels=T,xpd=NA)

    I get

    enter image description here

    So, a workaround may be to create ticks between (0,1) but label them (0,500)

    axis(side=1,at=seq(0,1,by=.02)[2:51],labels=seq(0,500,by=10)[2:51],xpd=NA) gives

    enter image description here