Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向打印轴添加文本而不删除r中的现有轴标签_R_Plot_Labels_Axes - Fatal编程技术网

向打印轴添加文本而不删除r中的现有轴标签

向打印轴添加文本而不删除r中的现有轴标签,r,plot,labels,axes,R,Plot,Labels,Axes,我有下面的情节 xleft<-c(1,2,3) xright<-c(2,3,4) ybottom<-c(1,2,3) ytop<-c(2,3,4) plot(c(1,4),c(1,4),type="n",main="title",xlab="site.x",ylab="ylab") rect(xleft,ybottom,xright,ytop,col=c("blue","red","green")) 和添加文本?感谢您的建议。我认为这样做的方法,R FAQ 7.27“

我有下面的情节

xleft<-c(1,2,3)
xright<-c(2,3,4)
ybottom<-c(1,2,3)
ytop<-c(2,3,4)

plot(c(1,4),c(1,4),type="n",main="title",xlab="site.x",ylab="ylab")
rect(xleft,ybottom,xright,ytop,col=c("blue","red","green"))

和添加文本?感谢您的建议。

我认为这样做的方法,R FAQ 7.27“如何创建旋转轴标签?”也同意,就是在页边空白处留出空间,然后添加长标签文本

 mx=12
 my=12
 par(mar = c(mx,my, 4, 2) + 0.1)
 plot(c(1,4),c(1,4),type="n",main="title",xlab="site.x",ylab="ylab")
 rect(xleft,ybottom,xright,ytop,col=c("blue","red","green"))
 text(par()$usr[1]-0.5,y.label.position,y.label,xpd=TRUE,adj=1)
 text(y=par()$usr[3]-0.5,x=x.label.position,x.label,xpd=TRUE,adj=1,srt=90)
您需要调整字符串长度的
mx
my
,还可能需要调整文本中的负偏移量,以防止与标签冲突。另请注意使用
xpd=TRUE
停止将图形剪裁到地物区域。这也是使图例就位的关键:

 par(xpd=TRUE)
 legend(locator(1),legend=c("Species A","Species B","Species C"),
                             fill=c("blue", "red", "green"))
然后单击需要图例的位置,或将定位器(1)替换为
列表(x=-0.7,y=0.6)


使用
ggplot
软件包,您可能会发现一种更简单的方法。

来自@Spacedman的答案非常好,但我只想提供一个更新。他的代码(至少对我来说)在刻度标签上绘制轴标签。要更改标签的位置,您需要查看
par(“mfg”)
。我认为以下代码或多或少满足了您的要求:

par(mai=c(2.5, 2.25, 0.82, 0.42), mgp=c(9, 1, 0))
plot(c(1,4), c(1,4), xaxp=c(1,4,3), yaxp=c(1,4,3), type="n", 
  main="title", xlab="site.x", ylab="ylab")
rect(xleft, ybottom, xright, ytop, col=c("blue","red","green"))
axis(1, at=x.label.position, labels=x.label, las=2)
axis(2, at=y.label.position, labels=y.label, las=2)
par(xpd=TRUE)
legend(x=0.25, y=0.75, 
  legend=c("Text", "Text", "Text"),
  fill=c("blue", "red", "green"),
  title="Legend")
这将生成以下绘图:


正如@Spacedman所建议的,在
ggplot2
软件包中,许多对边距和轴的更改更为直接(一旦您习惯了…!)。

我过去曾使用过一点ggplot2。您认为使用ggplot2解决此问题的一般方法是什么?对于使用边距特性,我将从以下内容开始:。对于绘制的项目,我会从这里开始:或者在您在这里提出的问题的伟大答案中:。
 par(xpd=TRUE)
 legend(locator(1),legend=c("Species A","Species B","Species C"),
                             fill=c("blue", "red", "green"))
par(mai=c(2.5, 2.25, 0.82, 0.42), mgp=c(9, 1, 0))
plot(c(1,4), c(1,4), xaxp=c(1,4,3), yaxp=c(1,4,3), type="n", 
  main="title", xlab="site.x", ylab="ylab")
rect(xleft, ybottom, xright, ytop, col=c("blue","red","green"))
axis(1, at=x.label.position, labels=x.label, las=2)
axis(2, at=y.label.position, labels=y.label, las=2)
par(xpd=TRUE)
legend(x=0.25, y=0.75, 
  legend=c("Text", "Text", "Text"),
  fill=c("blue", "red", "green"),
  title="Legend")