1

I'm trying to assemble a title for a histogram plot in R. The title I want derives from two sources, first a vector (death.cause) specifying, er, cause-of-death, and second a field from a data.frame, into which I index using a list, deathratecols, to get at the field.

> death.cause 

> [[1]] [1] "Heart Attack"
> 
> [[2]] [1] "Heart Failure"
> 
> [[3]] [1] "Pneumonia"


> deathratecols

> [1] 11 17 23

I want to assemble title like: Heart Attack (x-bar = 7.63763) , where x-bar is the mean of the data column. I can get the two parts to work separately as below.

> main=substitute(x, list(x=death.cause[[i]]) )) # gives title "Heart Attack" 

> main=substitute(bar(X)==k, list(k=mean(outcome[, deathratecols[i] ], na.rm=TRUE   ) )) # gives title x-bar=7.63763

But my efforts to put the two together using paste() have failed.

? someone point out the error of my ways please

4

2 回答 2

2

提前用sprintf语句格式化标题并将其传递给main. 简单的例子:

a <- "foo"
b <- "bar"
title <- sprintf("This combines %s and %s", a, b)
hist(rnorm(10), main = title)

也许是这样的(但可能不起作用,因为你没有提供一个可以使用的例子):

title <- sprintf("%s, (x-bar = %s)", death.cause[[i]], mean(outcome[, deathratecols[i]])
于 2012-10-11T20:06:00.753 回答
0

您可以像下面一样组合替换()和粘贴():

hist(outcome[, deathratecols[i]], 
     main=substitute(paste(death.cause[[i]], "(", bar(X), "=", k, ")"), list(k=mean(outcome[, deathratecols[i]], na.rm=TRUE))))

它对我有用。

于 2014-02-13T07:44:49.507 回答