在for循环中使用变量更改图标题

时间:2019-02-06 10:52:08

标签: r

我试图更改打印图标题,因为我在for循环中输出了5个图,但是当我尝试分配main = i时,标题变成了i中变量的所有值,而不是{ {1}}本身。

我的代码如下:

i

因此标题不会变成x1 <- rnorm(20); x2 <- rnorm(100); x3 <- runif(100); x4 <- rnorm(30); x5 <- rnorm(40) distributions = list(x1, x2, x3, x4,x5) for (i in distributions) {hist(i, main=paste("Histogram of", i))} Histogram of x1 ...等,标题变成了这些变量中包含的实际值

我该怎么做?

1 个答案:

答案 0 :(得分:0)

我认为您需要遍历分布的长度并每次绘制直方图。

x1 <- rnorm(20); x2 <- rnorm(100); x3 <- runif(100); x4 <- rnorm(30); x5 <- rnorm(40)
distributions = list(x1, x2, x3, x4,x5)
for (i in 1:length(distributions)) {
hist(distributions[[i]], main=paste("Histogram of", i))}
相关问题