4

I wonder how to add an arrow to point directly to a certain value on a scale next to a plot.

Let's say that I have different value in different years. I want to know what is the value in a particular year by pointing it to the scale directly. For example, I want to know what is the value in 2010. Then the scale would be pointing like in the image below:

In this image, the arrow was added. enter image description here

What I would really appreciate (and I don't know how to do this), is printing only the scale with the arrow without the graph so that I can put the scale where I want onto another graph. A bit like this: enter image description here

Here is the code to reproduce the image, without the arrow (highly influenced by this code to produce a scale).

effort = structure(c(-1.2696471203597, 0.176082447349155, 1.17697214807067, 
                          -0.101942469517932, -1.04722718686603, -0.99162220349261, 0.0648724806023204, 
                          2.40028178228585, 0.00926749722890297, -0.101942469517932, 0.00926749722890297, 
                          -0.324362403011601), .Dim = c(12L, 1L), "`scaled:center`" = 127.333333333333, "`scaled:scale`" = 71.9359984902405, decostand = "standardize", .Dimnames = list(
                            c("2003", "2004", "2005", "2006", "2007", "2008", "2009", 
                              "2010", "2011", "2012", "2013", "2014"), NULL))
plot(effort, type = "n")

image.scale <- function(z, zlim, col = heat.colors(12),
                        breaks, horiz=TRUE, ylim=NULL, xlim=NULL, ...){
  if(!missing(breaks)){
    if(length(breaks) != (length(col)+1)){stop("must have one more break than colour")}
  }
  if(missing(breaks) & !missing(zlim)){
    breaks <- seq(zlim[1], zlim[2], length.out=(length(col)+1)) 
  }
  if(missing(breaks) & missing(zlim)){
    zlim <- range(z, na.rm=TRUE)
    zlim[2] <- zlim[2]+c(zlim[2]-zlim[1])*(1E-3)#adds a bit to the range in both directions
    zlim[1] <- zlim[1]-c(zlim[2]-zlim[1])*(1E-3)
    breaks <- seq(zlim[1], zlim[2], length.out=(length(col)+1))
  }
  poly <- vector(mode="list", length(col))
  for(i in seq(poly)){
    poly[[i]] <- c(breaks[i], breaks[i+1], breaks[i+1], breaks[i])
  }
  xaxt <- ifelse(horiz, "s", "n")
  yaxt <- ifelse(horiz, "n", "s")
  if(horiz){YLIM<-c(0,1); XLIM<-range(breaks)}
  if(!horiz){YLIM<-range(breaks); XLIM<-c(0,1)}
  if(missing(xlim)) xlim=XLIM
  if(missing(ylim)) ylim=YLIM
  plot(1,1,t="n",ylim=ylim, xlim=xlim, xaxt=xaxt, yaxt=yaxt, xaxs="i", yaxs="i", ...)  
  for(i in seq(poly)){
    if(horiz){
      polygon(poly[[i]], c(0,0,1,1), col=col[i], border=NA)
    }
    if(!horiz){
      polygon(c(0,0,1,1), poly[[i]], col=col[i], border=NA)
    }
  }
}

# source("image.scale.R")

layout(matrix(c(1,2,3,0,4,0), nrow=1, ncol=2), widths=c(4,4,1), heights=c(4,1))
layout.show(4)

pal.1=colorRampPalette(c("red", "orange", "yellow"), space="rgb")
breaks <- seq(min(effort),max(effort),length.out = 10)
par(mar=c(1,1,1,8))
image(effort,col=pal.1(length(breaks)-1), breaks=breaks, xaxt="n", ylab="", xlab="")
#Add additional graphics
points(x = 1:12, y = effort , 
       pch=1, lwd=2, cex=1,col="black")

#Add scale
par(mar=c(1,1,1,3))
image.scale(effort, col=pal.1(length(breaks)-1), breaks=breaks, horiz=FALSE, yaxt="n")
axis(4,at=breaks, las=2)

1 Answer 1

1

You can add the year values using the text function and corresponding arrows using the arrows function. In each case effort provides the y-values for vertical placement.1 Add xpd=TRUE so that text/arrows outside the plot area won't be clipped. Note that I also changed the left margin to provide more room for the text and arrows.

par(mar=c(1,4,1,4))
image.scale(effort, col=pal.1(length(breaks)-1), breaks=breaks, horiz=FALSE, 
            yaxt="n", ylab="")
axis(4,at=breaks, las=2)
text(-0.11, effort, rownames(effort), cex=0.8, col="red", xpd=TRUE)
arrows(-0.07, effort, x1=-0.01, length=0.1, col="red", xpd=TRUE)

In the graph below, note that some of the years overlap, so you may need to shift text and arrows for overlapping values.

enter image description here


1 Wouldn't a scatterplot (year on the x-axis, effort on the y-axis) do a better job of clarifying the relationship? The color scale seems like overkill unless your real data is multivariate and this is just a toy example.

1
  • Thanks a lot! It's because I'm plotting a graph with a bunch of stuff in it. But I want to show that the graph was plotted under a certain condition of effort. Thus I found that adding a scale like this one could be a good visual way to indicate what's going on with the other parameter (effort). I just need to find a way to add this scale to my graph Jun 28, 2016 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.