使用R中的循环更改每个子批次的标题

使用R中的循环更改每个子批次的标题,r,loops,plot,title,R,Loops,Plot,Title,我正在尝试使用R中的循环来更改每个子地块的标题。我知道有一个类似的问题,但我无法将其应用到我的案例中。以下是我的数据和代码: # 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

我正在尝试使用R中的循环来更改每个子地块的标题。我知道有一个类似的问题,但我无法将其应用到我的案例中。以下是我的数据和代码:

# 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)
}
#宠物大小数据及其名称

颜色好的,这是你的解决方案。问题在于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()
没有获取变量。作为旁注,
pet name
不应该使用类似于
As.character(pet.stats$name[i])的东西获取吗?否则,您将给出一个包含所有级别的向量。还要检查感谢URL。在我发布之前,我没有看到这张照片。谢谢你,这张照片很有效,给了我想要的东西!我将在这里检查我不知道的env变量()。

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)
}