1

プロット軸のタイトルのテキストに、データの計算を実行する関数を含めたいと思います。

たとえば、ここにいくつかのデータがあります

a<-data.frame(time="1000",x=rnorm(10,12,3))
b<-data.frame(time="2000",x=rnorm(50,13,4))
c<-data.frame(time="3500",x=rnorm(50,12,4))
d<-data.frame(time="5000",x=rnorm(7,14,5))
e<-data.frame(time="7000",x=rnorm(20,10,3))
f<-data.frame(time="7500",x=rnorm(15,11,3))
g<-data.frame(time="9000",x=rnorm(15,10,5))
h<-data.frame(time="9500",x=rnorm(35,30,2))
i<-data.frame(time="10000",x=rnorm(30,28,4))
a2i<-rbind(a,b,c,d,e,f,g,h,i) 

library(ggplot2)
a2i$time<-as.numeric(levels(a2i$time))[a2i$time] 
ggplot(a2i,aes(time,x))+stat_smooth()+geom_point()+
# now let's try to put on a label with a function
# mixed in with the text
#
xlab("Time (total number of observations = paste(length(a2i$x))))")
#
# but that's no good, the function is not executed, just printed
# How can I get a function to work in the axis title?

ここに画像の説明を入力

タイトルの「時間」を定数として保持したいのですが、データの変化に応じて関数が異なる結果を返すようにします。そこにも括弧を入れます。

paste() と expression() を試してみましたが、うまくいきません。どんなヒントでも大歓迎です。

4

1 に答える 1

3

最初に必要なテストを含む文字列をpasteまたはsprintfで作成し、それを にフィードする必要がありxlabます。特に、paste文字列内に配置しないでください。

xlab( paste( 
  "Time (total number of observations = ", 
  length(a2i$x), 
  ")", 
  sep="" 
) )

# Equivalently
xlab( sprintf( 
  "Time (total number of observations = %s)", 
  length(a2i$x) 
) )
于 2012-03-02T07:34:06.750 に答える