使用 R 中的循环更改每个子图的标题
Change titles for each subplot using loop in R
我正在尝试使用 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)
}
这就是我得到的:
我希望每个子图都有一个标题 "Size of lovely (pet type) (pet name)" 例如 "Size of lovely bunny 'D'"
有人可以帮我解决我做错了什么吗?谢谢。
好的,这是您的解决方案。问题在于 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)
}
我正在尝试使用 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)
}
这就是我得到的:
我希望每个子图都有一个标题 "Size of lovely (pet type) (pet name)" 例如 "Size of lovely bunny 'D'"
有人可以帮我解决我做错了什么吗?谢谢。
好的,这是您的解决方案。问题在于 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)
}