2009-09-19 22 views

答えて

25
b0 = 2.5; b1 = 2 
n = 100 
x = rnorm(n, 20, 15) 
y = b0 + b1*x + rnorm(n, 0, 15) 
plot(x, y) 
plot(x, y, type='n') 
text(x, y, round(y, 2), cex=0.45) 
text(x, y, round(y, 2), cex=0.8) 
text(x, y, paste(round(x, 2), round(y, 2), sep=", "), cex=0.8) # for (x, y), but this gets cluttered. 

alt text

文字サイズの使用cex(テキストのヘルプを参照してください)。 plot(x, y, type='n')を使用して、実際に何も描画せずにウィンドウを正しく設定してください。 ggplot2を用いる以外ヴィンスの回答に似

+1

マイナーポイント:5行目にタイプミスがあります(プロットではなくプロット)。あなたは数字ではなく文字列を返すので、私はformatやformatCを使うのではなく、使いたいと思うでしょう。試してみてください。たとえば、format(y、trim = TRUE、digits = 2) –

+0

Thanks Richie。私は形式について考えましたが、round()の関数は初心者にはもっと明らかになると考えました。そしてpaste()は文字列に変換されます。 – Vince

5

使用text()

plot(1:10, 1:10) 
text(5, 5, "Foo") 

やテキストを配置する上でのオプションのhelp(text)を参照してください。テキストや位置のベクトルを持っている場合は、 も

text(1:10, 1:10, LETTERS[1:10]) 

に似て何かを行うことができますので、関数がベクトル化されます。

1

b0 = 2.5; b1 = 2 
n = 20 
x = rnorm(n, 20, 15) 
y = b0 + b1*x + rnorm(n, 0, 15) 
dat<-data.frame(x,y) 
library(ggplot2) 
ggplot(data=dat)+geom_text(aes(x=x,y=y),size=4,label=paste(round(x, 2), round(y, 2), sep=", ")) 

文字サイズがサイズパラメータを変更することによって変更することができます。

+0

私はggplotをもっと調べる必要があります。人気が高まっているようです。私はハードのもののために格子を使ってきました。 – Vince

0
x <- 1/3 
plot(1,type="none",ann=FALSE) 
## text and values only 
text(mean(par("usr")[1:2]),mean(par("usr")[3:4])-par("cxy")[2]*2, 
    paste("z = ",round(x,2))) 
## text, values, and mathematical expressions 
text(mean(par("usr")[1:2]),mean(par("usr")[3:4]), 
    bquote(x^2==.(round(x,2)))) 
text(mean(par("usr")[1:2]),mean(par("usr")[3:4])-par("cxy")[2], 
    substitute(gamma==value,list(value=round(x,2)))) 
2

ggplot2を使用すると、ポイントとラベルの両方を追加できます。 aes()をggplot()に入れると、このaes()がすべてのgeomのデフォルトになるという利点があります。

b0 <- 2.5 
b1 <- 2 
n <- 20 
dat <- data.frame(x = rnorm(n, 20, 15)) 
dat$y <- b0 + b1*dat$x + rnorm(n, 0, 15) 
dat$text <- with(dat, paste(round(x, 2), round(y, 2), sep=", ")) 
library(ggplot2) 
ggplot(data=dat, aes(x = x, y = y, label = text)) + geom_point() + geom_text(size=4, hjust = 1, vjust = 1) 
0
:したがって、イアン・フェローの変更されたコードは次のようになり、この場合、あなたは一度だけ、xと値を指定する必要がありますが、それらは両方ともgeom_point()とgeom_text()

で使用されています

は多分これは

# example data 
dat <- data.frame(name = sample(letters[1:4],20, replace=T), x1 = rnorm(20,2), x2 = 42+x1*rnorm(20,0,2)) 
# plot the data 
plot(dat$x1,dat$x2) 
# use identify to print name for each 'dot' that you click with left mouse 
identify(dat$x1,dat$x2,labels=name) 
# When done identifying point, click with right mousebutton. 

私はインタラクティブな目的のために、この機能を好きにも役立ちます。プロット内のすべてのメンバーのために、このラインを

for i=1:6 
text(x(i),y(i),num2str(y(i))); 
end 

繰り返し:私はこの方法を発見し、それは非常に便利です

-1

かかわらggplotでこれを達成する方法を知ってはいけません。

+3

これは有効なRコードではありません。 – joran