【问题标题】:Change titles for each subplot using loop in R使用 R 中的循环更改每个子图的标题
【发布时间】:2020-01-14 03:57:42
【问题描述】:

我正在尝试使用 R 中的循环更改每个子图的标题。我知道有一个类似的问题 here,但我无法在我的情况下应用它。这是我的数据和代码:

# Pet size data and their names
color <- c('W', 'B', 'G', 'W', 'B', 'G', 'W', 'B', 'G', 'W', 'B', 'G', 'W', 'B', 'G', 'W', 'B', 'G')
mass <- c(10, 14, 20, 11, 16, 13, 11, 15, 10, 14, 23, 18, 12, 22, 20, 13, 14, 17)
name <- c('B', 'B', 'B', 'F', 'F', 'F', 'D', 'D', 'D', 'A', 'A', 'A', 'C', 'C', 'C', 'E', 'E', 'E')

# Make it into data frame
pet.stats <- data.frame(color, mass, name)

# Name of pets (I want to plot them by type of pets)
kitty <- c('A', 'B', 'C')
bunny <- c('D', 'E', 'F')

all.pets <- append(kitty, bunny)
all.pets <- as.factor(all.pets)

par(mfrow = c(2, 3), mar = c(4, 4, 2, 1), oma = c(0.5, 0.5, 0.5, 0.5), mgp = c(2.2, 0.7, 0))

# Loop through pet names and give title with pet type and pet name for each subplot
for (i in 1: nlevels(pet.stats$name)) {
barplot(pet.stats$mass[pet.stats$name == levels(pet.stats$name)[i]],
main = substitute(paste('Size of ', bold('lovely'),
ifelse(pet.stats$name[i] %in% kitty, 'kitty', 'bunny'),
' (', levels(pet.stats$name[i]), ')')),
xlab = 'Color', ylab = 'Mass', names = c('White', 'Black', 'Gray'))
abline(h = 0)
}

这就是我得到的:

我希望每个子图都有一个标题“可爱(宠物类型)(宠物名称)的大小”,例如“可爱兔子'D'的大小”

有人可以帮忙解决我做错了什么吗?谢谢。

【问题讨论】:

  • 似乎substitute() 没有得到变量。作为旁注,不应该使用as.character(pet.stats$name[i])) 之类的东西来获得pet name 吗?否则,您将给出一个包含所有级别的向量。
  • 感谢您的网址。在我发布之前我没有看到这个。

标签: r loops plot title


【解决方案1】:

好的,这就是你的解决方案。问题在于 R 和非标准评估。 substitute 函数正在冻结函数的评估。获得所需行为的方法是在 env 变量中指定您需要评估的内容。

注意,我分解了每个变量,然后在main 中构造了它们。这样可以更轻松地查看正在发生的事情。

# Pet size data and their names
color <- c('W', 'B', 'G', 'W', 'B', 'G', 
           'W', 'B', 'G', 'W', 'B', 'G', 
           'W', 'B', 'G', 'W', 'B', 'G')
mass <- c(10, 14, 20, 11, 16, 13, 11, 15, 
          10, 14, 23, 18, 12, 22, 20, 13, 14, 17)
name <- c('B', 'B', 'B', 'F', 'F', 'F', 'D', 
          'D', 'D', 'A', 'A', 'A', 'C', 'C', 
          'C', 'E', 'E', 'E')

# Make it into data frame
pet.stats <- data.frame(color, mass, name)

# Name of pets (I want to plot them by type of pets)
kitty <- c('A', 'B', 'C')
bunny <- c('D', 'E', 'F')

all.pets <- append(kitty, bunny)
all.pets <- as.factor(all.pets)

现在注意传递给substitute的变量列表


par(mfrow = c(2, 3), mar = c(4, 4, 2, 1), oma = c(0.5, 0.5, 0.5, 0.5), mgp = c(2.2, 0.7, 0))

# Loop through pet names and give title with pet type and pet name for each subplot
for (i in 1: nlevels(pet.stats$name)) {

# Break out the names
  animal_type <- ifelse(pet.stats$name[i] %in% kitty, 'kitty', 'bunny')
  animal_names <- levels(pet.stats$name)[i]

  barplot(pet.stats$mass[pet.stats$name == levels(pet.stats$name)[i]],
          main = substitute(paste('Size of ', bold('lovely'),
                                  animal_type,
                                  ' (', animal_names, ')'),
                            env = list(animal_type = animal_type,
                                       animal_names = animal_names)),
          xlab = 'Color', ylab = 'Mass', names = c('White', 'Black', 'Gray'))
  abline(h = 0)
}

【讨论】:

猜你喜欢
  • 2020-09-02
  • 2021-03-08
  • 2019-08-02
  • 2013-04-24
  • 2021-09-02
  • 2020-09-22
  • 1970-01-01
  • 2019-10-15
  • 1970-01-01
相关资源
最近更新 更多