将文本添加到使用R中的函数创建的图上 [英] Add text to plots created with a function in r

查看:57
本文介绍了将文本添加到使用R中的函数创建的图上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用函数创建了一个包含两个图的图形,

I create a figure containing two plots using a function i.e.

data1<-1:3

basic<-function(data1){
par(mfrow=c(2,1))
plot(data1)
plot(data1)
}

basic(data1)

现在,我想在绘图中添加文本,而不必将其包含在函数中.但是我只能在底部绘图中添加文本(见下文).

Now I want to add text to the plots without having to include it in the function. But I can only add text to the bottom plot (see below).

text(x=c(1.5,1.6,1.7),y=c(2,2.1,2.2), labels=c("X","Y","Z"))

如何在函数外部的顶部绘图中添加文本?(我使用相同的功能创建了许多图形,但需要在每个文本上的稍有不同的位置放置稍有不同的文本标签).感谢您的任何建议.

How can I add text to the top plot outside of the function? (I have lots of figures I create using the same function but need to place slightly different text labels in slightly different positions on each one). Thanks for any advice.

推荐答案

您可以先概述一下不同的文本,例如

You could outline what the different texts are first e.g.

data1<-1:3

text1 <- data.frame(x=c(1.5,1.6,1.7),y=c(2,2.1,2.2), labels=c("X","Y","Z"))
text2 <- data.frame(x=c(2,2.9,1.0),y=c(2,2.1,2.2), labels=c("X","Y","Z"))

然后将它们放在您的函数中

then put them in your function

basic<-function(data1){
par(mfrow=c(2,1))
plot(data1)
text(text1)
plot(data1)
text(text2)
}

basic(data1)

@Andrie所说,调用图意味着任何进一步的 text()调用仅适用于最后一个图

As @Andrie says calling plot means any further text() calls will work on the last plot only

这篇关于将文本添加到使用R中的函数创建的图上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆