我想知道如何添加箭头直接指向绘图旁边的刻度上的某个值.
让我们说我在不同年份有不同的价值.我想通过直接指向比例来了解特定年份的价值.例如,我想知道2010年的价值是什么.那么比例将如下图所示:
我真的很感激(我不知道该怎么做),只用箭头打印没有图形的刻度,这样我就可以把我想要的刻度放到另一个图形上.有点像这样:
这是重现图像的代码,没有箭头(受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)
最佳答案 您可以使用箭头功能使用文本功能和相应的箭头添加年份值.在每种情况下,努力为垂直放置提供y值.1添加xpd = TRUE,以便绘图区域外的文本/箭头不会被剪裁.请注意,我还更改了左边距,以便为文本和箭头提供更多空间.
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)
在下图中,请注意某些年份重叠,因此您可能需要为重叠值移动文本和箭头.
1散点图(x轴上的年份,y轴上的努力)是否能更好地澄清这种关系?除非您的真实数据是多变量的,否则色标似乎有点过分,这只是一个玩具示例.